我正在學習Redis的Spring Data,但直到現在我還沒有找到任何有關如何使用該項目支持的序列化器的示例?如何使用Spring Data for Redis串行器?
我已閱讀該項目的參考文檔(http://static.springsource.org/spring-data/data-redis/docs/current/reference/html/redis.html#redis:serializer)的第4.6節,但它基本上只是說它存在。而已。我怎樣才能使用這個功能?
我正在學習Redis的Spring Data,但直到現在我還沒有找到任何有關如何使用該項目支持的序列化器的示例?如何使用Spring Data for Redis串行器?
我已閱讀該項目的參考文檔(http://static.springsource.org/spring-data/data-redis/docs/current/reference/html/redis.html#redis:serializer)的第4.6節,但它基本上只是說它存在。而已。我怎樣才能使用這個功能?
序列化器用於代碼庫中的一些地方,最顯着的是在RedisTemplate中將存儲在Redis中的原始字節作爲鍵/值轉換爲您的自定義數據類型(反之亦然)。這在文檔的第4.4節中提到。
在你Spring配置
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory">
<property name="keySerializer" ref="stringSerializer"/>
<property name="valueSerializer" ref="stringSerializer"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="myhostname" p:port="6379"/>
或者,如果你想將其設置爲的Java
// inject the actual template
@Autowired
private RedisTemplate<String, Object> template;
...
...
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());