springboot使用redis作为缓存

添加 依赖

<!-- 缓存: spring cache -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-cache</artifactId>
 </dependency>

 <!-- 缓存: redis -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

添加配置

# Redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.database=0
spring.redis.password=xxx

配置缓存

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.lang.reflect.Method;

@Configuration
public class KeyGeneratorCacheConfig extends CachingConfigurerSupport {

 private final RedisTemplate redisTemplate;

 @Autowired
 public KeyGeneratorCacheConfig(RedisTemplate redisTemplate) {
 this.redisTemplate = redisTemplate;
 }

 @Override
 public CacheManager cacheManager() {
 // 设置key的序列化方式为String
 redisTemplate.setKeySerializer(new StringRedisSerializer());
 // 设置value的序列化方式为JSON
 GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
 redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
 RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
 // 设置默认过期时间为600秒
 cacheManager.setDefaultExpiration(600);
 return cacheManager;
 }

 /**
 * key值为className+methodName+参数值列表
 * @return
 */
 @Override
 public KeyGenerator keyGenerator() {
 return new KeyGenerator() {
 @Override
 public Object generate(Object o, Method method, Object... args) {
 StringBuilder sb = new StringBuilder();
 sb.append(o.getClass().getName()).append("#");
 sb.append(method.getName()).append("(");
 for (Object obj : args) {
 if(obj != null) { // 在可选参数未给出时时,会出现null,此时需要跳过
 sb.append(obj.toString()).append(",");
 }
 }
 sb.append(")");
 return sb.toString();
 }
 };
 }
}

在serviceImpl中加入@CacheConfig并且给给每个方法加入缓存

@Service
@CacheConfig(cacheNames = "constant")
public class ConstantServiceImpl implements ConstantService {

 @Autowired
 private ConstantMapper constantMapper;

 @Cacheable
 @Override
 public List<Constant> alertMessage() {
 ConstantExample constantExample = new ConstantExample();
 ConstantExample.Criteria criteria = constantExample.createCriteria();
 criteria.andTypeEqualTo("alert");
 return constantMapper.selectByExample(constantExample);
 }

 @Cacheable
 @Override
 public List<Constant> noteMessage() {
 ConstantExample constantExample = new ConstantExample();
 ConstantExample.Criteria criteria = constantExample.createCriteria();
 criteria.andTypeEqualTo("note");
 return constantMapper.selectByExample(constantExample);
 }

 @Cacheable
 @Override
 public List<Constant> banner() {
 ConstantExample constantExample = new ConstantExample();
 ConstantExample.Criteria criteria = constantExample.createCriteria();
 criteria.andTypeEqualTo("banner");
 return constantMapper.selectByExample(constantExample);
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值