<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.7.2version>
dependency>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="30" />
<property name="maxIdle" value="10" />
<property name="numTestsPerEvictionRun" value="1024" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<property name="maxWaitMillis" value="1500" />
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="blockWhenExhausted" value="false" />
bean>
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="6379">constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
bean>
/**
* 单机版测试
*/
@Test
public void testSpringJedisSingle() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
Jedis jedis = pool.getResource();
String string = jedis.get("key1");
System.out.println(string);
jedis.close();
pool.close();
}
<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7001">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7002">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7003">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7004">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7005">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7006">constructor-arg>
bean>
set>
constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
bean>
@Test
public void testSpringJedisCluster() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
JedisCluster jedisCluster = (JedisCluster) applicationContext.getBean("redisClient");
String string = jedisCluster.get("key1");
System.out.println(string);
jedisCluster.close();
}
public class JedisClientSingle implements JedisClient{
@Autowired
private JedisPool jedisPool;
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
}
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
}
@Override
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
}
@Override
public long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
@Override
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return result;
}
@Override
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
}
id="jedisClient" class="com.test.redis.dao.JedisClientSingle">
public class JedisClientCluster implements JedisClient {
@Autowired
private JedisCluster jedisCluster;
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String hget(String hkey, String key) {
return jedisCluster.hget(hkey, key);
}
@Override
public long hset(String hkey, String key, String value) {
return jedisCluster.hset(hkey, key, value);
}
@Override
public long incr(String key) {
return jedisCluster.incr(key);
}
@Override
public long expire(String key, int second) {
return jedisCluster.expire(key, second);
}
@Override
public long ttl(String key) {
return jedisCluster.ttl(key);
}
}
//---------------------------------------------缓存添加--------------------
//从缓存中取内容
try{
String result=jedisClient.hget("test", id+"");
if(!StringUtils.isBlank(result)){
List list=JsonUtils.jsonToList(result, Test.class);
return list;
}
}catch(Exception e){
e.printStackTrace();
}
//-----------------------------------------------------------------
//---------------------------------------------缓存存储--------------------
//从缓存中添加内容
try{
//把list转换成字符串
String cacheString=JsonUtils.objectToJson(list);
jedisClient.hset("test",id+"", cacheString);
}catch(Exception e){
e.printStackTrace();
}
//----
待续