zhouyu 9 months ago
parent af12ec0e00
commit b0b6fbdcaf

@ -13,31 +13,21 @@ import redis.clients.jedis.JedisPool;
* @CONTACT 317758022@qq.com
* @DESC
*/
// 使用Spring框架的@Component注解将该类标记为一个组件意味着Spring会对这个类进行管理在需要使用这个类的实例时可以通过依赖注入等方式获取方便在整个应用的各个组件之间进行协作和复用。
@Component
// 使用lombok的@Slf4j注解用于自动生成日志相关的代码方便在类中记录各种操作相关的日志信息便于后续查看操作情况以及进行问题排查比如当与Redis交互出现异常等情况时可以详细记录错误信息帮助定位和解决问题。
@Slf4j
public class CommonCacheUtil {
// 通过Spring的依赖注入机制使用@Autowired注解自动注入JedisPoolWrapper类型的实例JedisPoolWrapper应该是对JedisPoolJedis连接池进行了一定封装的类通过它可以获取到Jedis连接池对象进而获取Jedis客户端实例来操作Redis缓存。
@Autowired
private JedisPoolWrapper jedisPoolWrapper;
/**
* Rediskeyvalue
*
* JedisPoolWrapperJedisPoolJedisPoolnullJedisJedisRedis
* try-with-resourcesJedisJedis使JedisJedisselect(0)Redis0Redis0使setRedis
* log.errorredisSnailmallExceptionredis便
*/
/**
* key
*/
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);
@ -49,12 +39,6 @@ public class CommonCacheUtil {
}
}
/**
* Rediskey
*
* valuenullJedisPoolWrapperJedisPoolJedisPoolnulltry-with-resourcesJedisRedis0使getRedisvalue
* redisSnailmallExceptionredisnull
*/
/**
* key
*/
@ -62,7 +46,7 @@ public class CommonCacheUtil {
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);
@ -75,13 +59,6 @@ public class CommonCacheUtil {
return value;
}
/**
* Redis使setnxset if not exists
*
* result0JedisPoolWrapperJedisPoolJedisPoolnulltry-with-resourcesJedisRedis0使setnxRedissetnx10result
* setnxexpireexpire
* redisSnailmallExceptionredisresult1
*/
/**
* key
*/
@ -89,7 +66,7 @@ public class CommonCacheUtil {
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);
@ -104,17 +81,12 @@ public class CommonCacheUtil {
return result;
}
/**
* RediskeyRedis
*
* JedisPoolWrapperJedisPoolJedisPoolnulltry-with-resourcesJedisRedis0使delRedisredisSnailmallExceptionredis便
*/
/**
* key
*/
public void delKey(String key) {
JedisPool pool = jedisPoolWrapper.getJedisPool();
if (pool!= null) {
if (pool != null) {
try (Jedis jedis = pool.getResource()) {
jedis.select(0);
try {
@ -129,4 +101,4 @@ public class CommonCacheUtil {
}
}

Loading…
Cancel
Save