pull/3/head
litingting 9 months ago
parent 57aa7484fb
commit e9c5da4aae

@ -12,43 +12,63 @@ import redis.clients.jedis.JedisPool;
* @Date 2019/1/1 15:03 * @Date 2019/1/1 15:03
* @CONTACT 317758022@qq.com * @CONTACT 317758022@qq.com
* @DESC * @DESC
* Redis
* Spring使JedisRedis
*/ */
@Component @Component
@Slf4j @Slf4j
public class CommonCacheUtil { public class CommonCacheUtil {
// 通过依赖注入获取JedisPoolWrapper实例用于后续获取JedisPool来操作Redis
@Autowired @Autowired
private JedisPoolWrapper jedisPoolWrapper; private JedisPoolWrapper jedisPoolWrapper;
/** /**
* key * key
* RediskeyvalueRedis
* SnailmallException
*
* @param key
* @param value
*/ */
public void cache(String key, String value) { public void cache(String key, String value) {
try { try {
// 通过JedisPoolWrapper获取JedisPool实例JedisPool用于管理Jedis连接资源
JedisPool pool = jedisPoolWrapper.getJedisPool(); JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) { if (pool!= null) {
// 从JedisPool中获取一个Jedis连接资源使用try-with-resources语句确保资源能自动关闭
try (Jedis Jedis = pool.getResource()) { try (Jedis Jedis = pool.getResource()) {
// 选择Redis的数据库这里选择索引为0的数据库通常用于存放通用缓存数据等情况
Jedis.select(0); Jedis.select(0);
// 将键值对存入Redis
Jedis.set(key, value); Jedis.set(key, value);
} }
} }
} catch (Exception e) { } catch (Exception e) {
// 如果出现异常,记录详细的错误日志,日志内容为"redis存值失败"以及异常信息
log.error("redis存值失败", e); log.error("redis存值失败", e);
// 抛出自定义的SnailmallException异常异常信息为"redis报错"
throw new SnailmallException("redis报错"); throw new SnailmallException("redis报错");
} }
} }
/** /**
* key * key
* Rediskeyvalue
*
*
* @param key
* @return Redisnull
*/ */
public String getCacheValue(String key) { public String getCacheValue(String key) {
String value = null; String value = null;
try { try {
JedisPool pool = jedisPoolWrapper.getJedisPool(); JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) { if (pool!= null) {
try (Jedis Jedis = pool.getResource()) { try (Jedis Jedis = pool.getResource()) {
Jedis.select(0); Jedis.select(0);
// 从Redis中获取指定键的值
value = Jedis.get(key); value = Jedis.get(key);
} }
} }
@ -61,15 +81,24 @@ public class CommonCacheUtil {
/** /**
* key * key
*
* Redissetnx
*
* @param key
* @param value
* @param expire Redis
* @return jedis.setnx10
*/ */
public long cacheNxExpire(String key, String value, int expire) { public long cacheNxExpire(String key, String value, int expire) {
long result = 0; long result = 0;
try { try {
JedisPool pool = jedisPoolWrapper.getJedisPool(); JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) { if (pool!= null) {
try (Jedis jedis = pool.getResource()) { try (Jedis jedis = pool.getResource()) {
jedis.select(0); jedis.select(0);
// 原子性地设置缓存值(当键不存在时才设置),返回设置结果
result = jedis.setnx(key, value); result = jedis.setnx(key, value);
// 为该键设置过期时间
jedis.expire(key, expire); jedis.expire(key, expire);
} }
} }
@ -83,13 +112,18 @@ public class CommonCacheUtil {
/** /**
* key * key
* Rediskey
*
*
* @param key
*/ */
public void delKey(String key) { public void delKey(String key) {
JedisPool pool = jedisPoolWrapper.getJedisPool(); JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool != null) { if (pool!= null) {
try (Jedis jedis = pool.getResource()) { try (Jedis jedis = pool.getResource()) {
jedis.select(0); jedis.select(0);
try { try {
// 调用Jedis的del方法删除指定的键
jedis.del(key); jedis.del(key);
} catch (Exception e) { } catch (Exception e) {
log.error("从redis中删除失败", e); log.error("从redis中删除失败", e);
@ -101,4 +135,4 @@ public class CommonCacheUtil {
} }
Loading…
Cancel
Save