redis服务启动
客户端连接
<!-- spring boot redis缓存引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lecttuce 缓存连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
package com.stu.service.base.config;
/******************************
* 用途说明:
* 作者姓名: Administrator
* 创建时间: 2022-06-29 23:15
******************************/
import org.springframework.cache.CacheManager;
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.cache.RedisCacheManager;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
import java.time.Duration;
/**
* Redis配置
*/
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
// 配置数据连接池
redisTemplate.setConnectionFactory(connectionFactory);
// 序列化自定义
// key 的序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
// value 的序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
@Bean
public CacheManager cacheManager(LettuceConnectionFactory connectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
//过期时间600秒
.entryTtl(Duration.ofSeconds(600))
// 配置序列化
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
3.1 Spring Boot缓存注解
(1)缓存@Cacheable
根据方法对其返回结果进行缓存,下次请求时,如果缓存存在,则直接读取缓存数据返回;如果缓存不存在,则执行方法,并把返回的结果存入缓存中。一般用在查询方法上。
查看源码,属性值如下:
属性/方法名 | 解释 |
---|---|
value | 缓存名,必填,它指定了你的缓存存放在哪块命名空间 |
cacheNames | 与 value 差不多,二选一即可 |
key | 可选属性,可以使用 SpEL 标签自定义缓存的key |
(2)缓存@CachePut
使用该注解标志的方法,每次都会执行,并将结果存入指定的缓存中。其他方法可以直接从响应的缓存中读取缓存数据,而不需要再去查询数据库。一般用在新增方法上。
查看源码,属性值如下:
属性/方法名 | 解释 |
---|---|
value | 缓存名,必填,它指定了你的缓存存放在哪块命名空间 |
cacheNames | 与 value 差不多,二选一即可 |
key | 可选属性,可以使用 SpEL 标签自定义缓存的key |
(3)缓存@CacheEvict
使用该注解标志的方法,会清空指定的缓存。一般用在更新或者删除方法上
查看源码,属性值如下:
属性/方法名 | 解释 |
---|---|
value | 缓存名,必填,它指定了你的缓存存放在哪块命名空间 |
cacheNames | 与 value 差不多,二选一即可 |
key | 可选属性,可以使用 SpEL 标签自定义缓存的key |
allEntries | 是否清空所有缓存,默认为 false。如果指定为 true,则方法调用后将立即清空所有的缓存 |
beforeInvocation | 是否在方法执行前就清空,默认为 false。如果指定为 true,则在方法执行前就会清空缓存 |
3.2 接口改造
(1)在配置文件添加redis配置
spring节点下
redis: # redis 配置
host: 127.0.0.1
port: 6379
database: 0
password: #默认为空
lettuce:
pool:
max-active: 20 #最大连接数,负值表示没有限制,默认8
max-wait: -1 #最大阻塞等待时间,负值表示没限制,默认-1
max-idle: 8 #最大空闲连接,默认8
min-idle: 0 #最小空闲连接,默认0
package com.stu.service.cms.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.service.base.result.R;
import com.stu.service.cms.entity.Ad;
import com.stu.service.cms.entity.vo.AdVo;
import com.stu.service.cms.feign.OssRemoveFileService;
import com.stu.service.cms.mapper.AdMapper;
import com.stu.service.cms.service.AdService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
* 广告推荐 服务实现类
* </p>
*
* @author stu
* @since 2022-06-15
*/
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {
@Autowired
private OssRemoveFileService ossRemoveFileService;
/***********************************
* 用途说明:广告位分页
* @param page
* @param limit
* 返回值说明:
* @return com.baomidou.mybatisplus.core.metadata.IPage<com.stu.service.cms.entity.vo.AdVo>
***********************************/
@Override
public IPage<AdVo> pageList(Long page, Long limit) {
QueryWrapper<AdVo> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("t1.sort");
Page<AdVo> pageResult = new Page<>(page, limit);
List<AdVo> result = baseMapper.pageList(pageResult, queryWrapper);
pageResult.setRecords(result);
return pageResult;
}
/***********************************
* 用途说明:删除云端图片
* @param id,当前图片地址对应的记录的主键
* 返回值说明:
* @return boolean
***********************************/
@Override
public boolean removeAdImgeById(String id) {
Ad ad = baseMapper.selectById(id);
if (null == ad) {
return false;
}
if (StringUtils.isNotEmpty(ad.getImageUrl())) {
R r = ossRemoveFileService.removeFile(ad.getImageUrl());
if (r.getSuccess()) {
int result = baseMapper.deleteById(id);
return result > 0;
}
}
return false;
}
/***********************************
* 用途说明:根据推荐位id取得广告推荐
* @param adTypeId
* 返回值说明:
* @return java.util.List<com.stu.service.cms.entity.Ad>
***********************************/
/*1、查询redis缓存中是否存在需要的数据key
2、如果不存在,从执行方法将返回值返回并存到redis
3、如果存在,从redis里读取数据*/
@Cacheable(value="index",key="'listAdTypes'")
@Override
public List<Ad> listAdTypes(String adTypeId) {
QueryWrapper<Ad> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type_id",adTypeId);
return baseMapper.selectList(queryWrapper);
}
}
作者:明
出处:https://www.cnblogs.com/konglxblog//
版权:本文版权归作者和博客园共有
转载:欢迎转载,文章中请给出原文连接,此文章仅为个人知识学习分享,否则必究法律责任