Redis作为一个key-value存储系统。与和Memcached相比,它支持存储的value类型更多,有string、list、set、zset(sorted set )和hash。
针对这些类型,Redis命令也比较多:
而在代码中使用jedis就可以操作这些命令实现存储。
本博客打算封装一个个RedisManger类,用于实现操作缓存的命令。
Redis配置文件:
cache.redis.servers=127.0.0.1
cache.redis.port=6379
cache.redis.maxActive=300
cache.redis.maxIdle=200
cache.redis.maxWaitMillis=3000
cache.redis.testOnBorrow=true
cache.redis.testOnReturn=true
spring管理的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-2.5.xsd" >
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:Redis.properties</value>
</list>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${cache.redis.maxActive}" />
<property name="maxIdle" value="${cache.redis.maxIdle}" />
<property name="maxWaitMillis" value="${cache.redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${cache.redis.testOnBorrow}" />
</bean>
<bean id="redisPool" class="com.test.cache.redis.RedisPool" init-method="init">
<property name="config" ref="jedisPoolConfig" />
<property name="serverIp" value="${cache.redis.servers}" />
<property name="port" value="${cache.redis.port}" />
</bean>
<bean id="redisManager" class="com.test.cache.redis.RedisManager">
<property name="redisPool" ref="redisPool" />
</bean>
</beans>
连接池代码:
package com.test.cache.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisPool {
private JedisPoolConfig config;
private String serverIp;
private int port;
private JedisPool pool;
public void init() {
pool = new JedisPool(config, serverIp, port, 4000);
}
public Jedis getInstance() {
return pool.getResource();
}
public void returnResource(Jedis jedis) {
pool.returnResource(jedis);
}
public void returnBrokenResource(Jedis jedis){
pool.returnBrokenResource(jedis);
}
public JedisPoolConfig getConfig() {
return config;
}
public void setConfig(JedisPoolConfig config) {
this.config = config;
}
public String getServerIp() {
return serverIp;
}
public void setServerIp(String serverIp) {
this.serverIp = serverIp;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
封装的RedisManager类:
package com.test.cache.redis;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.*;
public class RedisManager {
private RedisPool redisPool;
public Jedis getJedis() {
return redisPool.getInstance();
}
public void releaseJedis(Jedis jedis) {
redisPool.returnResource(jedis);
}
public void releaseBrokenJedis(Jedis jedis) {
redisPool.returnBrokenResource(jedis);
}
/**hash
* 通过key给field设置指定的值,如果key不存在,则先创建 ,存在会覆盖原来的值
* @param key
* @param field字段
* @param value
* @return 如果不存在,新建的返回1,存在返回0, 异常返回null
*
*/
public Long hset(String key, String field, String value) {
Jedis jedis = getJedis();
Long result = jedis.hset(key, field, value);
releaseJedis(jedis);
return result;
}
/**Hash
* 为哈希表 key 中的域 field 的值加上增量 value
* @param key
* @param field
* @param value
* @return
*/
public Long hincrBy(String key, String field, long value) {
Jedis jedis = getJedis();
Long result = jedis.hincrBy(key, field, value);
releaseJedis(jedis);
return result;
}
/**
* 通过key给field设置指定的值,如果key不存在则先创建,如果field已经存在,操作无效
* @param key
* @param field
* @param value
* @return 不存在新建返回1,存在返回0
*/
public Long hsetnx(String key, String field, String value) {
Jedis jedis = getJedis();
Long result = jedis.hsetnx(key, field, value);
releaseJedis(jedis);
return result;
}
/**
* 通过key同时设置 hash的多个field
* @param key
* @param hash
* @return 返回OK 异常返回null
*/
public String hmset(String key, Map<String, String> hash) {
Jedis jedis = getJedis();
String result = jedis.hmset(key, hash);
releaseJedis(jedis);
return result;
}
/**
* 通过key 和 field 获取指定的 value
* @param key
* @param field
* @return 没有返回null
*/
public String