mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-30 06:37:36 +00:00
[Update] RedisPoolCacheStore 优化代码;
This commit is contained in:
parent
1f3d99ac10
commit
0f202cb076
@ -9,6 +9,7 @@ import redis.clients.jedis.*;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@ -50,29 +51,27 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(String key, T value, Date expire) {
|
public void update(String key, T value, Date expire) {
|
||||||
Jedis jedis = jedisPool.getResource();
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
jedis.set(keyPrefix + key, parse(value));
|
jedis.set(keyPrefix + key, parse(value));
|
||||||
if(expire != null) {
|
if(expire != null) {
|
||||||
jedis.pexpireAt(keyPrefix + key, expire.getTime());
|
jedis.pexpireAt(keyPrefix + key, expire.getTime());
|
||||||
log.debug("已设置Key {} 的过期时间(Expire: {})", key, expire.getTime());
|
log.debug("已设置Key {} 的过期时间(Expire: {})", key, expire.getTime());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
jedis.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T getCache(String key) {
|
public T getCache(String key) {
|
||||||
Jedis jedis = jedisPool.getResource();
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
T result = analysis(jedis.get(keyPrefix + key));
|
return analysis(jedis.get(keyPrefix + key));
|
||||||
jedis.close();
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean exists(String key) {
|
public boolean exists(String key) {
|
||||||
Jedis jedis = jedisPool.getResource();
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
boolean result = jedis.exists(keyPrefix + key);
|
return jedis.exists(keyPrefix + key);
|
||||||
jedis.close();
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,11 +81,17 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean clear() {
|
public boolean clear() {
|
||||||
Jedis jedis = jedisPool.getResource();
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
String result = jedis.flushDB();
|
String result = jedis.flushDB();
|
||||||
jedis.close();
|
log.info("flushDB返回结果: {}", result);
|
||||||
log.info("flushDB返回结果: {}", result);
|
return true;
|
||||||
return true;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> keys() {
|
||||||
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
|
return jedis.keys(keyPrefix + "*");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user