diff --git a/snailmall-shipping-service/src/main/java/com/njupt/swg/cache/CommonCacheUtil.java b/snailmall-shipping-service/src/main/java/com/njupt/swg/cache/CommonCacheUtil.java index 66c6499..f756dd7 100644 --- a/snailmall-shipping-service/src/main/java/com/njupt/swg/cache/CommonCacheUtil.java +++ b/snailmall-shipping-service/src/main/java/com/njupt/swg/cache/CommonCacheUtil.java @@ -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 + * 缓存永久key的方法,用于将给定的键值对以永久存储的方式存入Redis中(这里所说的永久是指没有设置过期时间) + * 首先尝试从jedisPoolWrapper获取Jedis连接池,如果连接池不为空,则从连接池中获取一个Jedis客户端资源, + * 接着选择Redis的第0个数据库(Jedis.select(0)操作),然后使用Jedis客户端的set方法将键值对存入Redis中, + * 如果在这个过程中出现任何异常,会记录错误日志,并抛出一个自定义的SnailmallException异常,提示Redis报错 */ 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 + * 获取缓存key对应的value值的方法,尝试从Redis中获取指定键对应的缓存值 + * 先初始化一个用于接收返回值的变量value为null,然后尝试从jedisPoolWrapper获取Jedis连接池, + * 若连接池不为空,则从中获取Jedis客户端资源,选择Redis的第0个数据库后,使用Jedis客户端的get方法获取指定键对应的value值, + * 如果出现异常,同样会记录错误日志并抛出SnailmallException异常提示Redis报错,最后将获取到的value值返回(可能为null,如果键不存在等情况) */ 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 + * 过期key相关操作的方法,先尝试将给定的键值对以原子操作的方式存入Redis(使用setnx,只有键不存在时才设置成功), + * 然后设置该键在Redis中的过期时间(以秒为单位,由传入的expire参数指定) + * 首先初始化一个用于记录操作结果的变量result为0,接着尝试从jedisPoolWrapper获取Jedis连接池, + * 若连接池不为空,则获取Jedis客户端资源,选择数据库后,使用setnx方法设置键值对并将返回结果赋值给result, + * 随后使用expire方法设置键的过期时间,若在过程中出现异常,记录错误日志并抛出SnailmallException异常提示Redis报错, + * 最后将操作结果(setnx的返回值,成功为1,失败为0)返回 */ 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 + * 删除缓存key的方法,用于从Redis中删除指定的键 + * 先尝试从jedisPoolWrapper获取Jedis连接池,若连接池不为空,则获取Jedis客户端资源,选择数据库后, + * 使用Jedis客户端的del方法尝试删除指定的键,若在删除过程中出现异常,记录错误日志并抛出SnailmallException异常提示Redis报错 */ 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 { -} +} \ No newline at end of file