| | |
| | | package com.ks.lijin.aspect; |
| | | |
| | | import com.ks.lijin.service.manager.RedisManager; |
| | | import com.ks.lijin.util.annotation.RedisCache; |
| | | import com.ks.lijin.util.annotation.RedisCacheEvict; |
| | | import org.aspectj.lang.ProceedingJoinPoint; |
| | |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.core.DefaultParameterNameDiscoverer; |
| | | import org.springframework.core.annotation.Order; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.expression.EvaluationContext; |
| | | import org.springframework.expression.Expression; |
| | | import org.springframework.expression.ExpressionParser; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.lang.reflect.Method; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Component |
| | | @Aspect |
| | |
| | | public class RedisCacheAspect { |
| | | Logger logger = LoggerFactory.getLogger(RedisCacheAspect.class); |
| | | @Resource |
| | | private RedisManager redisManager; |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | |
| | | private ExpressionParser parser = new SpelExpressionParser(); |
| | |
| | | return cacheKey; |
| | | } |
| | | |
| | | @Around("execution(public * com.ks.app.service..*.*(..))") |
| | | @Around("execution(public * com.ks.lijin.service..*.*(..))") |
| | | public Object requestSerializable(ProceedingJoinPoint joinPoint) throws Throwable { |
| | | Signature signature = joinPoint.getSignature(); |
| | | MethodSignature methodSignature = (MethodSignature) signature; |
| | |
| | | String cacheKey = getRedisKey(redisCache, joinPoint); |
| | | if (!StringUtil.isNullOrEmpty(cacheKey)) { |
| | | try { |
| | | Object result = redisManager.getDataFromRedis(cacheKey); |
| | | Object result = redisTemplate.opsForValue().get(cacheKey); |
| | | if (result != null) { |
| | | return result; |
| | | } |
| | |
| | | String cacheKey = getRedisKey(redisCache, joinPoint); |
| | | if (!StringUtil.isNullOrEmpty(cacheKey)) { |
| | | try { |
| | | redisManager.setDataToRedis(cacheKey, object, redisCache.second()); |
| | | redisTemplate.opsForValue().set(cacheKey, object, redisCache.second(), TimeUnit.SECONDS); |
| | | } catch (Exception e) { |
| | | logger.error("redis保存缓存数据出错", e.getMessage(), "key:" + cacheKey); |
| | | } |
| | |
| | | if (!StringUtil.isNullOrEmpty(cacheKey)) { |
| | | try { |
| | | if (!StringUtil.isNullOrEmpty(key)) { |
| | | redisManager.remove(cacheKey); |
| | | redisTemplate.delete(cacheKey); |
| | | } else { |
| | | redisManager.removePrefix(cacheKey); |
| | | redisTemplate.delete(cacheKey + "*"); |
| | | } |
| | | } catch (Exception e) { |
| | | logger.error("redis清除缓存数据出错", e.getMessage(), "key:" + cacheKey); |