2017-08-06 92 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。

相關問題