1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2、相关配置
spring:
application:
name: Application
redis:
host: 127.0.0.1
port: 6379
database: 1
cache:
redis:
time-to-live: 3600000
cache-null-values: true
key-prefix: CACHE_
use-key-prefix: true
package com.zhangz.config;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
//绑定配置文件
@EnableConfigurationProperties(CacheProperties.class)
//开启SpringCache
@EnableCaching
public class RedisCacheConfig {
@Bean
public RedisCacheConfiguration getRedisCacheConfig(CacheProperties cacheProperties){
//获取默认的redis缓存配置
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//设置key的序列化方式
config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
//设置value 的序列化方式
config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
//将CacheProperties(prefix = "spring.cache")相关配置导入
if (redisProperties.getTimeToLive() != null) {
//过期时间 spring.cache.redis.time-to-live
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
//key前缀 spring.cache.redis.key-prefix
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
//是否缓存空值 spring.cache.redis.cache-null-values
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
//是否使用前缀 spring.cache.redis.use-key-prefix
config = config.disableKeyPrefix();
}
return config;
}
}
3、使用
@RequestMapping("/rule")
//默认使用value作为key值、自定义配置key可以使用SpEL表达式
/**
* Spring Expression Language (SpEL) expression for computing the key dynamically.
* <p>Default is {@code ""}, meaning all method parameters are considered as a key,
* unless a custom {@link #keyGenerator} has been configured.
* <p>The SpEL expression evaluates against a dedicated context that provides the
* following meta-data:
* <ul>
* <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
* references to the {@link java.lang.reflect.Method method}, target object, and
* affected cache(s) respectively.</li>
* <li>Shortcuts for the method name ({@code #root.methodName}) and target class
* ({@code #root.targetClass}) are also available.
* <li>Method arguments can be accessed by index. For instance the second argument
* can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
* can also be accessed by name if that information is available.</li>
* </ul>
*/
@Cacheable(value = "rule",key = "#root.methodName")
public String rule(){
return service.putRule();
}