青州市建设局网站,在线医疗网站建设,如何注册网站免费注册,网站备案后可以更换域名吗文章目录SpringCache简介常⽤注解Cacheable自定义CacheManager配置和过期时间自定义缓存KeyGenerator常用注解CachePut 和 CacheEvict多注解组合CachingSpringCache简介 ⽂档#xff1a;https://spring.io/guides/gs/caching/ ⾃Spring 3.1起#xff0c;提供了类似于Transact…
文章目录SpringCache简介常⽤注解Cacheable自定义CacheManager配置和过期时间自定义缓存KeyGenerator常用注解CachePut 和 CacheEvict多注解组合CachingSpringCache简介 ⽂档https://spring.io/guides/gs/caching/ ⾃Spring 3.1起提供了类似于Transactional注解事务的注解Cache⽀持且提供了Cache抽象提供基本的Cache抽象⽅便切换各种底层Cache只需要更少的代码就可以完成业务数据的缓存提供事务回滚时也⾃动回滚缓存⽀持⽐较复杂的缓存逻辑 核⼼ ⼀个是Cache接⼝缓存操作的API ⼀个是CacheManager管理各类缓存有多个缓存 项⽬中引⼊starter依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId
/dependency配置⽂件指定缓存类型
spring:cache:type: redis启动类开启缓存注解
EnableCaching常⽤注解Cacheable 标记在⼀个⽅法上也可以标记在⼀个类上缓存标注对象的返回结果标注在⽅法上缓存该⽅法的返回值标注在类上缓存该类所有的⽅法返回值value 缓存名称可以有多个key 缓存的key规则可以⽤springEL表达式默认是⽅法参数组合 condition 缓存条件使⽤springEL编写返回true才缓存 import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.xdclassredis.dao.ProductMapper;
import net.xdclass.xdclassredis.model.ProductDO;
import net.xdclass.xdclassredis.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;Service
public class ProductServiceImpl implements ProductService {Autowiredprivate ProductMapper productMapper;OverrideCacheable(value {product},key #root.args[0])public ProductDO findById(int id) {return productMapper.selectById(id);}OverrideCacheable(value {product_page},key #root.methodName_#page_#size)public MapString, Object page(int page, int size) {Page pageInfo new Page(page,size);IPageProductDO iPage productMapper.selectPage(pageInfo,null);MapString,Object pageMap new HashMap(3);pageMap.put(total_record,iPage.getTotal());pageMap.put(total_page,iPage.getPages());pageMap.put(current_data,iPage.getRecords());return pageMap;}}
自定义CacheManager配置和过期时间
默认为Primary注解所标注的CacheManager
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;import java.lang.reflect.Method;
import java.time.Duration;Configuration
public class AppConfiguration {/*** 新的分页插件*/Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}/*** 1分钟过期** param connectionFactory* return*/Beanpublic RedisCacheManager cacheManager1Minute(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration config instanceConfig(60L);return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).transactionAware().build();}/*** 默认是1小时** param connectionFactory* return*/BeanPrimarypublic RedisCacheManager cacheManager1Hour(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration config instanceConfig(3600L);return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).transactionAware().build();}/*** 1天过期** param connectionFactory* return*/Beanpublic RedisCacheManager cacheManager1Day(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration config instanceConfig(3600 * 24L);return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).transactionAware().build();}private RedisCacheConfiguration instanceConfig(Long ttl) {Jackson2JsonRedisSerializerObject jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper new ObjectMapper();objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);objectMapper.registerModule(new JavaTimeModule());// 去掉各种JsonSerialize注解的解析objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);// 只针对非空的值进行序列化objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);// 将类型序列化到属性json字符串中objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);return RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(ttl))//.disableCachingNullValues().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));}}
自定义缓存KeyGenerator
Configuration
public class AppConfiguration {/*** 自定义缓存key规则* return*/Beanpublic KeyGenerator springCacheCustomKeyGenerator() {return new KeyGenerator() {Overridepublic Object generate(Object o, Method method, Object... objects) {String key o.getClass().getSimpleName() _ method.getName() _ StringUtils.arrayToDelimitedString(objects, _);System.out.println(key);return key;}};}} key 属性和keyGenerator属性只能⼆选⼀ CacheManager和keyGenerator使用
Service
public class ProductServiceImpl implements ProductService {Autowiredprivate ProductMapper productMapper;OverrideCacheable(value {product}, keyGenerator springCacheCustomKeyGenerator,cacheManager cacheManager1Minute)public ProductDO findById(int id) {return productMapper.selectById(id);}OverrideCacheable(value {product_page},keyGenerator springCacheCustomKeyGenerator)public MapString, Object page(int page, int size) {Page pageInfo new Page(page,size);IPageProductDO iPage productMapper.selectPage(pageInfo,null);MapString,Object pageMap new HashMap(3);pageMap.put(total_record,iPage.getTotal());pageMap.put(total_page,iPage.getPages());pageMap.put(current_data,iPage.getRecords());return pageMap;}}
常用注解CachePut 和 CacheEvict
CachePut 根据方法的请求参数对其结果进行缓存每次都会触发真实⽅法的调⽤value 缓存名称可以有多个key 缓存的key规则可以⽤springEL表达式默认是⽅法参数组合condition 缓存条件使⽤springEL编写返回true才缓存 CacheEvict 从缓存中移除相应数据, 触发缓存删除的操作value 缓存名称可以有多个CachePut(value {“product”},key “#productDO.id”)key 缓存的key规则可以⽤springEL表达式默认是⽅法参数组合 beforeInvocation false 缓存的清除是否在⽅法之前执⾏ ,默认代表缓存清除操作是在⽅法执⾏之后执⾏如果出现异常缓存就不会清除 beforeInvocation true 代表清除缓存操作是在⽅法运⾏之前执⾏⽆论⽅法是否出现异常缓存都清除 import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.xdclassredis.dao.ProductMapper;
import net.xdclass.xdclassredis.model.ProductDO;
import net.xdclass.xdclassredis.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;Service
public class ProductServiceImpl implements ProductService {Autowiredprivate ProductMapper productMapper;OverrideCacheEvict(value {product},key #root.args[0])public int delById(int id) {return productMapper.deleteById(id);}OverrideCachePut(value {product},key#productDO.id, cacheManager cacheManager1Minute)public ProductDO updateById(ProductDO productDO) {productMapper.updateById(productDO);return productDO;}
多注解组合Caching 组合多个Cache注解使⽤允许在同⼀⽅法上使⽤多个嵌套的Cacheable、CachePut和CacheEvict注释 import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.xdclassredis.dao.ProductMapper;
import net.xdclass.xdclassredis.model.ProductDO;
import net.xdclass.xdclassredis.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;Service
public class ProductServiceImpl implements ProductService {Autowiredprivate ProductMapper productMapper;OverrideCaching(cacheable {Cacheable(value {product},key #root.args[0]),Cacheable(value {product},key xdclass_#root.args[0])},put {CachePut(value {product_test},key#id, cacheManager cacheManager1Minute)})public ProductDO findById(int id) {return productMapper.selectById(id);}}