Java Code Examples for redis.clients.jedis.Jedis#eval()
The following examples show how to use
redis.clients.jedis.Jedis#eval() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisLimit.java From seconds-kill with MIT License | 6 votes |
/** * Redis 限流 */ public static Boolean limit() { Jedis jedis = null; Object result = null; try { // 获取 jedis 实例 jedis = RedisPool.getJedis(); // 解析 Lua 文件 String script = ScriptUtil.getScript("limit.lua"); // 请求限流 String key = String.valueOf(System.currentTimeMillis() / 1000); // 计数限流 result = jedis.eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit))); if (FAIL_CODE != (Long) result) { log.info("成功获取令牌"); return true; } } catch (Exception e) { log.error("limit 获取 Jedis 实例失败:", e); } finally { RedisPool.jedisPoolClose(jedis); } return false; }
Example 2
Source File: RedisLockInternals.java From Mykit with Apache License 2.0 | 6 votes |
private String createRedisKey(String lockId) { Jedis jedis = null; //boolean broken = false; try { String value=lockId+randomId(1); jedis = jedisPool.getResource(); String luaScript = "" + "\nlocal r = tonumber(redis.call('SETNX', KEYS[1],ARGV[1]));" + "\nredis.call('PEXPIRE',KEYS[1],ARGV[2]);" + "\nreturn r"; List<String> keys = new ArrayList<String>(); keys.add(lockId); List<String> args = new ArrayList<String>(); args.add(value); args.add(lockTimeout+""); Long ret = (Long) jedis.eval(luaScript, keys, args); if( new Long(1).equals(ret)){ return value; } }finally { if(jedis!=null) jedis.close(); } return null; }
Example 3
Source File: RedisLockInternals.java From Mykit with Apache License 2.0 | 6 votes |
void unlockRedisLock(String key,String value) { Jedis jedis = null; //boolean broken = false; try { jedis = jedisPool.getResource(); String luaScript="" +"\nlocal v = redis.call('GET', KEYS[1]);" +"\nlocal r= 0;" +"\nif v == ARGV[1] then" +"\nr =redis.call('DEL',KEYS[1]);" +"\nend" +"\nreturn r"; List<String> keys = new ArrayList<String>(); keys.add(key); List<String> args = new ArrayList<String>(); args.add(value); /*Object r=*/jedis.eval(luaScript, keys, args); } finally { if(jedis!=null) jedis.close(); } }
Example 4
Source File: RedisLockInternals.java From Distributed-Kit with Apache License 2.0 | 6 votes |
void unlockRedisLock(String key,String value) { Jedis jedis = null; boolean broken = false; try { jedis = jedisPool.getResource(); String luaScript="" +"\nlocal v = redis.call('GET', KEYS[1]);" +"\nlocal r= 0;" +"\nif v == ARGV[1] then" +"\nr =redis.call('DEL',KEYS[1]);" +"\nend" +"\nreturn r"; List<String> keys = new ArrayList<String>(); keys.add(key); List<String> args = new ArrayList<String>(); args.add(value); Object r=jedis.eval(luaScript, keys, args); } finally { if(jedis!=null) jedis.close(); } }
Example 5
Source File: RedisLockInternals.java From Distributed-Kit with Apache License 2.0 | 6 votes |
private String createRedisKey(String lockId) { Jedis jedis = null; boolean broken = false; try { String value=lockId+randomId(1); jedis = jedisPool.getResource(); String luaScript = "" + "\nlocal r = tonumber(redis.call('SETNX', KEYS[1],ARGV[1]));" + "\nredis.call('PEXPIRE',KEYS[1],ARGV[2]);" + "\nreturn r"; List<String> keys = new ArrayList<String>(); keys.add(lockId); List<String> args = new ArrayList<String>(); args.add(value); args.add(lockTimeout+""); Long ret = (Long) jedis.eval(luaScript, keys, args); if( new Long(1).equals(ret)){ return value; } }finally { if(jedis!=null) jedis.close(); } return null; }
Example 6
Source File: RedisRateLimiter.java From redislimiter-spring-boot with Apache License 2.0 | 6 votes |
private boolean doPeriod(Jedis jedis, String keyPrefix, int permitsPerUnit) { List<String> jedisTime = jedis.time(); long currentSecond = Long.parseLong(jedisTime.get(0)); long microSecondsElapseInCurrentSecond = Long.parseLong(jedisTime.get(1)); String[] keyNames = getKeyNames(currentSecond, keyPrefix); //因为redis访问实际上是单线程的,而且jedis.time()方法返回的时间精度为微秒级,每一个jedis.time()调用耗时应该会超过1微秒,因此我们可以认为每次jedis.time()返回的时间都是唯一且递增 //因此这个currentTimeInMicroSecond在多线程情况下不会存在相同 long currentTimeInMicroSecond = currentSecond * 1000000 + microSecondsElapseInCurrentSecond; String previousSectionBeginScore = String.valueOf((currentTimeInMicroSecond - getPeriodMicrosecond())); String expires =String.valueOf(getExpire()); String currentTimeInMicroSecondStr = String.valueOf(currentTimeInMicroSecond); List<String> keys = new ArrayList<String>(); keys.add(keyNames[0]); keys.add(keyNames[1]); List<String> argvs = new ArrayList<String>(); argvs.add(currentTimeInMicroSecondStr); argvs.add(currentTimeInMicroSecondStr); argvs.add(previousSectionBeginScore); argvs.add(expires); argvs.add(String.valueOf(permitsPerUnit)); Long val = (Long)jedis.eval(LUA_PERIOD_SCRIPT, keys, argvs); return (val > 0); }
Example 7
Source File: SmoothRateLimiter.java From redis-limiter with Apache License 2.0 | 6 votes |
@Override long queryWaitMicros(int permits, Long timeoutMicros) { List<String> keys = Arrays.asList(key, String.valueOf(maxPermits), String.valueOf(permitsPerSecond)); List<String> args = new ArrayList<>(); args.add(String.valueOf(permits)); if (timeoutMicros != null) { args.add(String.valueOf(timeoutMicros)); } try { Jedis redis = getJedis(); Object result = redis.eval(script, keys, args); return (long) result; } catch (Exception e) { e.printStackTrace(); } return 0; }
Example 8
Source File: DistributedLock.java From MicroCommunity with Apache License 2.0 | 6 votes |
/** * 释放分布式锁 * * @param lockKey 锁 * @param requestId 请求标识 * @return 是否释放成功 */ public static boolean releaseDistributedLock(String lockKey, String requestId) { Jedis redis = null; try { redis = getJedis(); String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Object result = redis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId)); if (RELEASE_SUCCESS.equals(result)) { return true; } return false; } finally { if (redis != null) { redis.close(); } } }
Example 9
Source File: CounterRateLimiter.java From redis-limiter with Apache License 2.0 | 5 votes |
public Boolean acquire(int permits) throws Exception { Jedis redis = getJedis(); Object result = redis.eval(script, Arrays.asList(key, String.valueOf(maxPermits), String.valueOf(intervalMilliseconds)), Arrays.asList(String.valueOf(permits)) ); if (result != null && 0 != (Long) result) { return true; } else { return false; } }
Example 10
Source File: CloudDB.java From appinventor-extensions with Apache License 2.0 | 5 votes |
public Object jEval(String script, String scriptsha1, int argcount, String... args) throws JedisException { Jedis jedis = getJedis(); try { return jedis.evalsha(scriptsha1, argcount, args); } catch (JedisNoScriptException e) { if (DEBUG) { Log.d(LOG_TAG, "Got a JedisNoScriptException for " + scriptsha1); } // This happens if the server doesn't have the script loaded // So we use regular eval, which should then cache the script return jedis.eval(script, argcount, args); } }
Example 11
Source File: JedisClient.java From dlock with Apache License 2.0 | 5 votes |
/** * Eval lua script command * * @param script * @param keys * @param args * @return */ public Object eval(String script, List<String> keys, List<String> args) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.eval(script, keys, args); } finally { if (jedis != null) { jedis.close(); } } }
Example 12
Source File: UnLockTest.java From code with Apache License 2.0 | 5 votes |
/** * lua脚本释放分布式锁 * @param jedis Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @return 是否释放成功 */ public static boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) { // 首先获取锁对应的value值,检查是否与requestId相等,如果相等则删除锁(解锁) String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId)); if (RELEASE_SUCCESS.equals(result)) { return true; } return false; }
Example 13
Source File: RedisLimiter.java From blog with BSD 2-Clause "Simplified" License | 5 votes |
public boolean acquire() { String key = String.valueOf(System.currentTimeMillis() / 1000); Jedis jedis = pool.getResource(); try { Object result = jedis.eval(script, Lists.newArrayList(key), Lists.newArrayList(limit)); if ((Long) result != 0) { return true; } } catch (Exception e) { System.err.println(e.getStackTrace()); } finally { jedis.close(); } return false; }
Example 14
Source File: RedisRateLimiter.java From redislimiter-spring-boot with Apache License 2.0 | 5 votes |
public boolean acquire(String keyPrefix, int permitsPerUnit){ boolean rtv = false; if (jedisPool != null) { Jedis jedis = null; try { jedis = jedisPool.getResource(); if (timeUnit == TimeUnit.SECONDS) { String keyName = getKeyNameForSecond(jedis, keyPrefix); List<String> keys = new ArrayList<String>(); keys.add(keyName); List<String> argvs = new ArrayList<String>(); argvs.add(String.valueOf(getExpire())); argvs.add(String.valueOf(permitsPerUnit)); Long val = (Long)jedis.eval(LUA_SECOND_SCRIPT, keys, argvs); rtv = (val > 0); } else if (timeUnit == TimeUnit.MINUTES || timeUnit == TimeUnit.HOURS || timeUnit == TimeUnit.DAYS) { rtv = doPeriod(jedis, keyPrefix, permitsPerUnit); } } finally { if (jedis != null) { jedis.close(); } } } return rtv; }
Example 15
Source File: RedisLock.java From distributed-lock-redis with MIT License | 5 votes |
public boolean unlock(String key, String value) { Jedis jedis = jedisPool.getResource(); String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Object result = jedis.eval(script, Collections.singletonList(LOCK_PREFIX + key), Collections.singletonList(value)); jedis.close(); return UNLOCK_MSG.equals(result); }
Example 16
Source File: RedisService.java From halyard with Apache License 2.0 | 4 votes |
public static void flushKeySpace(Jedis jedis, String pattern) { jedis.eval( "for i, k in ipairs(redis.call('keys', '" + pattern + "')) do redis.call('del', k); end"); }
Example 17
Source File: DefaultRedis.java From craft-atom with MIT License | 4 votes |
private Object eval0(Jedis j, String script, List<String> keys, List<String> args) { return j.eval(script, keys, args); }
Example 18
Source File: RedisLockUtil.java From springboot-learn with MIT License | 2 votes |
/** * 释放分布式锁 * * @param jedis Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @return 是否释放成功 */ public static boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) { String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId)); return RELEASE_SUCCESS.equals(result); }