java redis执行lua_Springboot整合Redis以及Lua脚本的使用

packagecom.zxy.demo.redis;importjava.util.List;importjava.util.Map;importjava.util.Set;importjava.util.concurrent.TimeUnit;importorg.springframework.beans.factory.annotation.Autowired;importorg.spring...
摘要由CSDN通过智能技术生成

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.zxy.demo.redis;importjava.util.List;importjava.util.Map;importjava.util.Set;importjava.util.concurrent.TimeUnit;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.stereotype.Component;importorg.springframework.util.CollectionUtils;/*** Redis工具类

*@authorZENG.XIAO.YAN

* @date 2018年6月7日*/@Componentpublic final classRedisUtil {

@Autowiredprivate RedisTemplateredisTemplate;//=============================common============================

/*** 指定缓存失效时间

*@paramkey 键

*@paramtime 时间(秒)

*@return

*/

public boolean expire(String key, longtime) {try{if (time > 0) {

redisTemplate.expire(key, time, TimeUnit.SECONDS);

}return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 根据key 获取过期时间

*@paramkey 键 不能为null

*@return时间(秒) 返回0代表为永久有效*/

public longgetExpire(String key) {returnredisTemplate.getExpire(key, TimeUnit.SECONDS);

}/*** 判断key是否存在

*@paramkey 键

*@returntrue 存在 false不存在*/

public booleanhasKey(String key) {try{returnredisTemplate.hasKey(key);

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 删除缓存

*@paramkey 可以传一个值 或多个*/@SuppressWarnings("unchecked")public voiddel(String... key) {if (key != null && key.length > 0) {if (key.length == 1) {

redisTemplate.delete(key[0]);

}else{

redisTemplate.delete(CollectionUtils.arrayToList(key));

}

}

}//============================String=============================

/*** 普通缓存获取

*@paramkey 键

*@return值*/

publicObject get(String key) {return key == null ? null: redisTemplate.opsForValue().get(key);

}/*** 普通缓存放入

*@paramkey 键

*@paramvalue 值

*@returntrue成功 false失败*/

public booleanset(String key, Object value) {try{

redisTemplate.opsForValue().set(key, value);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 普通缓存放入并设置时间

*@paramkey 键

*@paramvalue 值

*@paramtime 时间(秒) time要大于0 如果time小于等于0 将设置无限期

*@returntrue成功 false 失败*/

public boolean set(String key, Object value, longtime) {try{if (time > 0) {

redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);

}else{

set(key, value);

}return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 递增

*@paramkey 键

*@paramdelta 要增加几(大于0)

*@return

*/

public long incr(String key, longdelta) {if (delta < 0) {throw new RuntimeException("递增因子必须大于0");

}returnredisTemplate.opsForValue().increment(key, delta);

}/*** 递减

*@paramkey 键

*@paramdelta 要减少几(小于0)

*@return

*/

public long decr(String key, longdelta) {if (delta < 0) {throw new RuntimeException("递减因子必须大于0");

}return redisTemplate.opsForValue().increment(key, -delta);

}//================================Map=================================

/*** HashGet

*@paramkey 键 不能为null</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值