2017-08-06 95 views
1

我正在使用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,opsForListgetConnection

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, dstKeybyte[]而不是String ..?

对不起,很多问题都引发了,因为我没有在spring数据中找到正确的java文档,或者教程来解释这些用法。

回答

0

关于问题4:首先,重要的是要注意Redis键和值can be any binary。 Spring-data模板将这个抽象出来,让开发人员处理Java对象而不是字节数组。请参阅以下spring-data reference的报价:

...该模板为Redis交互提供了高级抽象。虽然RedisConnection提供接受和返回二进制值(字节数组)的低级别方法,但该模板负责序列化和连接管理,使用户无需处理这些细节。

似乎与RedisConnection工作时,春不知道什么类型的键或值是,它没有努力转换的对象(例如,String)二进制Redis的理解,因此,您需要将原始二进制文件传递给Redis。

相关问题