Java Code Examples for redis.clients.jedis.Jedis#getSet()
The following examples show how to use
redis.clients.jedis.Jedis#getSet() .
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: LockTest.java From code with Apache License 2.0 | 6 votes |
public static boolean wrongGetLock2(Jedis jedis, String lockKey, int expireTime) { long expires = System.currentTimeMillis() + expireTime; String expiresStr = String.valueOf(expires); // 如果当前锁不存在,返回加锁成功 if (jedis.setnx(lockKey, expiresStr) == 1) { return true; } // 如果锁存在,获取锁的过期时间 String currentValueStr = jedis.get(lockKey); if (currentValueStr != null && Long.parseLong(currentValueStr) < System.currentTimeMillis()) { // 锁已过期,获取上一个锁的过期时间,并设置现在锁的过期时间 String oldValueStr = jedis.getSet(lockKey, expiresStr); if (oldValueStr != null && oldValueStr.equals(currentValueStr)) { // 考虑多线程并发的情况,只有一个线程的设置值和当前值相同,它才有权利加锁 return true; } } // 其他情况,一律返回加锁失败 return false; }
Example 2
Source File: JedisUtils.java From fw-spring-cloud with Apache License 2.0 | 5 votes |
/** * <p> * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。 * </p> * <p> * 当 key 存在但不是字符串类型时,返回一个错误。 * </p> * * @param key * @param value * @return 返回给定 key 的旧值。当 key 没有旧值时,也即是, key 不存在时,返回 nil */ public String getSet(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.getSet(key, value); } catch (Exception e) { log.error(e.getMessage()); } finally { returnResource(jedisPool, jedis); } return null; }
Example 3
Source File: JedisUtils.java From fw-spring-cloud with Apache License 2.0 | 5 votes |
/** * <p> * 设置key的值,并返回一个旧值 * </p> * * @param key * @param value * @return 旧值 如果key不存在 则返回null */ public String getset(String key, String value) { Jedis jedis = null; String res = null; try { jedis = jedisPool.getResource(); res = jedis.getSet(key, value); } catch (Exception e) { log.error(e.getMessage()); } finally { returnResource(jedisPool, jedis); } return res; }
Example 4
Source File: JedisUtil.java From scaffold-cloud with MIT License | 5 votes |
/** * getAndset * * @param key * @param newVal * @return */ public static String getAndset(String key, String newVal) { String oldVal = null; Jedis jedis = null; try { jedis = getResource(); oldVal = jedis.getSet(key, newVal); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { close(jedis); } return oldVal; }
Example 5
Source File: RedisPoolUtil.java From redis-distributed-lock with Apache License 2.0 | 5 votes |
public static String getSet(String key, String value){ Jedis jedis = null; String result = null; try { jedis = RedisPool.getJedis(); result = jedis.getSet(key, value); } catch (Exception e){ e.printStackTrace(); } finally { if (jedis != null) { jedis.close(); } return result; } }
Example 6
Source File: RedisClient.java From Mykit with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public <T> T getSetT(String key, T newValue) { Jedis client = jedisPool.getResource(); T t; try { String afterSerialize = JsonUtils.beanToJson(newValue); String value = client.getSet(key, afterSerialize); t = (T) JsonUtils.jsonToBean(value, newValue.getClass()); return t; } finally { // 向连接池“归还”资源 jedisPool.returnResourceObject(client); } }
Example 7
Source File: RedisServiceImpl.java From ace-cache with Apache License 2.0 | 5 votes |
@Override public String getset(String key, String value) { Jedis jedis = null; String res = null; try { jedis = pool.getResource(); res = jedis.getSet(key, value); } catch (Exception e) { LOGGER.error(e.getMessage()); } finally { returnResource(pool, jedis); } return res; }
Example 8
Source File: JedisLock.java From light-task-scheduler with Apache License 2.0 | 5 votes |
/** * Acquire lock. * * @param jedis * @return true if lock is acquired, false acquire timeouted * @throws InterruptedException in case of thread interruption */ protected synchronized boolean acquire(Jedis jedis) throws InterruptedException { int timeout = acquiryTimeoutInMillis; while (timeout >= 0) { final Lock newLock = asLock(System.currentTimeMillis() + lockExpiryInMillis); if (jedis.setnx(lockKeyPath, newLock.toString()) == 1) { this.lock = newLock; return true; } final String currentValueStr = jedis.get(lockKeyPath); final Lock currentLock = Lock.fromString(currentValueStr); if (currentLock.isExpiredOrMine(lockUUID)) { String oldValueStr = jedis.getSet(lockKeyPath, newLock.toString()); if (oldValueStr != null && oldValueStr.equals(currentValueStr)) { this.lock = newLock; return true; } } timeout -= DEFAULT_ACQUIRY_RESOLUTION_MILLIS; Thread.sleep(DEFAULT_ACQUIRY_RESOLUTION_MILLIS); } return false; }
Example 9
Source File: RedisClient.java From apollo with GNU General Public License v2.0 | 5 votes |
/** * get old value and set new value * * @param key * @param value * @param expiration * * @return false if redis did not execute the option * * @throws Exception * @author wangchongjie */ public Object getSet(String key, Object value, Integer expiration) throws Exception { Jedis jedis = null; try { jedis = this.jedisPool.getResource(); long begin = System.currentTimeMillis(); // 操作expire成功返回1,失败返回0,仅当均返回1时,实际操作成功 byte[] val = jedis.getSet(SafeEncoder.encode(key), serialize(value)); Object result = deserialize(val); boolean success = true; if (expiration > 0) { Long res = jedis.expire(key, expiration); if (res == 0L) { success = false; } } long end = System.currentTimeMillis(); if (success) { logger.info("getset key:" + key + ", spends: " + (end - begin) + "ms"); } else { logger.info("getset key: " + key + " failed, key has already exists! "); } return result; } catch (Exception e) { logger.error(e.getMessage(), e); this.jedisPool.returnBrokenResource(jedis); throw e; } finally { if (jedis != null) { this.jedisPool.returnResource(jedis); } } }
Example 10
Source File: DefaultRedis.java From craft-atom with MIT License | 4 votes |
private String getset0(Jedis j, String key, String value) { return j.getSet(key, value); }
Example 11
Source File: JedisUtil.java From Project with Apache License 2.0 | 3 votes |
/** * 获取并设置指定key对应的value<br/> * 如果key存在返回之前的value,否则返回null * * @param key * @param value * @return String 原始value或null */ public String getSet(String key, String value) { Jedis jedis = getJedis(); String str = jedis.getSet(key, value); jedis.close(); return str; }
Example 12
Source File: JedisUtil.java From BigData with GNU General Public License v3.0 | 3 votes |
/** * 获取并设置指定key对应的value<br/> * 如果key存在返回之前的value,否则返回null * * @param String * key * @param String * value * @return String 原始value或null * */ public String getSet(String key, String value) { Jedis jedis = getJedis(); String str = jedis.getSet(key, value); returnJedis(jedis); return str; }