| | |
| | | import com.vincent.rsf.common.utils.Serialize; |
| | | import com.vincent.rsf.server.common.config.RedisProperties; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import redis.clients.jedis.Jedis; |
| | |
| | | JedisPoolConfig config = new JedisPoolConfig(); |
| | | config.setTestOnBorrow(false); |
| | | this.index = redisProperties.getIndex(); |
| | | // 空白密码传 null,不向未开认证的 Redis 发 AUTH |
| | | String pwd = StringUtils.trimToNull(redisProperties.getPassword()); |
| | | this.pool = new JedisPool(config |
| | | , redisProperties.getHost() |
| | | , redisProperties.getPort() |
| | | , redisProperties.getTimeout() |
| | | , redisProperties.getPassword() |
| | | , pwd |
| | | ); |
| | | } |
| | | return this.pool; |
| | | } |
| | | |
| | | /** 借连接失败时返回 null */ |
| | | public Jedis getJedis(){ |
| | | try{ |
| | | Jedis jedis = this.getPool().getResource(); |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.set((flag + LINK + key).getBytes(), Serialize.serialize(value)); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.setex((flag + LINK + key).getBytes(), seconds, Serialize.serialize(value)); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | byte[] bytes = jedis.get((flag + LINK + key).getBytes()); |
| | | if(bytes == null || bytes.length == 0 ) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.del((flag + LINK + key).getBytes()); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | this.setValue(flag, "CLEARING", "true"); |
| | | try{ |
| | | Object returnValue = jedis.eval("local keys = redis.call('keys', ARGV[1]) for i=1,#keys,1000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return #keys",0,flag + LINK + "*"); |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expire((flag + LINK + key).getBytes(), seconds); |
| | | } catch (Exception e) { |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expireAt((flag + LINK + key).getBytes(), toTime.getTime()/1000); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.ttl((flag + LINK + key).getBytes()); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.set(flag + LINK + key, value); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.setex(flag + LINK + key, seconds , value); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.get(flag + LINK + key); |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | String[] keys = new String[key.length]; |
| | | for(int i=0;i<key.length;i++){ |
| | |
| | | |
| | | this.setValue(flag, "CLEARING", "true"); |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | |
| | | try{ |
| | | Object returnValue = jedis.eval("return redis.call('del', unpack(redis.call('keys', ARGV[1])))",0,flag + LINK + "*"); |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expire((flag + LINK + key).getBytes(), seconds); |
| | | } catch (Exception e) { |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expireAt((flag + LINK + key).getBytes(), atTime.getTime()/1000); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | return jedis.hset(name.getBytes(), key.getBytes(), Serialize.serialize(value)); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | byte[] bytes = jedis.hget(name.getBytes(), key.getBytes()); |
| | | if (bytes == null || bytes.length == 0) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.hkeys(name); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | String[] keys = new String[key.length]; |
| | | System.arraycopy(key, 0, keys, 0, key.length); |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.del(name); |
| | | } catch (Exception e) { |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expire(name.getBytes(), seconds); |
| | | } catch (Exception e) { |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expireAt(name.getBytes(), atTime.getTime()/1000); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.rpush(name.getBytes(), Serialize.serialize(value)); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | byte[] bytes = jedis.lpop(name.getBytes()); |
| | | if(bytes == null || bytes.length == 0) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.del(name); |
| | | } catch (Exception e) { |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expire(name.getBytes(), seconds); |
| | | } catch (Exception e) { |
| | |
| | | return; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return; |
| | | } |
| | | try{ |
| | | jedis.expireAt(name.getBytes(), atTime.getTime()/1000); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.incr("COUNT." + key); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return null; |
| | | } |
| | | try{ |
| | | return jedis.decr("COUNT." + key); |
| | | } catch (Exception e) { |
| | |
| | | return null; |
| | | } |
| | | |
| | | /** SET NX EX;键已存在返回 false;未初始化或借连接失败返回 true,由调用方降级 */ |
| | | public boolean trySetStringNxEx(String flag, String key, String value, int expireSeconds) { |
| | | if (!this.initialize) { |
| | | return true; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return true; |
| | | } |
| | | try { |
| | | String fullKey = flag + LINK + key; |
| | | String r = jedis.set(fullKey, value, "NX", "EX", expireSeconds); |
| | | return "OK".equals(r); |
| | | } catch (Exception e) { |
| | | log.error(this.getClass().getSimpleName(), e); |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | /** SETNX + EXPIRE;未抢到锁返回 false;jedis 异常时降级为 true */ |
| | | public boolean tryLockValue(String flag, String key, int expireSeconds) { |
| | | if(!this.initialize) { |
| | | return true; |
| | | } |
| | | Jedis jedis = this.getJedis(); |
| | | if (jedis == null) { |
| | | return true; |
| | | } |
| | | try { |
| | | String lockKey = flag + LINK + key; |
| | | Long r = jedis.setnx(lockKey, "1"); |
| | | if (r != null && r == 1L) { |
| | | jedis.expire(lockKey, expireSeconds); |
| | | return true; |
| | | } |
| | | return false; |
| | | } catch (Exception e) { |
| | | log.error(this.getClass().getSimpleName(), e); |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | public void unlockValue(String flag, String key) { |
| | | deleteValue(flag, key); |
| | | } |
| | | |
| | | } |