从Redis缓存服务器获取省市信息
2016-03-17 11:04
791 查看
private List<Map<String, Object>> getProvinceCitiesFromRedis() throws Exception {
JedisTemplate reader = JedisTemplate.getReaderInstance();
String value = reader.get(PROVINCE_CITIES_REDIS_KEY);
// 若是从redis获取省市信息为空,则直接数据库,并将接口对象进行序列化,放入redis
if(StringUtils.isBlank(value)) {
// 获取省、市信息
List<Map<String, Object>> result = getProvinceCities();
// 将对象序列化成字符串
try {
value = serializeRovinceCities(result);
} catch (IOException e) {
logger.error(e);
}
// 将数据写入到Redis
writerToRedis(value);
logger.info("user province cities cache...");
}
// 将Redis中获取的信息反序列化成集合
return deserializeRovinceCities(value);
}
JedisTemplate
package com.lvmama.com.lvtu.common.utils;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.Pool;
import com.alibaba.fastjson.JSON;
public class JedisTemplate {
private static Logger logger = LoggerFactory.getLogger(JedisTemplate.class);
private Pool<Jedis> jedisPool;
private static JedisTemplate readerInstance;
private static JedisTemplate writerInstance;
private static Object ReadLock = new Object();
private static Object WriteLock = new Object();
private static Properties properties = ConfigHelper.getProperties("redis.properties");
// redis是否启用
private static boolean isRedisEnable = "true".equals(properties.getProperty("redis.enable"));
public JedisTemplate(Pool<Jedis> jedisPool) {
this.jedisPool = jedisPool;
}
/**
* writer instance
*
* @return 如果配置了redis不启用,返回null
*/
public static JedisTemplate getWriterInstance(){
if (!isRedisEnable) {
return null;
}
if (writerInstance==null) {
synchronized(WriteLock) {
if (writerInstance==null) {
String ip = properties.getProperty("redis.writer.server");
int port = Integer.parseInt(properties.getProperty("redis.writer.port"));
int maxIdle = Integer.parseInt(properties.getProperty("redis.writer.maxIdle"));
int maxTotal = Integer.parseInt(properties.getProperty("redis.writer.maxTotal"));
int checkingIntervalSecs = Integer.parseInt(properties.getProperty("redis.writer.checkingIntervalSecs"));
int evictableIdleTimeSecs = Integer.parseInt(properties.getProperty("redis.writer.evictableIdleTimeSecs"));
int maxWaitMillis = Integer.parseInt(properties.getProperty("redis.writer.maxWaitMillis"));
JedisPoolConfig config = JedisUtils.createPoolConfig(maxIdle, maxTotal, checkingIntervalSecs, evictableIdleTimeSecs, maxWaitMillis);
config.setTestWhileIdle(true);
writerInstance = new JedisTemplate(new JedisPool(config, ip, port));
}
}
}
return writerInstance;
}
/**
* reader instance
*
* @return 如果配置了redis不启用,返回null
*/
public static JedisTemplate getReaderInstance(){
if (!isRedisEnable) {
return null;
}
if (readerInstance==null) {
synchronized(ReadLock) {
if (readerInstance==null) {
String ip = properties.getProperty("redis.reader.server");
int port = Integer.parseInt(properties.getProperty("redis.reader.port"));
int maxIdle = Integer.parseInt(properties.getProperty("redis.reader.maxIdle"));
int maxTotal = Integer.parseInt(properties.getProperty("redis.reader.maxTotal"));
int checkingIntervalSecs = Integer.parseInt(properties.getProperty("redis.reader.checkingIntervalSecs"));
int evictableIdleTimeSecs = Integer.parseInt(properties.getProperty("redis.reader.evictableIdleTimeSecs"));
int maxWaitMillis = Integer.parseInt(properties.getProperty("redis.reader.maxWaitMillis"));
JedisPoolConfig config = JedisUtils.createPoolConfig(maxIdle, maxTotal, checkingIntervalSecs, evictableIdleTimeSecs, maxWaitMillis);
config.setTestWhileIdle(true);
readerInstance = new JedisTemplate(new JedisPool(config, ip, port));
}
}
}
return readerInstance;
}
/**
* 执行有返回结果的action。
*/
public <T> T execute(JedisAction<T> jedisAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
return jedisAction.action(jedis);
} catch (JedisConnectionException e) {
logger.error("Redis connection lost.", e);
broken = true;
throw e;
} finally {
closeResource(jedis, broken);
}
}
/**
* 执行无返回结果的action。
*/
public void execute(JedisActionNoResult jedisAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
jedisAction.action(jedis);
} catch (JedisConnectionException e) {
logger.error("Redis connection lost.", e);
broken = true;
throw e;
} finally {
closeResource(jedis, broken);
}
}
/**
* 根据连接是否已中断的标志,分别调用returnBrokenResource或returnResource。
*/
protected void closeResource(Jedis jedis, boolean connectionBroken) {
if (jedis != null) {
try {
if (connectionBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
} catch (Exception e) {
logger.error("Error happen when return jedis to pool, try to close it directly.", e);
JedisUtils.closeJedis(jedis);
}
}
}
/**
* 获取内部的pool做进一步的动作。
*/
public Pool<Jedis> getJedisPool() {
return jedisPool;
}
/**
* 有返回结果的回调接口定义。
*/
public interface JedisAction<T> {
T action(Jedis jedis);
}
/**
* 无返回结果的回调接口定义。
*/
public interface JedisActionNoResult {
void action(Jedis jedis);
}
// ////////////// 常用方法的封装 ///////////////////////// //
// ////////////// 公共 ///////////////////////////
/**
* 删除key, 如果key存在返回true, 否则返回false。
*/
public Boolean del(final String... keys) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.del(keys) == 1;
}
});
}
public void flushDB() {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.flushDB();
}
});
}
// ////////////// 关于String ///////////////////////////
/**
* 如果key不存在, 返回null.
*/
public String get(final String key) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.get(key);
}
});
}
/**
* 如果key不存在, 返回null.
*/
public Long getAsLong(final String key) {
String result = get(key);
return result != null ? Long.valueOf(result) : null;
}
/**
* 如果key不存在, 返回null.
*/
public Integer getAsInt(final String key) {
String result = get(key);
return result != null ? Integer.valueOf(result) : null;
}
public void set(final String key, final String value) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.set(key, value);
}
});
}
public void setex(final String key, final String value, final int seconds) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.setex(key, seconds, value);
}
});
}
/**
* 如果key还不存在则进行设置,返回true,否则返回false.
*/
public Boolean setnx(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.setnx(key, value) == 1;
}
});
}
/**
* 综合setNX与setEx的效果。
*/
public Boolean setnxex(final String key, final String value, final int seconds) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
String result = jedis.set(key, value, "NX", "EX", seconds);
return JedisUtils.isStatusOk(result);
}
});
}
public Long incr(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.incr(key);
}
});
}
public Long decr(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.decr(key);
}
});
}
// ////////////// 关于List ///////////////////////////
public void lpush(final String key, final String... values) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.lpush(key, values);
}
});
}
public String rpop(final String key) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.rpop(key);
}
});
}
public String hmset(final String key,final Map<String,String> map){
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.hmset(key, map);
}
});
}
public Long hdel(final String key,final String ...fields){
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
// TODO Auto-generated method stub
return jedis.hdel(key, fields);
}
});
}
public List<String> hmget(final String key,final String ...fields){
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
// TODO Auto-generated method stub
return jedis.hmget(key, fields);
}
});
}
/**
* 返回List长度, key不存在时返回0,key类型不是list时抛出异常.
*/
public Long llen(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.llen(key);
}
});
}
/**
* 查询list中的指定范围的元素
*/
public List<String> lrange(final String key,final long start, final long end) {
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
return jedis.lrange(key, start, end);
}
});
}
/**
* 删除List中的第一个等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremOne(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return (jedis.lrem(key, 1, value)==1);
}
});
}
/**
* 删除List中的所有等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremAll(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return (jedis.lrem(key, 0, value) > 0);
}
});
}
// ////////////// 关于Set ///////////////////////////
/**
* 加入set, 如果member在Set里已存在,否则返回true.
*/
public Boolean sadd(final String key, final String ... members) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.sadd(key, members) > 0;
}
});
}
/**
* 删除set中的元素,成功删除返回true,key或member不存在返回false。
*/
public Boolean srem(final String key, final String... members) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.srem(key, members) > 0;
}
});
}
/**
* 返回集合 key 中的所有成员。
* 不存在的 key 被视为空集合。
* @param key
* @return
*/
public Set<String> smembers(final String key) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
logger.info("jedis smembers(), key="+key);
return jedis.smembers(key);
}
});
}
/**
* 返回set长度, key不存在时返回0.
*/
public Long scard(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.scard(key);
}
});
}
// ////////////// 关于Sorted Set ///////////////////////////
/**
* 加入Sorted set, 如果member在Set里已存在, 只更新score并返回false, 否则返回true.
*/
public Boolean zadd(final String key, final String member, final double score) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.zadd(key, score, member) == 1;
}
});
}
/**
* 删除sorted set中的元素,成功删除返回true,key或member不存在返回false。
*/
public Boolean zrem(final String key, final String member) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.zrem(key, member) == 1;
}
});
}
/**
* 当key不存在时返回null.
*/
public Double zscore(final String key, final String member) {
return execute(new JedisAction<Double>() {
@Override
public Double action(Jedis jedis) {
return jedis.zscore(key, member);
}
});
}
/**
* 返回sorted set长度, key不存在时返回0.
*/
public Long zcard(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.zcard(key);
}
});
}
// ////////////// 关于 List<Object> ///////////////////////////
public <T> void setArray(final String key, final List<T> value, final int seconds) {
if (isRedisEnable)
this.setex(key, JSON.toJSONString(value), seconds);
}
public <T> void setArray(final String key, final List<T> value) {
if (isRedisEnable)
this.set(key, JSON.toJSONString(value));
}
/** key 不存在时返回 null */
public <T> List<T> getArray(final String key, final Class<T> clazz) {
if (!isRedisEnable)
return null;
return execute(new JedisAction<List<T>>() {
@Override
public List<T> action(Jedis jedis) {
logger.info("jedis getArray(), key="+key);
return JSON.parseArray(jedis.get(key), clazz);
}
});
}
public Long expire(final String key, final int seconds) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.expire(key, seconds);
}
});
}
public Set<String> keys(final String key) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
return jedis.keys(key);
}
});
}
}
JedisTemplate reader = JedisTemplate.getReaderInstance();
String value = reader.get(PROVINCE_CITIES_REDIS_KEY);
// 若是从redis获取省市信息为空,则直接数据库,并将接口对象进行序列化,放入redis
if(StringUtils.isBlank(value)) {
// 获取省、市信息
List<Map<String, Object>> result = getProvinceCities();
// 将对象序列化成字符串
try {
value = serializeRovinceCities(result);
} catch (IOException e) {
logger.error(e);
}
// 将数据写入到Redis
writerToRedis(value);
logger.info("user province cities cache...");
}
// 将Redis中获取的信息反序列化成集合
return deserializeRovinceCities(value);
}
JedisTemplate
package com.lvmama.com.lvtu.common.utils;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.Pool;
import com.alibaba.fastjson.JSON;
public class JedisTemplate {
private static Logger logger = LoggerFactory.getLogger(JedisTemplate.class);
private Pool<Jedis> jedisPool;
private static JedisTemplate readerInstance;
private static JedisTemplate writerInstance;
private static Object ReadLock = new Object();
private static Object WriteLock = new Object();
private static Properties properties = ConfigHelper.getProperties("redis.properties");
// redis是否启用
private static boolean isRedisEnable = "true".equals(properties.getProperty("redis.enable"));
public JedisTemplate(Pool<Jedis> jedisPool) {
this.jedisPool = jedisPool;
}
/**
* writer instance
*
* @return 如果配置了redis不启用,返回null
*/
public static JedisTemplate getWriterInstance(){
if (!isRedisEnable) {
return null;
}
if (writerInstance==null) {
synchronized(WriteLock) {
if (writerInstance==null) {
String ip = properties.getProperty("redis.writer.server");
int port = Integer.parseInt(properties.getProperty("redis.writer.port"));
int maxIdle = Integer.parseInt(properties.getProperty("redis.writer.maxIdle"));
int maxTotal = Integer.parseInt(properties.getProperty("redis.writer.maxTotal"));
int checkingIntervalSecs = Integer.parseInt(properties.getProperty("redis.writer.checkingIntervalSecs"));
int evictableIdleTimeSecs = Integer.parseInt(properties.getProperty("redis.writer.evictableIdleTimeSecs"));
int maxWaitMillis = Integer.parseInt(properties.getProperty("redis.writer.maxWaitMillis"));
JedisPoolConfig config = JedisUtils.createPoolConfig(maxIdle, maxTotal, checkingIntervalSecs, evictableIdleTimeSecs, maxWaitMillis);
config.setTestWhileIdle(true);
writerInstance = new JedisTemplate(new JedisPool(config, ip, port));
}
}
}
return writerInstance;
}
/**
* reader instance
*
* @return 如果配置了redis不启用,返回null
*/
public static JedisTemplate getReaderInstance(){
if (!isRedisEnable) {
return null;
}
if (readerInstance==null) {
synchronized(ReadLock) {
if (readerInstance==null) {
String ip = properties.getProperty("redis.reader.server");
int port = Integer.parseInt(properties.getProperty("redis.reader.port"));
int maxIdle = Integer.parseInt(properties.getProperty("redis.reader.maxIdle"));
int maxTotal = Integer.parseInt(properties.getProperty("redis.reader.maxTotal"));
int checkingIntervalSecs = Integer.parseInt(properties.getProperty("redis.reader.checkingIntervalSecs"));
int evictableIdleTimeSecs = Integer.parseInt(properties.getProperty("redis.reader.evictableIdleTimeSecs"));
int maxWaitMillis = Integer.parseInt(properties.getProperty("redis.reader.maxWaitMillis"));
JedisPoolConfig config = JedisUtils.createPoolConfig(maxIdle, maxTotal, checkingIntervalSecs, evictableIdleTimeSecs, maxWaitMillis);
config.setTestWhileIdle(true);
readerInstance = new JedisTemplate(new JedisPool(config, ip, port));
}
}
}
return readerInstance;
}
/**
* 执行有返回结果的action。
*/
public <T> T execute(JedisAction<T> jedisAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
return jedisAction.action(jedis);
} catch (JedisConnectionException e) {
logger.error("Redis connection lost.", e);
broken = true;
throw e;
} finally {
closeResource(jedis, broken);
}
}
/**
* 执行无返回结果的action。
*/
public void execute(JedisActionNoResult jedisAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
jedisAction.action(jedis);
} catch (JedisConnectionException e) {
logger.error("Redis connection lost.", e);
broken = true;
throw e;
} finally {
closeResource(jedis, broken);
}
}
/**
* 根据连接是否已中断的标志,分别调用returnBrokenResource或returnResource。
*/
protected void closeResource(Jedis jedis, boolean connectionBroken) {
if (jedis != null) {
try {
if (connectionBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
} catch (Exception e) {
logger.error("Error happen when return jedis to pool, try to close it directly.", e);
JedisUtils.closeJedis(jedis);
}
}
}
/**
* 获取内部的pool做进一步的动作。
*/
public Pool<Jedis> getJedisPool() {
return jedisPool;
}
/**
* 有返回结果的回调接口定义。
*/
public interface JedisAction<T> {
T action(Jedis jedis);
}
/**
* 无返回结果的回调接口定义。
*/
public interface JedisActionNoResult {
void action(Jedis jedis);
}
// ////////////// 常用方法的封装 ///////////////////////// //
// ////////////// 公共 ///////////////////////////
/**
* 删除key, 如果key存在返回true, 否则返回false。
*/
public Boolean del(final String... keys) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.del(keys) == 1;
}
});
}
public void flushDB() {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.flushDB();
}
});
}
// ////////////// 关于String ///////////////////////////
/**
* 如果key不存在, 返回null.
*/
public String get(final String key) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.get(key);
}
});
}
/**
* 如果key不存在, 返回null.
*/
public Long getAsLong(final String key) {
String result = get(key);
return result != null ? Long.valueOf(result) : null;
}
/**
* 如果key不存在, 返回null.
*/
public Integer getAsInt(final String key) {
String result = get(key);
return result != null ? Integer.valueOf(result) : null;
}
public void set(final String key, final String value) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.set(key, value);
}
});
}
public void setex(final String key, final String value, final int seconds) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.setex(key, seconds, value);
}
});
}
/**
* 如果key还不存在则进行设置,返回true,否则返回false.
*/
public Boolean setnx(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.setnx(key, value) == 1;
}
});
}
/**
* 综合setNX与setEx的效果。
*/
public Boolean setnxex(final String key, final String value, final int seconds) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
String result = jedis.set(key, value, "NX", "EX", seconds);
return JedisUtils.isStatusOk(result);
}
});
}
public Long incr(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.incr(key);
}
});
}
public Long decr(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.decr(key);
}
});
}
// ////////////// 关于List ///////////////////////////
public void lpush(final String key, final String... values) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.lpush(key, values);
}
});
}
public String rpop(final String key) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.rpop(key);
}
});
}
public String hmset(final String key,final Map<String,String> map){
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.hmset(key, map);
}
});
}
public Long hdel(final String key,final String ...fields){
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
// TODO Auto-generated method stub
return jedis.hdel(key, fields);
}
});
}
public List<String> hmget(final String key,final String ...fields){
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
// TODO Auto-generated method stub
return jedis.hmget(key, fields);
}
});
}
/**
* 返回List长度, key不存在时返回0,key类型不是list时抛出异常.
*/
public Long llen(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.llen(key);
}
});
}
/**
* 查询list中的指定范围的元素
*/
public List<String> lrange(final String key,final long start, final long end) {
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
return jedis.lrange(key, start, end);
}
});
}
/**
* 删除List中的第一个等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremOne(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return (jedis.lrem(key, 1, value)==1);
}
});
}
/**
* 删除List中的所有等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremAll(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return (jedis.lrem(key, 0, value) > 0);
}
});
}
// ////////////// 关于Set ///////////////////////////
/**
* 加入set, 如果member在Set里已存在,否则返回true.
*/
public Boolean sadd(final String key, final String ... members) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.sadd(key, members) > 0;
}
});
}
/**
* 删除set中的元素,成功删除返回true,key或member不存在返回false。
*/
public Boolean srem(final String key, final String... members) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.srem(key, members) > 0;
}
});
}
/**
* 返回集合 key 中的所有成员。
* 不存在的 key 被视为空集合。
* @param key
* @return
*/
public Set<String> smembers(final String key) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
logger.info("jedis smembers(), key="+key);
return jedis.smembers(key);
}
});
}
/**
* 返回set长度, key不存在时返回0.
*/
public Long scard(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.scard(key);
}
});
}
// ////////////// 关于Sorted Set ///////////////////////////
/**
* 加入Sorted set, 如果member在Set里已存在, 只更新score并返回false, 否则返回true.
*/
public Boolean zadd(final String key, final String member, final double score) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.zadd(key, score, member) == 1;
}
});
}
/**
* 删除sorted set中的元素,成功删除返回true,key或member不存在返回false。
*/
public Boolean zrem(final String key, final String member) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.zrem(key, member) == 1;
}
});
}
/**
* 当key不存在时返回null.
*/
public Double zscore(final String key, final String member) {
return execute(new JedisAction<Double>() {
@Override
public Double action(Jedis jedis) {
return jedis.zscore(key, member);
}
});
}
/**
* 返回sorted set长度, key不存在时返回0.
*/
public Long zcard(final String key) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.zcard(key);
}
});
}
// ////////////// 关于 List<Object> ///////////////////////////
public <T> void setArray(final String key, final List<T> value, final int seconds) {
if (isRedisEnable)
this.setex(key, JSON.toJSONString(value), seconds);
}
public <T> void setArray(final String key, final List<T> value) {
if (isRedisEnable)
this.set(key, JSON.toJSONString(value));
}
/** key 不存在时返回 null */
public <T> List<T> getArray(final String key, final Class<T> clazz) {
if (!isRedisEnable)
return null;
return execute(new JedisAction<List<T>>() {
@Override
public List<T> action(Jedis jedis) {
logger.info("jedis getArray(), key="+key);
return JSON.parseArray(jedis.get(key), clazz);
}
});
}
public Long expire(final String key, final int seconds) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.expire(key, seconds);
}
});
}
public Set<String> keys(final String key) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
return jedis.keys(key);
}
});
}
}
相关文章推荐
- Redis 学习 ---- 3.链表 Linked List
- 深入理解Redis主键失效原理及实现机制
- Redis安装配置
- Redis 学习 ---- 1.引言
- Redis 主从配置和参数详解
- PHP-redis中文说明
- Redis学习(一)-基础理论、字符串命令、列表结构
- CentOS6.5下redis安装部署配置指南、常用命令、主从同步集群、redis-php学习资料整合详解
- windows下安装redis服务 AND PHP中添加扩展 AND PHP中使用
- redis 在spring boot工程中的应用(四) 图片的读取与缓存 byte string
- redis 在spring boot工程中的应用(三) 问题发现与解决 :反序列化 报错
- 常用的 redis操作
- java(优化12) 15天玩转redis之第六篇
- Redis提供商配置ASP.NET会话状态
- redis学习笔记2--命令返回值
- 重温redis--redis在springmvc下的使用
- redis主从配置
- Redis主从配置
- Apache-Shiro分布式环境配置(与redis集成)
- redis