package com.lichuang.redislearn.config;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
@ComponentScan
@EnableCachingpublic classAppConfig {
@BeanpublicRedisStandaloneConfiguration redisStandaloneConfiguration(){
RedisStandaloneConfiguration standaloneConfiguration= new RedisStandaloneConfiguration("101.132.107.145",6379);returnstandaloneConfiguration;
}
@BeanpublicRedisConnectionFactory redisConnectionFactory(){
LettuceConnectionFactory lettuceConnectionFactory= newLettuceConnectionFactory(redisStandaloneConfiguration());returnlettuceConnectionFactory;
}
@BeanpublicStringRedisTemplate stringRedisTemplate(){
StringRedisTemplate stringRedisTemplate= newStringRedisTemplate(redisConnectionFactory());returnstringRedisTemplate;
}
@BeanpublicCacheManager cacheManager(){
RedisCacheConfiguration redisCacheConfiguration=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(newGenericJackson2JsonRedisSerializer()));
RedisCacheWriter redisCacheWriter=RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory());
RedisCacheManager redisCacheManager=newRedisCacheManager(redisCacheWriter,redisCacheConfiguration);returnredisCacheManager;
}
@BeanpublicJdbcTemplate jdbcTemplate(){
DriverManagerDataSource dataSource=newDriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("123");
dataSource.setUrl("jdbc:mysql://localhost:3306/docker-test?serverTimezone=UTC");return newJdbcTemplate(dataSource);
}
}