3、redis应用的核心文件:
public class RedisCache {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private JedisPool pool;
public JedisPool getPool() {
return pool;
}
public void setPool(JedisPool pool) {
this.pool = pool;
}
public Map
Jedis jedis = pool.getResource();
String info;
try {
info = jedis.info(section == null || section.isEmpty() ? "default" : section);
} finally {
pool.returnResource(jedis);
}
Map
try (BufferedReader reader = new BufferedReader(new StringReader(info))) {
String key = null;
Map
for (String line; (line = reader.readLine()) != null;) {
line = line.trim();
if (line.isEmpty())
continue;
if (line.charAt(0) == '#') {
key = line.substring(1).trim();
values = new LinkedHashMap<>();
result.put(key, values);
} else {
String[] tmp = line.split(":", 2);
values.put(tmp[0], tmp.length == 1 ? null : tmp[1]);
}
}
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return result;
}
public boolean exists(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.exists(key);
} finally {
pool.returnResource(jedis);
}
}
/**
* 设置过期时间
* @param key
* @param seconds 秒
* @return 1:设置成功,0:key不存在或设置失败
*/
public Long expire(String key, int seconds) {
Jedis jedis = pool.getResource();
try {
return jedis.expire(key, seconds);
} finally {
pool.returnResource(jedis);
}
}
/**
* 设置毫秒过期
* @param key
* @param milliseconds 毫秒
* @return 1:设置成功,0:key不存在或设置失败
*/
public Long pexpire(String key, int milliseconds) {
Jedis jedis = pool.getResource();
try {
return jedis.pexpire(key, milliseconds);
} finally {
pool.returnResource(jedis);
}
}
public Long ttl(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.ttl(key);
} finally {
pool.returnResource(jedis);
}
}
public String get(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.get(key);
} finally {
pool.returnResource(jedis);
}
}
/**
* 同时获取多个key对应的value(m为Multiple的缩写)
* @param keys
* @return 依次返回多个key对应的value集合
*/
public List
Jedis jedis = pool.getResource();
try {
return jedis.mget(keys);
} finally {
pool.returnResource(jedis);
}
}
/**
* 同时获取多个key对应的value(m为Multiple的缩写)
*
* @param keys
* @param limit 限制单次
* @return
*/
public List
int size = keys.length;
List
if (size > limit) {
int index = 0;
while (index < size) {
String[] sub = new String[Math.min(limit, size - index)];
System.arraycopy(keys, index, sub, 0, sub.length);
result.addAll(mget(sub));
index += limit;
}
return result;
} else {
return mget(keys);
}
}
public void set(String key, String value) {
Jedis jedis = pool.getResource();
try {
jedis.set(key, value);
} finally {
pool.returnResource(jedis);
}
}
/**
* set + expire的组合使用,设置key、value、过期时间
* @param key
* @param seconds 过期时间(秒)
* @param value
*/
public void setex(String key, int seconds, String value) {
Jedis jedis = pool.getResource();
try {
jedis.setex(key, seconds, value);
} finally {
pool.returnResource(jedis);
}
}
public void del(String key) {
Jedis jedis = pool.getResource();
try {
jedis.del(key);
} finally {
pool.returnResource(jedis);
}
}
/**
* 同时删除多个key
* @param key
*/
public void del(String... key) {
Jedis jedis = pool.getResource();
try {
jedis.del(key);
} finally {
pool.returnResource(jedis);
}
}
/**
*
* @param pattern
* @return
*/
public Set
Jedis jedis = pool.getResource();
try {
return jedis.keys(pattern);
} finally {
pool.returnResource(jedis);
}
}
public Set
Jedis jedis = pool.getResource();
try {
return jedis.hkeys(key);
} finally {
pool.returnResource(jedis);
}
}
/**
* hash+get:获取哈希结构的数据
* @param key
* @param field
* @return
*/
public String hget(String key, String field) {
Jedis jedis = pool.getResource();
try {
return jedis.hget(key, field);
} finally {
pool.returnResource(jedis);
}
}
/**
* 根据key获取所有哈希的数据
* @param key
* @return
*/
public Map
Jedis jedis = pool.getResource();
try {
return jedis.hgetAll(key);
} finally {
pool.returnResource(jedis);
}
}
/**
* 一次获取多个fields的值:hash+Multiple+get
*
* @param fields
* 会被自动转换为字符串
*/
public List
Jedis jedis = pool.getResource();
try {
return jedis.hmget(key, fields);
} finally {
pool.returnResource(jedis);
}
}
/**
* 根据field删除hash中的value
* @param key
* @param field
* @return
*/
public Long hdel(String key, String field) {
Jedis jedis = pool.getResource();
try {
return jedis.hdel(key, field);
} finally {
pool.returnResource(jedis);
}
}
/**
* 设置多个hash的值:hash+Multiple+set
* @param key
* @param hash
*/
public void hmset(String key, Map
Jedis jedis = pool.getResource();
try {
jedis.hmset(key, hash);
} finally {
pool.returnResource(jedis);
}
}
/**
* 哈希设置:hash+set
* @param key
* @param field
* @param value
*/
public void hset(String key, String field, String value) {
Jedis jedis = pool.getResource();
try {
jedis.hset(key, field, value);
} finally {
pool.returnResource(jedis);
}
}
/**
* hash+set+nx(if not exists)
* 将哈希表key中的域field的值设置为value,当且仅当域field 不存在。 若域 field 已经存在,该操作无效。 如果 key
* 不存在,一个新哈希表被创建并执行 HSETNX 命令。
*
* @return true设置成功
*/
public boolean hsetnx(String key, String field, String value) {
Jedis jedis = pool.getResource();
try {
return jedis.hsetnx(key, field, value) == 1;
} finally {
pool.returnResource(jedis);
}
}
public void lpush(String key, String... values) {
Jedis jedis = pool.getResource();
try {
jedis.lpush(key, values);
} finally {
pool.returnResource(jedis);
}
}
public void rpush(String key, String... values) {
Jedis jedis = pool.getResource();
try {
jedis.rpush(key, values);
} finally {
pool.returnResource(jedis);
}
}
public List
Jedis jedis = pool.getResource();
try {
return jedis.lrange(key, offset, end);
} finally {
pool.returnResource(jedis);
}
}
public Long llen(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.llen(key);
} finally {
pool.returnResource(jedis);
}
}
public String rpop(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.rpop(key);
} finally {
pool.returnResource(jedis);
}
}
public String lpop(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.lpop(key);
} finally {
pool.returnResource(jedis);
}
}
public void zadd(String key, double score, String member) {
Jedis jedis = pool.getResource();
try {
jedis.zadd(key, score, member);
} finally {
pool.returnResource(jedis);
}
}
public Long sadd(String key, String member) {
Jedis jedis = pool.getResource();
try {
return jedis.sadd(key, member);
} finally {
pool.returnResource(jedis);
}
}
public Set
Jedis jedis = pool.getResource();
try {
return jedis.zrange(key, offset, end);
} finally {
pool.returnResource(jedis);
}
}
public Set
Jedis jedis = pool.getResource();
try {
return jedis.zrevrange(key, offset, end);
} finally {
pool.returnResource(jedis);
}
}
public Long zrem(final String key, final String... members) {
Jedis jedis = pool.getResource();
try {
return jedis.zrem(key, members);
} finally {
pool.returnResource(jedis);
}
}
public Long zremrangeByRank(final String key, final long start, final long end) {
Jedis jedis = pool.getResource();
try {
return jedis.zremrangeByRank(key, start, end);
} finally {
pool.returnResource(jedis);
}
}
/**
* 计数器加1
*
* @param key
* @param expire
* 过期时间,单位秒,传入-1则永不过期
* @return
*/
public long incr(String key, int expire) {
Jedis jedis = pool.getResource();
try {
long value = jedis.incr(key);
if (expire != -1) {
// 设置过期时间
jedis.expire(key, expire);
}
return value;
} finally {
pool.returnResource(jedis);
}
}
/**
* 计数器加一个指定的整数
*
* @param key
* @param integer 步进值,增加的值
* @param expire 过期时间,单位秒,传入-1则永不过期
* @return
*/
public long incrBy(String key, Long integer, int expire) {
Jedis jedis = pool.getResource();
try {
long value = jedis.incrBy(key, integer);
if (expire != -1) {
// 设置过期时间
jedis.expire(key, expire);
}
return value;
} finally {
pool.returnResource(jedis);
}
}
/**
* 计数器减1
*
* @param key
* @param expire
* 过期时间,单位秒,传入-1则永不过期
* @return
*/
public long decr(String key, int expire) {
Jedis jedis = pool.getResource();
try {
long value = jedis.decr(key);
if (expire != -1) {
// 设置过期时间
jedis.expire(key, expire);
}
return value;
} finally {
pool.returnResource(jedis);
}
}
/**
* 计数器减一个整数
*
* @param key
* @param integer
* 步进值
* @param expire
* 过期时间,单位秒,传入-1则永不过期
* @return
*/
public long decrBy(String key, Long integer, int expire) {
Jedis jedis = pool.getResource();
try {
long value = jedis.decrBy(key, integer);
if (expire != -1) {
// 设置过期时间
jedis.expire(key, expire);
}
return value;
} finally {
pool.returnResource(jedis);
}
}
}