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

com.feingto.cloud.config.redis.support.RedisAutoConfiguration Maven / Gradle / Ivy

The newest version!
package com.feingto.cloud.config.redis.support;

import com.feingto.cloud.config.redis.EnableRedisAutoConfiguration;
import com.feingto.cloud.config.redis.adapter.*;
import com.feingto.cloud.kit.json.JSONObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.Advisor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

import java.time.Duration;

/**
 * Redis 缓存自动注入
 *
 * @author longfei
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(annotation = EnableRedisAutoConfiguration.class)
@ConditionalOnClass(RedisOperations.class)
@EnableCaching
public class RedisAutoConfiguration extends CachingConfigurerSupport {
    @Bean
    public KeyGenerator customKeyGenerator() {
        return (target, method, params) -> CacheUtils.generateKey(method, params);
    }

    @Bean
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
        defaultCacheConfig.entryTtl(Duration.ofDays(1));
        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
    }

    @Bean
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        log.info("Bean '{}' has been injected", StringRedisTemplate.class.getName());
        StringRedisTemplate template = new StringRedisTemplate(connectionFactory);
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(new JSONObjectMapper());
        template.setValueSerializer(serializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public Advisor redisCacheablePointcutAdvisor(RedisTemplate redisTemplate) {
        RedisCacheablePointcutAdvisor advisor = new RedisCacheablePointcutAdvisor();
        advisor.setAdvice(new RedisCacheableAroundAdvice(redisTemplate));
        return advisor;
    }

    @Bean
    public Advisor redisCacheEvictPointcutAdvisor(RedisTemplate redisTemplate) {
        RedisCacheEvictPointcutAdvisor advisor = new RedisCacheEvictPointcutAdvisor();
        advisor.setAdvice(new RedisCacheEvictAfterAdvice(redisTemplate));
        return advisor;
    }

    @Bean
    public Advisor redisCachePutPointcutAdvisor(RedisTemplate redisTemplate) {
        RedisCachePutPointcutAdvisor advisor = new RedisCachePutPointcutAdvisor();
        advisor.setAdvice(new RedisCachePutAfterAdvice(redisTemplate));
        return advisor;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy