All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hetianyi.common.cache.ExpireCacheAspectResolver Maven / Gradle / Ivy

package com.github.hetianyi.common.cache;

import com.github.hetianyi.common.util.ExpressionParserUtil;
import com.github.hetianyi.common.util.StringUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnection;

/**
 * 缓存失效注解切面处理类。
 *
 * @author Jason He
 * @version 1.0.5
 * @since 1.0.5
 * @date 2020-02-01
 */
public abstract class ExpireCacheAspectResolver implements CacheAspectResolver {

    private static Logger log = LoggerFactory.getLogger(ExpireCacheAspectResolver.class);

    /**
     * 拦截ExpireCache注解的方法,对启用该注解的方法删除相应的缓存。
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation(expireCache)")
    @Override
    public Object resolve(ProceedingJoinPoint joinPoint, ExpireCache expireCache) throws Throwable {
        if (!expireCache.enable()) {
            return joinPoint.proceed();
        }

        if (StringUtil.isNullOrEmptyTrimed(expireCache.key())) {
            throw new IllegalArgumentException("key expression cannot be empty");
        }

        RedisConnection redisConnection = redisConnectionFactory().getConnection();

        byte[] key = ExpressionParserUtil.parse(expireCache.key(), "args", joinPoint.getArgs(), String.class).getBytes();

        // expire cache
        redisConnection.del(key);

        if (log.isDebugEnabled()) {
            log.debug("cache expired: {}", new String(key));
        }

        return joinPoint.proceed();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy