zoukankan      html  css  js  c++  java
  • 操作Redis

    公司用的是redisTemplate

    redisTemplate 实现了RedisOperations接口,是该接口的实现类

    根据redis不同的数据类型:String,List,ZSet,set分别对应ValueOperations、ListOperations、SetOperations、ZSetOperations几个接口

    具体实现类是DefaultValueOperations、DefaultListOperations、DefaultSetOperations、DefaultZSetOperations

    redisTemplate操作不同数据结构:

    redisTemplate.opsForValue();//操作字符串
    redisTemplate.opsForHash();//操作hash
    redisTemplate.opsForList();//操作list
    redisTemplate.opsForSet();//操作set
    redisTemplate.opsForZSet();//操作有序set

    举例:如果数据结构为String类型既可以用 redisTemplate,也可以用ValueOperations来操作

    在RedisTemplate中提供了几个常用的接口方法的使用,分别是:

    private ValueOperations<K, V> valueOps;
    private ListOperations<K, V> listOps;
    private SetOperations<K, V> setOps;
    private ZSetOperations<K, V> zSetOps;
    

      

    RedisOperations

    这个接口的实现类就是RedisTemplate,提供了一些对Redis命令的一些操作。

    ValueOperations
    这个接口的实现类为:DefaultValueOperations.
    在RedisTemplate中,已经提供了一个工厂方法:opsForValue()。这个方法会返回一个默认的操作类。另外,我们可以直接通过注解@Resource(name = “redisTemplate”)来进行注入。

    //声明
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> template;
    
    //调用方法
    template.opsForValue().set("key","value");
    //RedisTemplate还提供了对应的*OperationsEditor,用来通过RedisTemplate直接注入对应的Operation。
    //声明
    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> vOps;
    
    //调用方法
    vOps.set("key","value");
    

      

    除了可以通过template注入ValueOperations,还可以注入 上面的其他几种operations以及HashOperations

    DefaultValueOperations提供了所有Redis字符串类型的操作api。比如set,get,incr等等。使用这些方法,可以方便的直接存储任意的java类型,而不需要自己去将存储的东西序列化以及反序列化

    ListOperations,SetOperations,ZSetOperations除了提供的操作API不一样以外,其他的调用方法与DefaultValueOperations一致。

    HashOperations接口说明

    这个接口并没有定义成员变量,但是直接提供了方法。

    public <HK, HV> HashOperations<K, HK, HV> opsForHash() {
        return new DefaultHashOperations<K, HK, HV>(this);
    }

    具体的调用如下: 
    方法1:

    //注入HashOperations对象
    @Resource(name = "redisTemplate")
    private HashOperations<String,String,Object> hashOps;
    
    //具体调用
    Map<String,String> map = new HashMap<String, String>();
    map.put("value","code");
    map.put("key","keyValue");
    hashOps.putAll("hashOps",map);
    

      

    //注入RedisTemplate对象
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> template;
    
    
    //具体调用
    Map<String,String> map = new HashMap<String, String>();
    map.put("value","code");
    map.put("key","keyValue");
    template.opsForHash().putAll("hashOps",map);
    

      

    引用自:https://blog.csdn.net/whatlookingfor/article/details/51863286

  • 相关阅读:
    初试Shell脚本
    iOS分类Category探索
    cocoaPods安装爬坑总结
    关于FFmpeg工具的使用总结
    关于Boost在工程下的配置
    关于Phabricator Arcanist以及提交项目代码
    关于visual studio的一些日常总结
    关于Python在Linux、Mac和Windows上的安装方法总结
    TextSwitcher 文本切换器的功能与用法
    Android必知必会-App 常用图标尺寸规范汇总
  • 原文地址:https://www.cnblogs.com/mmh760/p/10856928.html
Copyright © 2011-2022 走看看