#393 补充JedisTemplate和JedisSharedTemplate的方法

This commit is contained in:
Calvin
2014-09-13 17:51:03 +08:00
parent 38485571a9
commit f3d84344ad
16 changed files with 790 additions and 206 deletions
@@ -16,10 +16,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springside.modules.nosql.redis.JedisTemplate.JedisAction;
import org.springside.modules.nosql.redis.pool.JedisPool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.util.Pool;
/**
* 装载并执行Lua Script,
@@ -34,7 +34,7 @@ public class JedisScriptExecutor {
private String script;
private String sha1;
public JedisScriptExecutor(Pool<Jedis> jedisPool) {
public JedisScriptExecutor(JedisPool jedisPool) {
this.jedisTemplate = new JedisTemplate(jedisPool);
}
@@ -1,12 +1,8 @@
/*------------------------------------------------------------------------------
* COPYRIGHT Ericsson 2013
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* The copyright to the computer program(s) herein is the property of
* Ericsson Inc. The programs may be used and/or copied only with written
* permission from Ericsson Inc. or in accordance with the terms and
* conditions stipulated in the agreement/contract under which the
* program(s) have been supplied.
*----------------------------------------------------------------------------*/
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis;
import java.util.List;
@@ -19,12 +15,20 @@ import org.springside.modules.nosql.redis.JedisTemplate.JedisAction;
import org.springside.modules.nosql.redis.JedisTemplate.JedisActionNoResult;
import org.springside.modules.nosql.redis.pool.JedisPool;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.Hashing;
/**
* JedisShardTemplate, 在JedisTemplate的基础上支持按key进行sharding分区,
* 如果只传入一个JedisPool,则会退化到JedisTemplate, 不会进行sharding运算。
* JedisShardTemplate is the JedisTemplate, which has key sharding feature.
*
* Pass more than one JedisPool to the it, it will calculate which jedisPool will handle the key. If
* only one jedisPool passed, it won't do the calculation, so please use JedisShardedTemplate by default.
*
* Limitation: JedisShardedTemplate donen't support multi-key actions like:
* 1. Lua Script
* 2. Pipelined and Transaction process multi-key
* 3. Methods in @redis.clients.jedis.MultiKeyCommands like mget, rpoplpush....
*/
public class JedisShardedTemplate {
@@ -32,7 +36,7 @@ public class JedisShardedTemplate {
private TreeMap<Long, JedisTemplate> nodes = new TreeMap<Long, JedisTemplate>();
private JedisTemplate singleTemplate = null;
public JedisShardedTemplate(JedisPool[] jedisPools) {
public JedisShardedTemplate(JedisPool... jedisPools) {
if (jedisPools.length == 1) {
singleTemplate = new JedisTemplate(jedisPools[0]);
} else {
@@ -40,10 +44,14 @@ public class JedisShardedTemplate {
}
}
public JedisShardedTemplate(List<JedisPool> jedisPools) {
this(jedisPools.toArray(new JedisPool[jedisPools.size()]));
}
private void initNodes(JedisPool... jedisPools) {
for (int i = 0; i != jedisPools.length; i++) {
// 环上更多的点让负载更均衡
for (int n = 0; n < 5; n++) {
// more entry the make the hash ring be more balance
for (int n = 0; n < 128; n++) {
final JedisPool jedisPool = jedisPools[i];
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), new JedisTemplate(jedisPool));
}
@@ -51,34 +59,31 @@ public class JedisShardedTemplate {
}
/**
* 对Key进行哈希并返回哈希环上对应的节点。
* 参考了Jedis的Sharded.java
* Hash the key and get the jedisTemplate from the hash ring. Get idea from Jedis's Sharded.java
*/
public JedisTemplate getShard(String key) {
if (singleTemplate != null) {
return singleTemplate;
}
SortedMap<Long, JedisTemplate> tail = nodes.tailMap(algo.hash(key));
// 已到最后,返回第一个点.
if (tail.isEmpty()) {
// the last node, back to first.
return nodes.get(nodes.firstKey());
}
return tail.get(tail.firstKey());
}
/**
* 包含key以帮助sharding的execute 版本.
/*
* Execute the action, the action must process only one key.
*/
public <T> T execute(String key, JedisAction<T> jedisAction) throws JedisException {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.execute(jedisAction);
}
/**
* 包含key以帮助sharding的execute 版本.
/*
* Execute the action, the action must process only one key.
*/
public void execute(String key, JedisActionNoResult jedisAction) throws JedisException {
JedisTemplate jedisTemplate = getShard(key);
@@ -99,6 +104,16 @@ public class JedisShardedTemplate {
return jedisTemplate.get(key);
}
public Long getAsLong(final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.getAsLong(key);
}
public Integer getAsInt(final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.getAsInt(key);
}
public void set(final String key, final String value) {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.set(key, value);
@@ -119,16 +134,36 @@ public class JedisShardedTemplate {
return jedisTemplate.setnxex(key, value, seconds);
}
public String getSet(String key, String value) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.getSet(key, value);
}
public Long incr(String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.incr(key);
}
public Long incrBy(String key, Long increment) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.incrBy(key, increment);
}
public Double incrByFloat(final String key, final double increment) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.incrByFloat(key, increment);
}
public Long decr(String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.decr(key);
}
public Long decrBy(String key, Long decrement) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.decrBy(key, decrement);
}
// / Hash Actions ///
public String hget(final String key, final String field) {
@@ -136,35 +171,66 @@ public class JedisShardedTemplate {
return jedisTemplate.hget(key, field);
}
public void hset(final String key, final String field, final String value) {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.hset(key, field, value);
}
public List<String> hmget(String key, String[] fields) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hmget(key, fields);
}
public Map<String, String> hgetAll(final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hgetAll(key);
}
public void hset(final String key, final String field, final String value) {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.hset(key, field, value);
}
public void hmset(String key, Map<String, String> map) {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.hmset(key, map);
}
public Boolean hsetnx(final String key, final String fieldName, final String value) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hsetnx(key, fieldName, value);
}
public Long hincrBy(final String key, final String fieldName, final long increment) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hincrBy(key, fieldName, increment);
}
public Double hincrByFloat(final String key, final String fieldName, final double increment) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hincrByFloat(key, fieldName, increment);
}
public Long hdel(final String key, final String... fieldsName) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hdel(key, fieldsName);
}
public Boolean hexists(final String key, final String fieldName) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hexists(key, fieldName);
}
public Set<String> hkeys(final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.hkeys(key);
}
// ////////////// 关于List ///////////////////////////
public void lpush(final String key, final String... values) {
public Long hlen(final String key) {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.lpush(key, values);
return jedisTemplate.hlen(key);
}
// / List Actions ///
public Long lpush(final String key, final String... values) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.lpush(key, values);
}
public String rpop(final String key) {
@@ -172,58 +238,133 @@ public class JedisShardedTemplate {
return jedisTemplate.rpop(key);
}
/**
* 返回List长度, key不存在时返回0,key类型不是list时抛出异常.
*/
public String brpop(final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.brpop(key);
}
public String brpop(final int timeout, final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.brpop(timeout, key);
}
public Long llen(final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.llen(key);
}
/**
* 删除List中的第一个等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremOne(final String key, final String value) {
public String lindex(final String key, final long index) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.lremOne(key, value);
return jedisTemplate.lindex(key, index);
}
public List<String> lrange(final String key, final int start, final int end) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.lrange(key, start, end);
}
public void ltrim(final String key, final int start, final int end) {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.ltrim(key, start, end);
}
public void ltrimFromLeft(final String key, final int size) {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.ltrimFromLeft(key, size);
}
public Boolean lremFirst(final String key, final String value) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.lremFirst(key, value);
}
/**
* 删除List中的所有等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremAll(final String key, final String value) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.lremAll(key, value);
}
// ////////////// 关于Sorted Set ///////////////////////////
/**
* 加入Sorted set, 如果member在Set里已存在, 只更新score并返回false, 否则返回true.
*/
public Boolean zadd(final String key, final String member, final double score) {
// / Sorted Set Actions ///
public Boolean zadd(final String key, final double score, final String member) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zadd(key, member, score);
return jedisTemplate.zadd(key, score, member);
}
/**
* 删除sorted set中的元素,成功删除返回true,key或member不存在返回false。
*/
public Boolean zrem(final String key, final String member) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrem(key, member);
}
/**
* 当key不存在时返回null.
*/
public Double zscore(final String key, final String member) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zscore(key, member);
}
/**
* 返回sorted set长度, key不存在时返回0.
*/
public Long zrank(final String key, final String member) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrank(key, member);
}
public Long zrevrank(final String key, final String member) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrevrank(key, member);
}
public Long zcount(final String key, final double start, final double end) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zcount(key, start, end);
}
public Set<String> zrange(final String key, final int start, final int end) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrange(key, start, end);
}
public Set<Tuple> zrangeWithScores(final String key, final int start, final int end) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrangeWithScores(key, start, end);
}
public Set<String> zrevrange(final String key, final int start, final int end) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrevrange(key, start, end);
}
public Set<Tuple> zrevrangeWithScores(final String key, final int start, final int end) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrevrangeWithScores(key, start, end);
}
public Set<String> zrangeByScore(final String key, final double min, final double max) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrangeByScore(key, min, max);
}
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrangeByScoreWithScores(key, min, max);
}
public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrevrangeByScore(key, max, min);
}
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrevrangeByScoreWithScores(key, max, min);
}
public Boolean zrem(final String key, final String member) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zrem(key, member);
}
public Long zremByScore(final String key, final double min, final double max) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zremByScore(key, min, max);
}
public Long zremByRank(final String key, final long start, final long end) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zremByRank(key, start, end);
}
public Long zcard(final String key) {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.zcard(key);
@@ -11,28 +11,35 @@ import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.nosql.redis.pool.JedisPool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.Pool;
/**
* JedisTemplate 提供了一个template方法,负责对Jedis连接的获取与归还。
* JedisAction<T> 和 JedisActionNoResult两种回调接口,适用于有无返回值两种情况。
* 同时提供一些JedisOperation中定义的 最常用函数的封装, 如get/set/zadd等。
*/
/**
* JedisTemplate is the template to execute jedis actions, fetch and return the connection from the pool correctly.
*/
public class JedisTemplate {
private static Logger logger = LoggerFactory.getLogger(JedisTemplate.class);
private Pool<Jedis> jedisPool;
private JedisPool jedisPool;
public JedisTemplate(Pool<Jedis> jedisPool) {
public JedisTemplate(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
/**
* 执行有返回结果的action。
* Execute with a call back action with result.
*/
public <T> T execute(JedisAction<T> jedisAction) throws JedisException {
Jedis jedis = null;
@@ -40,9 +47,8 @@ public class JedisTemplate {
try {
jedis = jedisPool.getResource();
return jedisAction.action(jedis);
} catch (JedisConnectionException e) {
logger.error("Redis connection lost.", e);
broken = true;
} catch (JedisException e) {
broken = handleJedisException(e);
throw e;
} finally {
closeResource(jedis, broken);
@@ -50,7 +56,7 @@ public class JedisTemplate {
}
/**
* 执行无返回结果的action。
* Execute with a call back action without result.
*/
public void execute(JedisActionNoResult jedisAction) throws JedisException {
Jedis jedis = null;
@@ -58,9 +64,8 @@ public class JedisTemplate {
try {
jedis = jedisPool.getResource();
jedisAction.action(jedis);
} catch (JedisConnectionException e) {
logger.error("Redis connection lost.", e);
broken = true;
} catch (JedisException e) {
broken = handleJedisException(e);
throw e;
} finally {
closeResource(jedis, broken);
@@ -68,56 +73,76 @@ public class JedisTemplate {
}
/**
* 根据连接是否已中断的标志,分别调用returnBrokenResource或returnResource。
* Return the internal JedisPool.
*/
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.destroyJedis(jedis);
}
}
}
/**
* 获取内部的pool做进一步的动作。
*/
public Pool<Jedis> getJedisPool() {
public JedisPool getJedisPool() {
return jedisPool;
}
/**
* 有返回结果的回调接口定义。
* Handle jedisException, write log and return whether the connection is broken.
*/
protected boolean handleJedisException(JedisException jedisException) {
if (jedisException instanceof JedisConnectionException) {
logger.error("Redis connection " + jedisPool.getAddress() + " lost.", jedisException);
} else if (jedisException instanceof JedisDataException) {
if ((jedisException.getMessage() != null) && (jedisException.getMessage().indexOf("READONLY") != -1)) {
logger.error("Redis connection " + jedisPool.getAddress() + " are read-only slave.", jedisException);
} else {
// dataException, isBroken=false
return false;
}
} else {
logger.error("Jedis exception happen.", jedisException);
}
return true;
}
/**
* Return jedis connection to the pool, call different return methods depends on the conectionBroken status.
*/
protected void closeResource(Jedis jedis, boolean conectionBroken) {
try {
if (conectionBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
} catch (Exception e) {
logger.error("return back jedis failed, will fore close the jedis.", e);
JedisUtils.destroyJedis(jedis);
}
}
/**
* Callback interface for template.
*/
public interface JedisAction<T> {
T action(Jedis jedis);
}
/**
* 无返回结果的回调接口定义。
* Callback interface for template without result.
*/
public interface JedisActionNoResult {
void action(Jedis jedis);
}
// ////////////// 常用方法的封装 ///////////////////////// //
// / Common Actions ///
// ////////////// 公共 ///////////////////////////
/**
* 删除key, 如果key存在返回true, 否则返回false。
* Remove the specified keys. If a given key does not exist no operation is
* performed for this key.
*
* return false if one of the key is not exist.
*/
public Boolean del(final String... keys) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.del(keys) == 1 ? true : false;
return jedis.del(keys) == keys.length ? true : false;
}
});
}
@@ -132,11 +157,13 @@ public class JedisTemplate {
});
}
// ////////////// 关于String ///////////////////////////
/**
* 如果key不存在, 返回null.
*/
// / String Actions ///
/**
* Get the value of the specified key. If the key does not exist null is
* returned. If the value stored at key is not a string an error is returned
* because GET can only handle string values.
*/
public String get(final String key) {
return execute(new JedisAction<String>() {
@@ -148,7 +175,7 @@ public class JedisTemplate {
}
/**
* 如果key不存在, 返回null.
* Get the value of the specified key as Long.If the key does not exist null is returned.
*/
public Long getAsLong(final String key) {
String result = get(key);
@@ -156,13 +183,32 @@ public class JedisTemplate {
}
/**
* 如果key不存在, 返回null.
* Get the value of the specified key as Integer.If the key does not exist null is returned.
*/
public Integer getAsInt(final String key) {
String result = get(key);
return result != null ? Integer.valueOf(result) : null;
}
/**
* Get the values of all the specified keys. If one or more keys dont exist
* or is not of type String, a 'nil' value is returned instead of the value
* of the specified key, but the operation never fails.
*/
public List<String> mget(final String... keys) {
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
return jedis.mget(keys);
}
});
}
/**
* Set the string value as value of the key. The string can't be longer than
* 1073741824 bytes (1 GB).
*/
public void set(final String key, final String value) {
execute(new JedisActionNoResult() {
@@ -173,6 +219,11 @@ public class JedisTemplate {
});
}
/**
* The command is exactly equivalent to the following group of commands: {@link #set(String, String) SET} +
* {@link #expire(String, int) EXPIRE}.
* The operation is atomic.
*/
public void setex(final String key, final String value, final int seconds) {
execute(new JedisActionNoResult() {
@@ -184,9 +235,12 @@ public class JedisTemplate {
}
/**
* 如果key还不存在则进行设置,返回true,否则返回false.
* SETNX works exactly like {@link #setNX(String, String) SET} with the only
* difference that if the key already exists no operation is performed.
* SETNX actually means "SET if Not eXists".
*
* return true if the key was set.
*/
public Boolean setnx(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@@ -198,9 +252,10 @@ public class JedisTemplate {
}
/**
* 综合setNX与setEx的效果。
* The command is exactly equivalent to the following group of commands: {@link #setex(String, String, int) SETEX} +
* {@link #sexnx(String, String) SETNX}.
* The operation is atomic.
*/
public Boolean setnxex(final String key, final String value, final int seconds) {
return execute(new JedisAction<Boolean>() {
@@ -212,6 +267,34 @@ public class JedisTemplate {
});
}
/**
* GETSET is an atomic set this value and return the old value command. Set
* key to the string value and return the old value stored at key. The
* string can't be longer than 1073741824 bytes (1 GB).
*/
public String getSet(final String key, final String value) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.getSet(key, value);
}
});
}
/**
* Increment the number stored at key by one. If the key does not exist or
* contains a value of a wrong type, set the key to the value of "0" before
* to perform the increment operation.
* <p>
* INCR commands are limited to 64 bit signed integers.
* <p>
* Note: this is actually a string operation, that is, in Redis there are not "integer" types. Simply the string
* stored at the key is parsed as a base 10 64 bit signed integer, incremented, and then converted back as a string.
*
* @return Integer reply, this commands will reply with the new value of key
* after the increment.
*/
public Long incr(final String key) {
return execute(new JedisAction<Long>() {
@Override
@@ -221,6 +304,29 @@ public class JedisTemplate {
});
}
public Long incrBy(final String key, final long increment) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.incrBy(key, increment);
}
});
}
public Double incrByFloat(final String key, final double increment) {
return execute(new JedisAction<Double>() {
@Override
public Double action(Jedis jedis) {
return jedis.incrByFloat(key, increment);
}
});
}
/**
* Decrement the number stored at key by one. If the key does not exist or
* contains a value of a wrong type, set the key to the value of "0" before
* to perform the decrement operation.
*/
public Long decr(final String key) {
return execute(new JedisAction<Long>() {
@Override
@@ -230,32 +336,55 @@ public class JedisTemplate {
});
}
// ////////////// 关于Hash ///////////////////////////
public String hget(final String key, final String field) {
return execute(new JedisAction<String>() {
public Long decrBy(final String key, final long decrement) {
return execute(new JedisAction<Long>() {
@Override
public String action(Jedis jedis) {
return jedis.hget(key, field);
public Long action(Jedis jedis) {
return jedis.decrBy(key, decrement);
}
});
}
public void hset(final String key, final String field, final String value) {
// / Hash Actions ///
/**
* If key holds a hash, retrieve the value associated to the specified
* field.
* <p>
* If the field is not found or the key does not exist, a special 'nil' value is returned.
*/
public String hget(final String key, final String fieldName) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.hget(key, fieldName);
}
});
}
public List<String> hmget(final String key, final String[] fieldsName) {
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
return jedis.hmget(key, fieldsName);
}
});
}
public Map<String, String> hgetAll(final String key) {
return execute(new JedisAction<Map<String, String>>() {
@Override
public Map<String, String> action(Jedis jedis) {
return jedis.hgetAll(key);
}
});
}
public void hset(final String key, final String fieldName, final String value) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.hset(key, field, value);
}
});
}
public List<String> hmget(final String key, final String[] fields) {
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
return jedis.hmget(key, fields);
jedis.hset(key, fieldName, value);
}
});
}
@@ -268,6 +397,35 @@ public class JedisTemplate {
jedis.hmset(key, map);
}
});
}
public Boolean hsetnx(final String key, final String fieldName, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.hsetnx(key, fieldName, value) == 1 ? true : false;
}
});
}
public Long hincrBy(final String key, final String fieldName, final long increment) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.hincrBy(key, fieldName, increment);
}
});
}
public Double hincrByFloat(final String key, final String fieldName, final double increment) {
return execute(new JedisAction<Double>() {
@Override
public Double action(Jedis jedis) {
return jedis.hincrByFloat(key, fieldName, increment);
}
});
}
public Long hdel(final String key, final String... fieldsName) {
@@ -279,6 +437,15 @@ public class JedisTemplate {
});
}
public Boolean hexists(final String key, final String fieldName) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.hexists(key, fieldName);
}
});
}
public Set<String> hkeys(final String key) {
return execute(new JedisAction<Set<String>>() {
@Override
@@ -288,12 +455,22 @@ public class JedisTemplate {
});
}
// ////////////// 关于List ///////////////////////////
public void lpush(final String key, final String... values) {
execute(new JedisActionNoResult() {
public Long hlen(final String key) {
return execute(new JedisAction<Long>() {
@Override
public void action(Jedis jedis) {
jedis.lpush(key, values);
public Long action(Jedis jedis) {
return jedis.hlen(key);
}
});
}
// / List Actions ///
public Long lpush(final String key, final String... values) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.lpush(key, values);
}
});
}
@@ -308,9 +485,62 @@ public class JedisTemplate {
});
}
public String brpop(final String key) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
List<String> nameValuePair = jedis.brpop(key);
if (nameValuePair != null) {
return nameValuePair.get(1);
} else {
return null;
}
}
});
}
public String brpop(final int timeout, final String key) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
List<String> nameValuePair = jedis.brpop(timeout, key);
if (nameValuePair != null) {
return nameValuePair.get(1);
} else {
return null;
}
}
});
}
/**
* 返回List长度, key不存在时返回0,key类型不是list时抛出异常.
* Not support for sharding.
*/
public String rpoplpush(final String sourceKey, final String destinationKey) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.rpoplpush(sourceKey, destinationKey);
}
});
}
/**
* Not support for sharding.
*/
public String brpoplpush(final String source, final String destination, final int timeout) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.brpoplpush(source, destination, timeout);
}
});
}
public Long llen(final String key) {
return execute(new JedisAction<Long>() {
@@ -321,10 +551,45 @@ public class JedisTemplate {
});
}
/**
* 删除List中的第一个等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremOne(final String key, final String value) {
public String lindex(final String key, final long index) {
return execute(new JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.lindex(key, index);
}
});
}
public List<String> lrange(final String key, final int start, final int end) {
return execute(new JedisAction<List<String>>() {
@Override
public List<String> action(Jedis jedis) {
return jedis.lrange(key, start, end);
}
});
}
public void ltrim(final String key, final int start, final int end) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.ltrim(key, start, end);
}
});
}
public void ltrimFromLeft(final String key, final int size) {
execute(new JedisActionNoResult() {
@Override
public void action(Jedis jedis) {
jedis.ltrim(key, 0, size - 1);
}
});
}
public Boolean lremFirst(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
@@ -334,9 +599,6 @@ public class JedisTemplate {
});
}
/**
* 删除List中的所有等于value的元素,value不存在或key不存在时返回false.
*/
public Boolean lremAll(final String key, final String value) {
return execute(new JedisAction<Boolean>() {
@Override
@@ -347,11 +609,37 @@ public class JedisTemplate {
});
}
// ////////////// 关于Sorted Set ///////////////////////////
// / Set Actions ///
/**
* 加入Sorted set, 如果member在Set里已存在, 只更新score并返回false, 否则返回true.
* @param key
* @param member
* @return true for add new element, false for only update the score.
*/
public Boolean zadd(final String key, final String member, final double score) {
public Boolean sadd(final String key, final String member) {
return execute(new JedisAction<Boolean>() {
@Override
public Boolean action(Jedis jedis) {
return jedis.sadd(key, member) == 1 ? true : false;
}
});
}
public Set<String> smembers(final String key) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
return jedis.smembers(key);
}
});
}
// / Ordered Set Actions ///
/**
* return true for add new element, false for only update the score.
*/
public Boolean zadd(final String key, final double score, final String member) {
return execute(new JedisAction<Boolean>() {
@Override
@@ -361,22 +649,6 @@ public class JedisTemplate {
});
}
/**
* 删除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 ? true : false;
}
});
}
/**
* 当key不存在时返回null.
*/
public Double zscore(final String key, final String member) {
return execute(new JedisAction<Double>() {
@@ -387,9 +659,146 @@ public class JedisTemplate {
});
}
/**
* 返回sorted set长度, key不存在时返回0.
*/
public Long zrank(final String key, final String member) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.zrank(key, member);
}
});
}
public Long zrevrank(final String key, final String member) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.zrevrank(key, member);
}
});
}
public Long zcount(final String key, final double min, final double max) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.zcount(key, min, max);
}
});
}
public Set<String> zrange(final String key, final int start, final int end) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
return jedis.zrange(key, start, end);
}
});
}
public Set<Tuple> zrangeWithScores(final String key, final int start, final int end) {
return execute(new JedisAction<Set<Tuple>>() {
@Override
public Set<Tuple> action(Jedis jedis) {
return jedis.zrangeWithScores(key, start, end);
}
});
}
public Set<String> zrevrange(final String key, final int start, final int end) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
return jedis.zrevrange(key, start, end);
}
});
}
public Set<Tuple> zrevrangeWithScores(final String key, final int start, final int end) {
return execute(new JedisAction<Set<Tuple>>() {
@Override
public Set<Tuple> action(Jedis jedis) {
return jedis.zrevrangeWithScores(key, start, end);
}
});
}
public Set<String> zrangeByScore(final String key, final double min, final double max) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
return jedis.zrangeByScore(key, min, max);
}
});
}
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
return execute(new JedisAction<Set<Tuple>>() {
@Override
public Set<Tuple> action(Jedis jedis) {
return jedis.zrangeByScoreWithScores(key, min, max);
}
});
}
public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
return execute(new JedisAction<Set<String>>() {
@Override
public Set<String> action(Jedis jedis) {
return jedis.zrevrangeByScore(key, max, min);
}
});
}
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
return execute(new JedisAction<Set<Tuple>>() {
@Override
public Set<Tuple> action(Jedis jedis) {
return jedis.zrevrangeByScoreWithScores(key, max, min);
}
});
}
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 ? true : false;
}
});
}
public Long zremByScore(final String key, final double start, final double end) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.zremrangeByScore(key, start, end);
}
});
}
public Long zremByRank(final String key, final long start, final long end) {
return execute(new JedisAction<Long>() {
@Override
public Long action(Jedis jedis) {
return jedis.zremrangeByRank(key, start, end);
}
});
}
public Long zcard(final String key) {
return execute(new JedisAction<Long>() {
@@ -1,3 +1,8 @@
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis.pool;
import redis.clients.jedis.Protocol;
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis.pool;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPoolConfig;
/**
* Pool which connect to redis instance directly.
*/
public class JedisDirectPool extends JedisPool {
public JedisDirectPool(HostAndPort address, JedisPoolConfig config) {
initInternalPool(address, new ConnectionInfo(), config);
}
public JedisDirectPool(HostAndPort address, ConnectionInfo connectionInfo, JedisPoolConfig config) {
initInternalPool(address, connectionInfo, config);
}
}
@@ -1,3 +1,8 @@
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis.pool;
import org.apache.commons.pool2.PooledObject;
@@ -1,3 +1,8 @@
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis.pool;
import org.apache.commons.pool2.impl.GenericObjectPool;
@@ -10,7 +15,7 @@ import redis.clients.util.Pool;
/**
* Jedis Pool base class.
*/
public class JedisPool extends Pool<Jedis> {
public abstract class JedisPool extends Pool<Jedis> {
protected HostAndPort address;
@@ -32,18 +37,6 @@ public class JedisPool extends Pool<Jedis> {
return config;
}
protected JedisPool() {
}
public JedisPool(HostAndPort address, JedisPoolConfig config) {
initInternalPool(address, new ConnectionInfo(), config);
}
public JedisPool(HostAndPort address, ConnectionInfo connectionInfo, JedisPoolConfig config) {
initInternalPool(address, connectionInfo, config);
}
/**
* Initialize the internal pool with connection info and pool config.
*/
@@ -61,7 +54,9 @@ public class JedisPool extends Pool<Jedis> {
*/
@Override
public void returnBrokenResource(final Jedis resource) {
returnBrokenResourceObject(resource);
if (resource != null) {
returnBrokenResourceObject(resource);
}
}
/**
@@ -69,8 +64,10 @@ public class JedisPool extends Pool<Jedis> {
*/
@Override
public void returnResource(final Jedis resource) {
resource.resetState();
returnResourceObject(resource);
if (resource != null) {
resource.resetState();
returnResourceObject(resource);
}
}
public HostAndPort getAddress() {
@@ -1,3 +1,8 @@
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis.pool;
import java.util.ArrayList;
@@ -154,7 +159,7 @@ public class JedisPoolBuilder {
logger.info("Building JedisDirectPool, on redis server " + hostPort[0] + " ,sentinelPort is " + hostPort[1]);
HostAndPort masterAddress = new HostAndPort(hostPort[0], Integer.parseInt(hostPort[1]));
return new JedisPool(masterAddress, config);
return new JedisDirectPool(masterAddress, config);
}
private JedisPool buildSentinelPool(String sentinelMasterName, ConnectionInfo connectionInfo, JedisPoolConfig config) {
@@ -1,3 +1,8 @@
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis.pool;
import java.util.ArrayList;
@@ -51,7 +56,7 @@ public final class JedisSentinelPool extends JedisPool {
assertArgument(((sentinelAddresses == null) || (sentinelAddresses.length == 0)), "seintinelInfos is not set");
for (HostAndPort sentinelInfo : sentinelAddresses) {
JedisPool sentinelPool = new JedisPool(sentinelInfo, new JedisPoolConfig());
JedisPool sentinelPool = new JedisDirectPool(sentinelInfo, new JedisPoolConfig());
sentinelPools.add(sentinelPool);
}
@@ -19,11 +19,11 @@ import org.slf4j.LoggerFactory;
import org.springside.modules.nosql.redis.JedisTemplate;
import org.springside.modules.nosql.redis.JedisTemplate.JedisActionNoResult;
import org.springside.modules.nosql.redis.JedisUtils;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.utils.Threads;
import org.springside.modules.utils.Threads.WrapExceptionRunnable;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;
/**
* Master选举实现, 基于setNx()与expire()两大API.
@@ -53,7 +53,7 @@ public class MasterElector implements Runnable {
private String masterKey = DEFAULT_MASTER_KEY;
private AtomicBoolean master = new AtomicBoolean(false);
public MasterElector(Pool<Jedis> jedisPool, int intervalSecs) {
public MasterElector(JedisPool jedisPool, int intervalSecs) {
this.jedisTemplate = new JedisTemplate(jedisPool);
this.intervalSecs = intervalSecs;
this.expireSecs = intervalSecs + (intervalSecs / 2);
@@ -14,12 +14,10 @@ import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.nosql.redis.JedisScriptExecutor;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.utils.Threads;
import org.springside.modules.utils.Threads.WrapExceptionRunnable;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;
import com.google.common.collect.Lists;
/**
@@ -50,7 +48,7 @@ public class JobDispatcher implements Runnable {
private String jobName;
private List<String> keys;
public JobDispatcher(String jobName, Pool<Jedis> jedisPool) {
public JobDispatcher(String jobName, JedisPool jedisPool) {
this.jobName = jobName;
String scheduledJobKey = Keys.getScheduledJobKey(jobName);
@@ -10,9 +10,7 @@ import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.nosql.redis.JedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;
import org.springside.modules.nosql.redis.pool.JedisPool;
/**
* 任务生成的管理器,支持任务的安排与取消。
@@ -28,7 +26,7 @@ public class JobProducer {
private String readyJobKey;
public JobProducer(String jobName, Pool<Jedis> jedisPool) {
public JobProducer(String jobName, JedisPool jedisPool) {
jedisTemplate = new JedisTemplate(jedisPool);
scheduledJobKey = Keys.getScheduledJobKey(jobName);
readyJobKey = Keys.getReadyJobKey(jobName);
@@ -46,7 +44,7 @@ public class JobProducer {
*/
public void schedule(final String job, final long delay, final TimeUnit timeUnit) {
final long delayTimeMillis = System.currentTimeMillis() + timeUnit.toMillis(delay);
jedisTemplate.zadd(scheduledJobKey, job, delayTimeMillis);
jedisTemplate.zadd(scheduledJobKey, delayTimeMillis, job);
}
/**
@@ -6,9 +6,7 @@
package org.springside.modules.nosql.redis.service.scheduler;
import org.springside.modules.nosql.redis.JedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;
import org.springside.modules.nosql.redis.pool.JedisPool;
/**
* 支持对当前任务池情况的状态数据查询.
@@ -25,7 +23,7 @@ public class JobStatistics {
private String dispatchCounterKey;
private String retryCounterKey;
public JobStatistics(String jobName, Pool<Jedis> jedisPool) {
public JobStatistics(String jobName, JedisPool jedisPool) {
scheduledJobKey = Keys.getScheduledJobKey(jobName);
readyJobKey = Keys.getReadyJobKey(jobName);
lockJobKey = Keys.getLockJobKey(jobName);
@@ -11,11 +11,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.nosql.redis.JedisTemplate;
import org.springside.modules.nosql.redis.JedisTemplate.JedisAction;
import org.springside.modules.nosql.redis.pool.JedisPool;
import org.springside.modules.utils.Threads;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.util.Pool;
/*
* 简单的基于brpop()API, 阻塞的取出任务。
@@ -32,7 +32,7 @@ public class SimpleJobConsumer {
private String readyJobKey;
private int popupTimeoutSecs = DEFAULT_POPUP_TIMEOUT_SECONDS;
public SimpleJobConsumer(String jobName, Pool<Jedis> jedisPool) {
public SimpleJobConsumer(String jobName, JedisPool jedisPool) {
jedisTemplate = new JedisTemplate(jedisPool);
readyJobKey = Keys.getReadyJobKey(jobName);
}
@@ -119,7 +119,7 @@ public class JedisShardedTemplateTest {
jedisTemplate.lpush(key, value);
jedisTemplate.lpush(key, value);
assertThat(jedisTemplate.llen(key)).isEqualTo(3);
assertThat(jedisTemplate.lremOne(key, value)).isTrue();
assertThat(jedisTemplate.lremFirst(key, value)).isTrue();
assertThat(jedisTemplate.llen(key)).isEqualTo(2);
assertThat(jedisTemplate.lremAll(key, value)).isTrue();
assertThat(jedisTemplate.llen(key)).isEqualTo(0);
@@ -136,8 +136,8 @@ public class JedisShardedTemplateTest {
double score2 = 2;
// zadd
assertThat(jedisTemplate.zadd(key, member, score1)).isTrue();
assertThat(jedisTemplate.zadd(key, member2, score2)).isTrue();
assertThat(jedisTemplate.zadd(key, score1, member)).isTrue();
assertThat(jedisTemplate.zadd(key, score2, member2)).isTrue();
// zcard
assertThat(jedisTemplate.zcard(key)).isEqualTo(2);
@@ -149,7 +149,7 @@ public class JedisShardedTemplateTest {
assertThat(jedisTemplate.zrem(key, member2 + "not.exist")).isFalse();
// unique & zscore
assertThat(jedisTemplate.zadd(key, member, score11)).isFalse();
assertThat(jedisTemplate.zadd(key, score11, member)).isFalse();
assertThat(jedisTemplate.zcard(key)).isEqualTo(1);
assertThat(jedisTemplate.zscore(key, member)).isEqualTo(score11);
assertThat(jedisTemplate.zscore(key, member + "not.exist")).isNull();
@@ -14,9 +14,9 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.mockito.Mockito;
import org.springside.modules.nosql.redis.pool.JedisPool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.lordofthejars.nosqlunit.redis.EmbeddedRedis;
import com.lordofthejars.nosqlunit.redis.EmbeddedRedis.EmbeddedRedisRuleBuilder;
@@ -127,7 +127,7 @@ public class JedisTemplateTest {
jedisTemplate.lpush(key, value);
jedisTemplate.lpush(key, value);
assertThat(jedisTemplate.llen(key)).isEqualTo(3);
assertThat(jedisTemplate.lremOne(key, value)).isTrue();
assertThat(jedisTemplate.lremFirst(key, value)).isTrue();
assertThat(jedisTemplate.llen(key)).isEqualTo(2);
assertThat(jedisTemplate.lremAll(key, value)).isTrue();
assertThat(jedisTemplate.llen(key)).isEqualTo(0);
@@ -144,8 +144,8 @@ public class JedisTemplateTest {
double score2 = 2;
// zadd
assertThat(jedisTemplate.zadd(key, member, score1)).isTrue();
assertThat(jedisTemplate.zadd(key, member2, score2)).isTrue();
assertThat(jedisTemplate.zadd(key, score1, member)).isTrue();
assertThat(jedisTemplate.zadd(key, score2, member2)).isTrue();
// zcard
assertThat(jedisTemplate.zcard(key)).isEqualTo(2);
@@ -157,7 +157,7 @@ public class JedisTemplateTest {
assertThat(jedisTemplate.zrem(key, member2 + "not.exist")).isFalse();
// unique & zscore
assertThat(jedisTemplate.zadd(key, member, score11)).isFalse();
assertThat(jedisTemplate.zadd(key, score11, member)).isFalse();
assertThat(jedisTemplate.zcard(key)).isEqualTo(1);
assertThat(jedisTemplate.zscore(key, member)).isEqualTo(score11);
assertThat(jedisTemplate.zscore(key, member + "not.exist")).isNull();