这个集群环境下和单机环境下:
package com.taotao.sso.dao.impl; import org.springframework.beans.factory.annotation.Autowired; import com.taotao.sso.dao.JedisClient; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
//单机版的dao实现类
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 hashKey, String key) {
Jedis jedis=jedisPool.getResource();
String string=jedis.hget(hashKey, key);
jedis.close();
return string;
} @Override
public Long hset(String hashKey, String key, String value) {
Jedis jedis=jedisPool.getResource();
Long result=jedis.hset(hashKey,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; } @Override
public long del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
jedis.close();
return result;
} @Override
public long hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(hkey, key);
jedis.close();
return result;
} }
集群下:
package com.taotao.sso.dao.impl; import org.springframework.beans.factory.annotation.Autowired; import com.taotao.sso.dao.JedisClient; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
//集群版的dao实现类
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);
}
//若内容修改后,更新缓存中的内容,将之前在缓存中的key删除
@Override
public long del(String key) { return jedisCluster.del(key);
} @Override
public long hdel(String hkey, String key) { return jedisCluster.hdel(hkey, key);
} }