Spring中使用RedisTemplate操作Redis(spring(2)

RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer );
        template.setValueSerializer(stringSerializer );
        template.setHashKeySerializer(stringSerializer );
        template.setHashValueSerializer(stringSerializer );

public interface ValueOperations<K,V>
Redis operations for simple (or in Redis terminology 'string') values.
ValueOperations可以对String数据结构进行操作:

•set void set(K key, V value);

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

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

使用:redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
结果:redisTemplate.opsForValue().get("name")由于设置的是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);
也支持浮点数

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8f96c330d6318f32beb9e4557005b934.html