ehcache配置
1.在src/main/resources下创建ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!-- echcache的默认缓存策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<!-- 自定义缓存策略 -->
<defaultCache
name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
2.在application.properties中加
spring.cache.ehcache.config=ehcache.xml
3.在启动类加
@EnableCaching
4.在ServiceImpl上的方法加
Cacheable(value="users" key="#pageable.pageSize "):对当前查询的对象做缓存处理,value对应缓存策略
key属性:给存储的值起个名称。在查询时如果有名称相同的,那么则从缓存中取数据。
默认#后面的名字是ServiceImpl是参数的名字。.后面是对象属性
额外:分页可以使用pageable
清除缓存(同步缓存使用)
在ServiceImpl上的方法加@CacheEvict(value="users",allEntries=true)
添加和获取
public class RedisTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
//添加一个字符串
@Test
public void testSet() {
redisTemplate.opsForValue().set("key", "钟佳宏");
}
//获取一个字符串
@Test
public void testGet() {
redisTemplate.opsForValue().get("key");
}
}
Redis
一、redis配置类
@Configurationspring启动会马上加载它,StringRedisSerializer序列化器
@Configuration
public class RedisConfig {
/**
* 1.创建JedisPoolConfig对象。在该对象中完成一些连接池配置
*/
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
//最大空闲数
config.setMaxIdle(10);
//最小空闲数
config.setMinIdle(5);
//最大连接数
config.setMaxTotal(20);
return config;
}
/**
* 2.创建JedisConnectionFactory,配置redis连接信息
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
JedisConnectionFactory factory = new JedisConnectionFactory();
//关联接连池的配置对象
factory.setPoolConfig(config);
//配置连接Redis的信息
//主机地址
factory.setHostName("192.168.169.140");
//端口
factory.setPort(6379);
return factory;
}
/**
* 3.创建RedisTemplate:用于执行Redis操作的方法
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//关联
template.setConnectionFactory(factory);
//为key设置序列化器
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化器
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
二、redis获取配置参数
application.properties
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-active=20
spring.redis.host=192.168.70.128
spring.redis.port=6379
在方法上加@ConfigurartionProperties
/**
* 1.创建JedisPoolConfig对象。在该对象中完成一些连接池配置
*/
@Bean
@ConfigurartionProperties(prefix="spring.redis.pool")
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
存取json格式java对象
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class)();