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;
}
}
}