SpringBoot 中使用RedisTemplate操作Redis(一)String数据结构

      Redis可以存储5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合)。

结构类型 结构存储的值 结构的读写能力
String 可以是字符串、整数或者浮点数 对整个字符串或者字符串的其中一部分执行操作;对象和浮点数执行自增(increment)或者自减(decrement)
List 一个链表,链表上的每个节点都包含了一个字符串 从链表的两端推入或者弹出元素;根据偏移量对链表进行修剪(trim);读取单个或者多个元素;根据值来查找或者移除元素
Set 包含字符串的无序收集器(unorderedcollection),并且被包含的每个字符串都是独一无二的、各不相同 添加、获取、移除单个元素;检查一个元素是否存在于某个集合中;计算交集、并集、差集;从集合里随机获取元素
Hash 包含键值对的无序散列表 添加、获取、移除单个键值对;获取所有键值对
Zset 字符串成员(member)与浮点数分值(score)之间的有序映射,元素的排列顺序由分值的大小决定 添加、获取、删除单个元素;根据分值范围(range)或者成员来获取元素

SpringBoot redis中的配置使用见:

https://blog.csdn.net/u011051912/article/details/74923918

RedisTemplate中定义了对5种数据结构操作:

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

Redis的String数据结构 

set void set(K key, V value);

使用:redisTemplate.opsForValue().set("key","value");
结果:redisTemplate.opsForValue().get("key")  输出结果为value

set void set(K key, V value, long timeout, TimeUnit unit);

使用:redisTemplate.opsForValue().set("key","value",10, TimeUnit.SECONDS);
结果:redisTemplate.opsForValue().get("key")由于设置的是10秒失效,十秒之内查询有结果,十秒之后返回为null

set void set(K key, V value, long offset);

该方法是用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始;

使用:template.opsForValue().set("key","hello world");
      template.opsForValue().set("key","redis", 6);
      System.out.println("***************"+template.opsForValue().get("key"));
结果:***************hello redis

setIfAbsent Boolean setIfAbsent(K key, V value);

使用:System.out.println(template.opsForValue().setIfAbsent("multi1","multi1"));//false  multi1之前已经存在
        System.out.println(template.opsForValue().setIfAbsent("multi111","multi111"));//true  multi111之前不存在
结果:false
true

multiSet void multiSet(Map<? extends K, ? extends V> m);
为多个键分别设置它们的值:

使用:Map<String,String> maps = new HashMap<String, String>();
      maps.put("multi1","multi1");
      maps.put("multi2","multi2");
      maps.put("multi3","multi3");
      template.opsForValue().multiSet(maps);
      List<String> keys = new ArrayList<String>();
      keys.add("multi1");
      keys.add("multi2");
      keys.add("multi3");
      System.out.println(template.opsForValue().multiGet(keys));
结果:[multi1, multi2, multi3]

multiSetIfAbsent Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m);
为多个键分别设置它们的值,如果存在则返回false,不存在返回true

使用:Map<String,String> maps = new HashMap<String, String>();
      maps.put("multi11","multi11");
      maps.put("multi22","multi22");
      maps.put("multi33","multi33");
      Map<String,String> maps2 = new HashMap<String, String>();
      maps2.put("multi1","multi1");
      maps2.put("multi2","multi2");
      maps2.put("multi3","multi3");
      System.out.println(template.opsForValue().multiSetIfAbsent(maps));
      System.out.println(template.opsForValue().multiSetIfAbsent(maps2));
结果:true
false

get V get(Object key);

使用:template.opsForValue().set("key","hello world");
      System.out.println("***************"+template.opsForValue().get("key"));
结果:***************hello world

getAndSet V getAndSet(K key, V value);
设置键的字符串值并返回其旧值:

使用:template.opsForValue().set("getSetTest","test");
      System.out.println(template.opsForValue().getAndSet("getSetTest","test2"));
结果:test

multiGet List<V> multiGet(Collection<K> keys);
为多个键分别取出它们的值:

使用:Map<String,String> maps = new HashMap<String, String>();
      maps.put("multi1","multi1");
      maps.put("multi2","multi2");
      maps.put("multi3","multi3");
      template.opsForValue().multiSet(maps);
      List<String> keys = new ArrayList<String>();
      keys.add("multi1");
      keys.add("multi2");
      keys.add("multi3");
      System.out.println(template.opsForValue().multiGet(keys));
结果:[multi1, multi2, multi3]

increment Long increment(K key, long delta);
支持整数:

使用:template.opsForValue().increment("increlong",1);
      System.out.println("***************"+template.opsForValue().get("increlong"));
结果:***************1

increment Double increment(K key, double delta);
也支持浮点数:

使用:template.opsForValue().increment("increlong",1.2);
      System.out.println("***************"+template.opsForValue().get("increlong"));
结果:***************2.2

append Integer append(K key, String value);
如果key已经存在并且是一个字符串,则该命令将该值追加到字符串的末尾。如果键不存在,则它被创建并设置为空字符串,因此APPEND在这种特殊情况下将类似于SET。

使用:template.opsForValue().append("appendTest","Hello");
      System.out.println(template.opsForValue().get("appendTest"));
      template.opsForValue().append("appendTest","world");
      System.out.println(template.opsForValue().get("appendTest"));
结果:Hello
      Helloworld

get String get(K key, long start, long end);
截取key所对应的value字符串:

使用:appendTest对应的value为Helloworld
System.out.println("*********"+template.opsForValue().get("appendTest",0,5));
结果:*********Hellow
使用:System.out.println("*********"+template.opsForValue().get("appendTest",0,-1));
结果:*********Helloworld
使用:System.out.println("*********"+template.opsForValue().get("appendTest",-3,-1));
结果:*********rld

size Long size(K key);
返回key所对应的value值得长度:

使用:template.opsForValue().set("key","hello world");
  System.out.println("***************"+template.opsForValue().size("key"));
结果:***************11

setBit Boolean setBit(K key, long offset, boolean value);
对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)
key键对应的值value对应的ascii码,在offset的位置(从左向右数)变为value:

使用:template.opsForValue().set("bitTest","a");
      // 'a' 的ASCII码是 97。转换为二进制是:01100001
      // 'b' 的ASCII码是 98  转换为二进制是:01100010
      // 'c' 的ASCII码是 99  转换为二进制是:01100011
      //因为二进制只有0和1,在setbit中true为1,false为0,因此我要变为'b'的话第六位设置为1,第七位设置为0
      template.opsForValue().setBit("bitTest",6, true);
      template.opsForValue().setBit("bitTest",7, false);
      System.out.println(template.opsForValue().get("bitTest"));
结果:b

getBit Boolean getBit(K key, long offset);
获取键对应值的ascii码的在offset处位值:

使用:System.out.println(template.opsForValue().getBit("bitTest",7));
结果:false

 

发布了48 篇原创文章 · 获赞 60 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/u011051912/article/details/81186470