SpringBoot系列——使用技巧之方法添加缓存功能
一、文章概述
各位小伙伴,在JAVA编程中,肯定常常会遇到某个方法因处理时间较为缓慢,如果每次调用都重新进行响应处理,较为浪费性能和时间。本文教大家使用老生常谈的Springboot + Spring data redis缓存技术实现简单的方法调用,有兴趣的同志,可详细探究哈!!!
文章书写不易,点个关注不迷路哦,O(∩_∩)O哈哈~
二、准备工作
- 开发环境
java 1.8 springboot2.0版本 maven
三、具体实现
1. 导入依赖
导入相关依赖,cache、spring data redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.4</version>
</dependency>
2. 添加redis配置
在application中添加redis配置和使用缓存
spring:
cache:
type: redis
redis:
host: 192.168.2.156
port: 32190
database: 2
jedis:
pool:
max-active: 200
max-wait: -1
min-idle: 10
timeout: 2000
3. 添加缓存配置类
该配置类,主要配置内容为:自定义缓存配置,自定义缓存key生成器
关于什么是缓存配置、key生成器,这个大家自行了解一下,这里就不多赘述啦_
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Method;
@Configuration
public class CacheManagerConfig {
@Bean
public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() {
return new CacheManagerCustomizer<ConcurrentMapCacheManager>() {
public void customize(ConcurrentMapCacheManager cacheManager) {
cacheManager.setAllowNullValues(false);
}
};
}
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
public Object generate(Object target, Method method, Object... params) {
return method.getName() + "-" + params.toString();
}
};
}
}
4. 配置redis连接池
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class JedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.jedis.pool.max-wait}")
private int maxWait;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.timeout}")
private int timeout;
@Bean
public JedisPool jedisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxWaitMillis(maxWait);
jedisPoolConfig.setMaxTotal(maxActive);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null);
Logger logger = LoggerFactory.getLogger(JedisPool.class);
logger.info("redis连接成功:{} {}", host, port);
return jedisPool;
}
}
5. 方法添加注解实现缓存
-
关于缓存注解 @EnableCaching: ? 开关性注解**,在项目启动类或某个配置类上使用此注解后,则**表示允许使用注解的方式进行缓存操作 @Cacheable: ? 可用于类或方法上;在目标方法执行前,会根据key先去缓存中查询看是否有数据,有就直接 ? 返回缓存中的key对应的value值。不再执行目标方法;无则执行目标方法,并将方法的返回值 ? 作为value,并以键值对的形式存入缓存 @CachePut: ? **可用于类或方法上;**在执行完目标方法后,并将方法的返回值作为value,并以键值对的形式存入缓存中 @CacheEvice: ? 可用于类或方法上;在执行完目标方法后,清除缓存中对应key的数据 @Caching: ? 此注解用作复杂缓存配置情形,可组合@Cacheable、@CacheEvict、@CachePut三种注解任意几种使用 @CacheConfig: ? 用于配置一些公共的属性 -
本文主要使用的注解
-
实现代码 在需要使用缓存的方法上添加@Cacheable注解,即可解锁方法缓存功能,如下:
@Cacheable(cacheNames = "getName", keyGenerator = "myKeyGenerator")
public String getName(int key) {
System.out.println("缓存在不存在该key:"+key);
return persionName.get(key);
}
6. 测试代码
不知道怎么用的小伙伴,给大家提供一个简单的测试代码。但是工作中,大家完成以上配置后,肯定不会这么简单调用,在自己工程中灵活使用即可,但是要想用的出神入化,还需要深入了解缓存技术以及redis缓存雪崩等各类问题。
import org.springframework.cache.annotation.Cacheable;
import java.util.HashMap;
import java.util.Map;
public class CacheUtils {
static final Map<Integer, String> persionName = new HashMap<Integer, String>(){};
static {
persionName.put(1, "JAVA");
persionName.put(2, "PYTHON");
persionName.put(3, "GO");
}
@Cacheable(cacheNames = "getName", keyGenerator = "myKeyGenerator")
public String getName(int key) {
System.out.println("缓存在不存在该key:"+key);
return persionName.get(key);
}
public static void main(String[] args) {
String name = new CacheUtils().getName(1);
String name1 = new CacheUtils().getName(1);
}
}
通过Spring data redis实现的缓存仅为个人想法,如果有其他好的想法,欢迎评论区交流。
|