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 ) ; } } }
get old value and set new value