使用 RedisTemplate 从 Redis 获取 Set 值总之解释
我能够从使用中检索值:Redis
Jedis
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's时,我没有得到任何数据。我的数据存储在 .RedisTemplate
Redis
Set
// 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 "";
}
有人可以向我指出我错过了什么吗?
编辑:我的 RedisTemplate 的 xml 配置。
<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" />