properties文件的配置
redis.addr = 127.0.0.1
redis.port = 6379redis.auth = root
application中的配置
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="200" />
<property name="maxTotal" value="1024" />
<property name="MaxWaitMillis" value="10000" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.addr}" p:port="${redis.port}" p:password="${redis.auth}"
p:pool-config-ref="poolConfig" p:database="0" />
<bean id="connectionFactory1"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.addr}" p:port="${redis.port}" p:password="${redis.auth}"
p:pool-config-ref="poolConfig" p:database="1" />
<bean id="connectionFactory2"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.addr}" p:port="${redis.port}" p:password="${redis.auth}"
p:pool-config-ref="poolConfig" p:database="2" />
<bean id="connectionFactory3"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.addr}" p:port="${redis.port}" p:password="${redis.auth}"
p:pool-config-ref="poolConfig" p:database="3" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="redisAuthTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory1" />
</bean>
<bean id="redisRegTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory2" />
</bean>
<bean id="redisSucTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory3" />
</bean>
不知道为什么,使用redisTemplate的存入map类型时,如果value为对象,只能存入一种类型。如果想存入另一种对象,则需要配另一个database
访客实体类
public class Visitor implements Serializable {
private static final long serialVersionUID = 1L;
public static final String OBJECT_KEY = "UV";
private String ip;
private String channelType;
private String date;
public Visitor() {
}
public Visitor(String ip) {
this.ip = ip;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getChannelType() {
return channelType;
}
public void setChannelType(String channelType) {
this.channelType = channelType;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public static String getObjectKey() {
return OBJECT_KEY;
}
}
实体类dao,map的key为UV,value为map(key为渠道类型,value为访客类)
@Service
public class VisitorDao extends BaseAction {
@Resource(name = "redisTemplate")
private RedisTemplate<String, Visitor> redisTemplate;
public void put(Visitor visitor) {
redisTemplate.opsForHash().put(Visitor.getObjectKey(), visitor.getIp(), visitor);
}
public void delete(Visitor visitor) {
redisTemplate.opsForHash().delete(Visitor.getObjectKey(), visitor.getIp());
}
public Visitor get(Visitor visitor) {
return (Visitor) redisTemplate.opsForHash().get(Visitor.getObjectKey(), visitor.getIp());
}
public void deleteAll(String key) {
redisTemplate.delete(key);
}
public Set<Object> getAllIp() {
return redisTemplate.opsForHash().keys(Visitor.getObjectKey());
}
public int getCountByChannel(String channel) {
int visitorCount = 0;
final Set<Object> ips = getAllIp();
for (Object ip : ips) {
if (get(new Visitor((String) ip)).getChannelType().equals(channel)) {
visitorCount++;
}
}
return visitorCount;
}
}
记录点击次数的实体类
public class AuthCount implements Serializable {
private static final long serialVersionUID = 1L;
public static final String OBJECT_KEY = "auth_count";
private String channelType;
private int count;
public AuthCount() {
}
public AuthCount(String channelType) {
this.channelType = channelType;
}
public AuthCount(String channelType,int count) {
this.channelType = channelType;
this.count=count;
}
public String getChannelType() {
return channelType;
}
public void setChannelType(String channelType) {
this.channelType = channelType;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public static String getObjectKey() {
return OBJECT_KEY;
}
}
点击次数的实体类,key为auth_count,value为map(key为渠道类型,value为实体类)
@Service
public class AuthCountDao{
@Resource(name = "redisAuthTemplate")
private RedisTemplate<String, AuthCount> redisTemplate;
public void put(AuthCount authCount) {
redisTemplate.opsForHash().put(AuthCount.getObjectKey(), authCount.getChannelType(), authCount);
}
public void delete(AuthCount authCount) {
redisTemplate.opsForHash().delete(AuthCount.getObjectKey(), authCount.getChannelType());
}
public AuthCount get(AuthCount authCount) {
return (AuthCount) redisTemplate.opsForHash().get(AuthCount.getObjectKey(), authCount.getChannelType());
}
public void deleteAll(String key) {
redisTemplate.delete(key);
}
public boolean exists() {
return redisTemplate.hasKey(AuthCount.getObjectKey());
}
public boolean channelExists(AuthCount authCount) {
return this.get(authCount) == null ? false : true;
}
}