spring boot整合redis
依赖jar
或者
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置
################redis 集群版#########################
spring.redis.cluster.nodes=192.168.25.129:7001,192.168.25.129:7002,192.168.25.129:7003,192.168.25.129:7004,192.168.25.129:7005,192.168.25.129:7006
####################################################
##################redis单机版#####################
#spring.redis.host=211.159.182.188
#spring.redis.port=6379
#spring.redis.database=0
#################设置redis做数据缓存############################
spring.cache.type=redis
启动缓存注解
注入StringRedisTemplate享受redis读写功能
@Autowired
private StringRedisTemplate redisTemplate;
redis使用注解自动缓存
1.开启缓存
在application.properties指定缓存类型为redis
spring.cache.type=redis
2.查询缓存
@Cacheable(value="stus",key="#name")
生成的key为 stus:name变量的值
3.缓存同步(清空缓存)
@CacheEvict(value={"stus"},allEntries=true)