RedisTemplate操作String及Hash数据

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

RedisTemplate详细用法

封装自己的操作方法

  1. 单个key的删除我们可以是封装自己的一个delete方法然后将参数设置为key在通过redisTemplate调用delete方法删除

    redisTemplate.delete(key)

  2. 多个key的删除多个key删除则和上面的单key一样只不过是在参数上设置为多个key的方式即可

    redisTemplate.delete(keys)

    这里的keys是指的多参数public void deleteByKey(String ...keys)

  3. 指定key失效时间设置失效时间我们自己定义的方法设置三个参数分比为key时间单位《秒或分》

    redisTemplate.expir(key,time,TimeUnit.MINUTES)

  4. 根据key值获取过期的时间我们自己设置key参数然后通key获取过期时间

    redisTemplate.getExpire(key)

  5. 判断key是否已经存在经常会用到的key是否存在则和上面的方法类似只是设置个key参数值

    redisTemplate.hasKey(key)

String 类型的操作

  1. 添加缓存
// 通过redisTemplate设置值
redisTemplate.boundValueOps("StringKey").set("StringValue");
redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);


//通过BoundValueOperations设置值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringVaule");
stringKey.set("StringValue",1, TimeUnit.SECOND);

//通过ValueOperations设置值
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
ops.set("StringValue","StringVaule",1, TimeUnit.SECOND);
  1. 删除缓存key

Boolean i = redisTemplate.delete(key)

  1. 顺序递增

redisTemplate.boundValueOps("key").increment(4L)

  1. 顺序递减 redisTemplate.boundValueOps("key").increment(-4L)

Hash 类型数据相关操作

  1. 添加我们的缓存数据
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");

BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashVaue");

HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashVaue");
  1. 设置过期的时间
redisTemplate.boundValueOps("HashKey").expire(1,TimeUnit.SECOND);
redisTemplate.expire("HashKey",1,TimeUnit.SECOND);
  1. 添加一个Map类型的数据
HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );
  1. 提取所有的的小key值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();

BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();

HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");

今天关于RedisTemplate操作String 及Hash的文章就讲解到这欢迎大家留言交流也欢迎大家关注我的工种昊《coder练习生》

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: redis