pull/4/head
zhouyu 9 months ago
parent a615429ca4
commit eafc9437f9

@ -13,21 +13,27 @@ import redis.clients.jedis.JedisPool;
* @CONTACT 317758022@qq.com
* @DESC
*/
// 使用@Component注解将该类标记为Spring容器中的一个组件这样Spring可以对其进行管理方便进行依赖注入等操作
@Component
// 使用lombok的@Slf4j注解用于自动生成日志相关的代码使得在类中可以方便地记录各种日志信息便于调试和问题排查
@Slf4j
public class CommonCacheUtil {
// 通过Spring的依赖注入机制使用@Autowired注解自动注入JedisPoolWrapper类型的实例
// JedisPoolWrapper应该是对Jedis连接池进行了一定封装的类后续操作Redis会借助这个实例获取Jedis连接池来获取Jedis客户端实例进行相关操作
@Autowired
private JedisPoolWrapper jedisPoolWrapper;
/**
* key
* keyRedis
* jedisPoolWrapperJedisJedis
* Redis0Jedis.select(0)使JedissetRedis
* SnailmallExceptionRedis
*/
public void cache(String key, String value) {
try {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) {
if (pool!= null) {
try (Jedis Jedis = pool.getResource()) {
Jedis.select(0);
Jedis.set(key, value);
@ -40,13 +46,16 @@ public class CommonCacheUtil {
}
/**
* key
* keyvalueRedis
* valuenulljedisPoolWrapperJedis
* JedisRedis0使Jedisgetvalue
* SnailmallExceptionRedisvaluenull
*/
public String getCacheValue(String key) {
String value = null;
try {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) {
if (pool!= null) {
try (Jedis Jedis = pool.getResource()) {
Jedis.select(0);
value = Jedis.get(key);
@ -60,13 +69,18 @@ public class CommonCacheUtil {
}
/**
* key
* keyRedis使setnx
* Redisexpire
* result0jedisPoolWrapperJedis
* Jedis使setnxresult
* 使expireSnailmallExceptionRedis
* setnx10
*/
public long cacheNxExpire(String key, String value, int expire) {
long result = 0;
try {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) {
if (pool!= null) {
try (Jedis jedis = pool.getResource()) {
jedis.select(0);
result = jedis.setnx(key, value);
@ -82,11 +96,13 @@ public class CommonCacheUtil {
}
/**
* key
* keyRedis
* jedisPoolWrapperJedisJedis
* 使JedisdelSnailmallExceptionRedis
*/
public void delKey(String key) {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) {
if (pool!= null) {
try (Jedis jedis = pool.getResource()) {
jedis.select(0);
try {
@ -101,4 +117,4 @@ public class CommonCacheUtil {
}
}
Loading…
Cancel
Save