如何使用RedisTemplate存储不同的数据类型

时间:2014-02-28 08:58:38

标签: redis

我正在使用Spring RedisTemplate来处理与Redis相关的操作。我能存储两种数据类型吗?例如,我想存储Key,String以及Key,Integer。如何实现这个目标?

1 个答案:

答案 0 :(得分:0)

您是否阅读过http://docs.spring.io/spring-data/redis/docs/current/reference/html/redis.html

从文档中改变一点,未经测试:

public class Example {

  // inject the actual template 
  @Autowired
  private RedisTemplate<String, String> template;

  // inject the template as ValueOperations
  @Resource(name="redisTemplate")
  private ValueOperations<String, Integer> intOp;

  @Resource(name="redisTemplate")
  private ValueOperations<String, String> stringOp;

  public void sampleKeySetting() {
    intOp.set('my_int_value', 10);
    stringOp.set('my_string_value', 'good!');
  }
}

其他数据结构也有XXXXOperations:检查http://docs.spring.io/spring-data/data-keyvalue/docs/current/api/org/springframework/data/keyvalue/redis/core/RedisOperations.html

相关问题