| | |
| | | * @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案 |
| | | */ |
| | | public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) { |
| | | RBucket<Object> bucket = CLIENT.getBucket(key); |
| | | RBucket<T> bucket = CLIENT.getBucket(key); |
| | | if (isSaveTtl) { |
| | | try { |
| | | bucket.setAndKeepTTL(value); |
| | | } catch (Exception e) { |
| | | long timeToLive = bucket.remainTimeToLive(); |
| | | bucket.set(value); |
| | | bucket.expire(Duration.ofMillis(timeToLive)); |
| | | setCacheObject(key, value, Duration.ofMillis(timeToLive)); |
| | | } |
| | | } else { |
| | | bucket.set(value); |
| | |
| | | * @param duration 时间 |
| | | */ |
| | | public static <T> void setCacheObject(final String key, final T value, final Duration duration) { |
| | | RBucket<T> result = CLIENT.getBucket(key); |
| | | result.set(value); |
| | | result.expire(duration); |
| | | RBatch batch = CLIENT.createBatch(); |
| | | RBucketAsync<T> bucket = batch.getBucket(key); |
| | | bucket.setAsync(value); |
| | | bucket.expireAsync(duration); |
| | | batch.execute(); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存Map的key列表 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @return key列表 |
| | | */ |
| | | public static <T> Set<String> getCacheMapKeySet(final String key) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | return rMap.keySet(); |
| | | } |
| | | |
| | | /** |
| | | * 往Hash中存入数据 |
| | | * |
| | | * @param key Redis键 |