SpringBoot+Redis做缓存


## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379


<!--Redis pom-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!--开启Redis注解功能-->
@EnableCaching
@SpringBootApplication
public class SunnySaltApplication {

public static void main(String[] args) {
SpringApplication.run(SunnySaltApplication.class, args);
}

}

<!--Configuration的配置-->
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;



@Configuration
public class RedisConfig {

@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}

@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}

@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

//解决查询缓存转换异常的问题 转换成json格式
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);

// 配置序列化(解决乱码的问题)
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();

RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}

}
<!--Service层-->
@Autowired
private AccountMapper mapper;

@Cacheable(cacheNames = "cache1",key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#param")
public Account selectPrimaryKey(int id){
Account account = mapper.selectPrimaryKey(id);
return account;
}

@Cacheable(cacheNames = "cache1",key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#param")
public List<Account> findAll(){
List<Account> accounts = mapper.selectAccount();
return accounts;
}

@CacheEvict(cacheNames = "cache1",allEntries = true)
public void insertAccount(Account account){
mapper.insert(account);
}

@Cacheable 通常用于配置方法,将方法的返回结果注入到缓存对象中
@CacheConfig 用于对类进行配置,对整个类的缓存进行配置,可用 @Cacheable取代
@CacheEvict 可用于类或方法,用于清空缓存
@CachePut 强制执行方法并将返回结果放入缓存,而不是像 @Cacheable 那样首先从缓存中寻找方法返回结果是否存在缓存
@EnableCaching 用于SpringBoot的启动类,开启注解功能



 
posted @ 2020-08-11 15:08  爵士灬  阅读(303)  评论(0编辑  收藏  举报