1.springboot五大缓存注解:
注解名 详细内容 @Cacheable | 作用:存值入缓存,先检查缓存是否存在,在查询数据库。 属性: 1.value/cacheNames:指定缓存地址的key。 2.key:指定缓存内容对象的key。 3.cacheManager:指定缓存管理器,默认为:ConCurrentMapCacheManager。 4.keyGenerator:指定key的生成器。 5.condition:指定缓存方法的条件。 6.unless:指定不缓存的条件。· 7.syns:是否开启异步。 |
@CachePut | 作用:先调用方法,在更新数据库。 |
@CacheEvict | 作用:清除缓存。 属性: 1.allEntries:指定清除缓存中所有记录。 2.beforeInvocation:执行方法之前先清除缓存中的数据。 |
@Caching | 作用:用于复杂的注解,一般Cacheable,Cacheable结合起来使用。 |
@CacheConfig | 作用:抽取多缓存的公共配置。 |
2.缓存SpEl
3.springboot整合redis
1.先引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置redis数据库application.properties
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=alian
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=10
3.启动类上添加@EnableCaching
4.查看redis提供的配置类RedisAutoConfiguration中包含两个缓存管理器RedisTemplate和StringRedisTemplate。
5.简单测试demo(向数据库中追加一个值)
package alian;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import com.sun.org.apache.regexp.internal.recompile;
@SpringBootTest(classes = SpringbootMybatisApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class Testdemo {
@Autowired
RedisTemplate<String,String> redistemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void demo1()
{
redistemplate.opsForValue().append("alian", "cls");
}
}
6.自定义缓存管理器(修改默序列化方式)
package alian.config;
import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.http.codec.json.Jackson2CodecSupport;
/*
*
* 自定义缓存管理器
*
* */
@Configuration
public class MyredisTemplate {
@Bean
public RedisTemplate<Object, Object> MyredisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setConnectionFactory(redisConnectionFactory);
// 设置json格式序列化存取操作
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =new Jackson2JsonRedisSerializer<Object>(Object.class);
template.setDefaultSerializer(jackson2JsonRedisSerializer);
return template;
}
}