redis常用命令getex_redis实现 msetex和 getdel命令

这是一个关于如何在Java中扩展Redis客户端,实现msetex(设置多个键值对并指定过期时间)和getdel(获取键的值并同时删除键)功能的博客。通过使用Lua脚本,提高了命令执行的效率和原子性。博客详细介绍了这两个自定义命令的实现过程,包括Lua脚本的内容和Java代码的解析。
摘要由CSDN通过智能技术生成

classRedisExtend {private static final Logger logger = LoggerFactory.getLogger(RedisExtend.class);private static final int Port = 6379;private static final String Host = "192.168.1.1";private static final String PASS_WORD = "1234";private staticJedis instance;/*** Lua脚本(msetex)*/

private static final String LUA_SCRIPT_MSETEX = "local keysLen = table.getn(KEYS);" +

"local argvLen = table.getn(ARGV);" +

"local idx=1;" +

"local argVIdx=1;" +

"for idx=1,keysLen,1 do " +

"argVIdx=(idx-1)*2+1; " +

"redis.call('Set',KEYS[idx],ARGV[argVIdx],'EX',ARGV[argVIdx+1]);" +

"end " +

"return keysLen;";private staticString LUA_SCRIPT_MSETEX_SHA1;/*** Lua脚本 (获取后删除)*/

private static final String LUA_SCRIPT_GET_AND_DELETE =

"local current = redis.call('get', KEYS[1]);\n" +

"if (current) then\n" +

" redis.call('del', KEYS[1]);\n" +

"end\n" +

"return current;";private staticString LUA_SCRIPT_GET_AND_DELETE_SHA1;static{

LUA_SCRIPT_MSETEX_SHA1=SHA1.encode(LUA_SCRIPT_MSETEX);

LUA_SCRIPT_GET_AND_DELETE_SHA1=SHA1.encode(LUA_SCRIPT_GET_AND_DELETE);

}public staticJedis getInstance() {if (instance == null) {

instance=initJedisLite().getTemplate().getJedisPool().getResource();

}returninstance;

}private staticJedisLite initJedisLite() {return newJedisLite(Host, Port);

}private static JedisTemplate jedisTemplate =initJedisLite().getTemplate();public static long msetex(ListredisKeyValues) {if(CollectionUtils.isEmpty(redisKeyValues)) {return 0;

}int keyCount =redisKeyValues.size();

List param = new ArrayList<>(keyCount * 3);for(RedisKeyValue item : redisKeyValues) {

Assert.notNull(item,"KeyValue不允许为空");

Assert.hasLength(item.getKey(),"Key不允许为空");

param.add(item.getKey());

}for(RedisKeyValue item : redisKeyValues) {

param.add(item.getValue());

param.add(Integer.toString(item.getSeconds()));

}

String[] paramArr= newString[param.size()];

param.toArray(paramArr);return execLunaScript(newRedisScript(LUA_SCRIPT_MSETEX, LUA_SCRIPT_MSETEX_SHA1), keyCount, paramArr,

(o)-> (long) (o == null ? 0L: Integer.parseInt(o.toString())));

}public staticString getdel(String key) {return execLunaScript(new RedisScript(LUA_SCRIPT_GET_AND_DELETE, LUA_SCRIPT_GET_AND_DELETE_SHA1), 1, newString[]{key},

(o)-> (o == null ? null: o.toString()));

}private static T execLunaScript(RedisScript redisScriptObj, intkeyCount, String[] param,

Functionfunction) {try{return jedisTemplate.execute((Jedis jedis) ->function.apply(jedis.evalsha(redisScriptObj.sha1, keyCount,

param)));

}catch(redis.clients.jedis.exceptions.JedisNoScriptException ex) {return jedisTemplate.execute((Jedis jedis) ->function.apply(jedis.eval(redisScriptObj.script, keyCount,

param)));

}catch(Exception ex) {

logger.error("执行redis脚本异常!", ex);return null;

}

}static classRedisScript {privateString script;privateString sha1;publicRedisScript(String script) {this(script, SHA1.encode(script));

}publicRedisScript(String script, String sha1) {this.script =script;this.sha1 =sha1;

}

}static classRedisKeyValue {privateString key;privateString value;private intseconds;publicRedisKeyValue(String key, String value) {this(key, value, -1);

}public RedisKeyValue(String key, String value, intseconds) {this.key =key;this.value =value;this.seconds =seconds;

}publicString getKey() {returnkey;

}publicString getValue() {returnvalue;

}public intgetSeconds() {returnseconds;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值