使用 RedisTemplate 从 Redis 获取设置值

Get Set value from Redis using RedisTemplate

我可以使用 Jedis:

Redis 中检索值
public static void main(String[] args) {
        Jedis jedis = new Jedis(HOST, PORT);
        jedis.connect();
        Set<String> set = jedis.smembers(KEY);
        for (String s : set) {
            System.out.println(s);
        }
        jedis.disconnect();
        jedis.close();
    }

但是当我尝试使用 Spring 的 RedisTemplate 时,我没有得到任何数据。我的数据作为 Set.

存储在 Redis
      // inject the actual template 
      @Autowired
      private RedisTemplate<String, Object> template;

      // inject the template as SetOperations
      @Resource(name="redisTemplate")
      private SetOperations<String,String> setOps;

public String logHome() {       
        Set<String> set =  setOps.members(KEY);
        for(String str:set){
            System.out.println(str); //EMPTY
        }       
        Set<byte[]> keys = template.getConnectionFactory().getConnection().keys("*".getBytes());
        Iterator<byte[]> it = keys.iterator();
        while(it.hasNext()){
            byte[] data = (byte[])it.next();
            System.out.println(new String(data, 0, data.length)); //KEYS are printed.
        }
        Set<Object> mySet = template.boundSetOps(KEY).members();        
        System.out.println(mySet); //EMPTY      
        return "";
    }

有人可以指出我遗漏了什么吗?

编辑:我的 xml RedisTemplate 配置。

 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory"/>

     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="myhostname" p:port="6379" />

简而言之

您必须配置序列化程序。

说明

Redis 模板对键、值和散列使用序列化器keys/values。序列化程序用于将 Java 输入转换为存储在 Redis 中的表示形式。如果你不配置任何东西,序列化器默认为JdkSerializationRedisSerializer。因此,如果您在 Java 代码中请求密钥 key,序列化程序会将其转换为

"\xac\xed\x00\x05t\x00\x03key"

和Spring数据Redis使用这些字节作为查询Redis的键。

您可以使用 Spring Data Redis 添加数据并使用 redis-cli:

查询它
template.boundSetOps("myKey").add(new Date());

然后在 redis-cli

127.0.0.1:6379> keys *
1) "\xac\xed\x00\x05t\x00\x05myKey"
127.0.0.1:6379> SMEMBERS "\xac\xed\x00\x05t\x00\x05myKey"
1) "\xac\xed\x00\x05sr\x00\x0ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\b\x00\x00\x01N\xcf#\x9cHx"

如您所见,字符串和日期被序列化为一些疯狂的字节,代表 Java-序列化对象。

您的代码表明您希望存储基于字符串的键和值。只需在 RedisTemplate

中设置 StringRedisSerializer

Java配置

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());

XML配置

<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"/>

运行 您的代码之后的输出如下:

value
key
[value]

Spring Data Redis 有一些有趣的序列化程序,允许在不同系统之间交换消息。您可以从内置的序列化程序中进行选择

  • JacksonJsonRedisSerializer
  • Jackson2JsonRedisSerializer
  • JdkSerializationRedisSerializer(默认)
  • OxmSerializer
  • GenericToStringSerializer

或创建您自己的。

我用SpringData Redis 1.5.1.RELEASE和jedis 2.6.2验证了你的问题结果。 HTH,马克

进一步阅读:

使用 Redisson 可以更轻松地做到这一点:

public static void main(String[] args) {
    Config conf = new Config();
    conf.useSingleServer().setAddress(redisURL);

    RedissonClient redisson = Redisson.create(conf);
    RSet<String> set = redisson.getSet("key")
    for (String s : set.readAllValues()) {
        System.out.println(s);
    }
    redisson.shutdown();
}

此框架处理序列化并处理连接,因此您无需每次都处理它。像使用 Java 对象(Set、Map、List ...)一样使用 Redis。它也支持许多流行的编解码器。