【问题标题】:How to use RedisTemplate to store different datatypes如何使用 RedisTemplate 存储不同的数据类型
【发布时间】:2014-04-01 04:14:42
【问题描述】:

我正在使用 Spring RedisTemplate 来处理与 Redis 相关的操作。我可以存储两种数据类型吗?例如,我想存储 Key、String 以及 Key、Integer。如何做到这一点?

【问题讨论】:

    标签: redis


    【解决方案1】:

    你读过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

    【讨论】:

      猜你喜欢
      • 2013-04-05
      • 2014-05-15
      • 1970-01-01
      • 2019-09-03
      • 2015-06-28
      • 2020-02-04
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多