Redis使用lua脚本实现increase + expire 的原子操作
2018-11-02 09:58
1056 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z69183787/article/details/79033670
JedisCluter
阅读更多
lua脚本:
- local current = redis.call('incrBy',KEYS[1],ARGV[1]);" +
- " if current == tonumber(ARGV[1]) then" +
- " local t = redis.call('ttl',KEYS[1]);" +
- " if t == -1 then " +
- " redis.call('expire',KEYS[1],ARGV[2])" +
- " end;" +
- " end;" +
- " return current";
JedisCluter
- public Long incrBy(final String key, final long step, final int defaultExpire) {
- return new JedisClusterCommand<Long>(connectionHandler, maxRedirections, Protocol.Command.INCR) {
- @Override
- public Long execute(Jedis connection) {
- if (defaultExpire > 0) {
- Object val = connection.eval(ScriptConstant.INCRBY_1_EXPIRE, 1, new String[]{key, String.valueOf(step), String.valueOf(defaultExpire)});
- return Long.valueOf(val.toString());
- } else {
- return connection.incrBy(key, step);
- }
- }
- }.run(key);
- }
阅读更多
相关文章推荐
- Redis使用lua脚本实现increase + expire 的原子操作
- 使用lua脚本和jedis实现redis的hmsetnx命令,操作hash表时不覆盖原有数据
- Redis之使用lua脚本操作redis数据库的原因
- redis哨兵模式使用lua脚本实现分布式锁
- redis实现附近的人,但jedis中没有相关api,那么直接使用lua脚本执行。
- 【COCOS2DX-LUA 脚本开发之八】使用Lua实现Http交互
- Cocos2d-x使用Luajit将Lua脚本编译为bytecode,从而实现加密
- java使用jeids实现redis2.6的String操作(4)
- 【COCOS2DX-LUA 脚本开发之十】使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- PHP中使用redis执行lua脚本示例
- Redis系列四 - 在springboot中通过Lua脚本在redis中实现定时任务
- 使用Lua脚本完成高并发下Redis数据修改
- Cocos2d-x使用Luajit将Lua脚本编译为bytecode,从而实现加密
- Cocos2d-x使用Luajit将Lua脚本编译为bytecode,从而实现加密
- 使用ConcurrentMap实现高效可靠的原子操作
- java使用jeids实现redis2.6的脚本执行
- 阿里云redis集群使用lua脚本
- 在Spring中使用Redis Lua脚本批量删除缓存
- Redis 2.6 Lua脚本功能实现分析