我正在使用Jedis通过Spring数据访问Redis。Spring Red Data支持Redis BRPOPLPUSH
<bean
id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig"
p:maxTotal="100"
p:maxIdle="10"
p:maxWaitMillis="5000"
/>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.server.host}" p:port="${redis.server.port}"
p:password="${redis.server.password}"
p:use-pool="${redis.server.usepool}"
p:pool-config-ref="jedisPoolConfig"/>
<bean id="genericJackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"> <property name="keySerializer" ref="stringSerializer"/>
<property name="hashKeySerializer" ref="stringSerializer"/>
<property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"/>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory" />
想执行BRPOPLPUSH
,我没有看到下
OPTION_1 -> getRedisTemplate().boundListOps("Key").*
OPTION_2 -> getRedisTemplate().opsForList().rightPopAndLeftPush("sourceKey", "destinationKey") ---> No blocking ?
相反,它是在该选项,
OPTION_3 -> getRedisTemplate().getConnectionFactory().getConnection().bRPopLPush(timeout, srcKey, dstKey)
Question_0:这是唯一的选项?为什么它在上面的两个API中不可用?
Answer_0:没有,只要TIMEUNIT在boundListOps
提到与left/rightPop(timeout, unit)
,opsForList
它是阻塞调用。用0秒的时间单位阻止永远。
Question_1:为什么有这么多变种..?或可能何时使用boundListOps
,opsForList
和getConnection
?
Partial_Answer_1:看起来,如果多种业务于单个键,然后boundListOps采取行动,因为它绑定到一个键,并没有必要提及的是,在反复的动作,其中在opsForList,每一个动作关键必须提及。
从boundListOps的DOC:
返回上绑定到给定 键列表值执行的操作。
请注意,boundListOps仍然通过将注册密钥传递给它来固有地使用opsForList。
好的,我仍然应该什么时候直接使用getConnectionFactory().getConnection()
?
Question_2:如果我使用getConnectionFactory().getConnection()
,我是否需要关闭()finally(){}
在这方面? (因为这是目前支持blocking RPopLPush
的那个)。
从代码boundListOps
本质上是使用opsForList,它的所有行动最终像下面与释放连接执行,因此问。
RedisTemplate.java
public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {
RedisConnectionFactory factory = getConnectionFactory();
RedisConnection conn = null;
try {
conn = RedisConnectionUtils.getConnection(factory);
...
...
// TODO: any other connection processing?
return postProcessResult(result, connToUse, existingConnection);
} finally {
RedisConnectionUtils.releaseConnection(conn, factory);
}
Question_3:为什么getRedisTemplate().boundListOps("key").rightPush(new HashMap());
在接受Map
,而不是像String/Object
虽然"key"
值已经提到...?
Answer_3:这是我的问题,我已经这么声明过了。
public RedisTemplate<String, Map<String, Object>> getRedisTemplate() {
return redisTemplate;
}
同样的RedisTemplate对象在获取DefaultBoundListOperations时从RedisTemplate引用。
public BoundListOperations<K, V> boundListOps(K key) {
return new DefaultBoundListOperations<K, V>(key, this);
}
class DefaultBoundListOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundListOperations<K, V> {
public DefaultBoundListOperations(K key, RedisOperations<K, V> operations) {
super(key, operations); //RedisOperations<K, V> is converted to BoundListOperations<K, V> here
this.ops = operations.opsForList();
}
}
Question_4:为什么getConnection().bRPopLPush
被接受srcKey, dstKey
为byte[]
而不是String
..?
对不起,很多问题都引发了,因为我没有在spring数据中找到正确的java文档,或者教程来解释这些用法。