springboot的缓存Cacheable-Cacheput-CacheEvict-Caching-CacheConfig详解-自定义redis缓存中间件的整合

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;
	}
	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿联爱学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值