当前位置: 首页 > news >正文

php网站开发师中国一级爱做电影网站

php网站开发师,中国一级爱做电影网站,网站建设自建与租用区别,网站的建设步骤文章目录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);}}
http://www.huolong8.cn/news/115343/

相关文章:

  • 手机网站使用微信支付wordpress虚拟资源交易平台
  • 长沙响应式网站设计有哪些app制作教程课件
  • 建一个营销网站多少钱网站主题风格有哪些
  • 阜宁网站制作服务商全国哪个县网站做的最好
  • 石家庄网站建设招商怎样建淘宝客网站
  • 网站备案需要什么条件网站建设基本完成
  • 民宿网站开发dfd图网站设计背景怎么写
  • 做肯德基玻璃门网站铜山区规划建设局网站
  • 建设银行网站官网登录短信验证专业的上海网站建设公司
  • 易语言可以做网站么请问网络维护有前途吗
  • 网站建设佛化妆品手机端网站模板
  • 做平面设计兼职的网站logo制作软件哪个好
  • 建设银行网站能不能注销卡wordpress调取循环文章的图片
  • 贷款网站怎么做的邯郸国外网站建设费用
  • 网站建设代码生成器郑州百姓网征婚交友
  • 装修效果图在线设计苏州网站关键词优化
  • 那个网站是专门做机械设备怎么建自己的销售网站
  • html做静态网站手游折扣平台app哪个好
  • 阐述企业搭建网站的重要性建设了湛江市志愿服务网站
  • 建设微网站中国上市公司排行榜
  • 电子商务网站建设与运维论文潍坊网站制作保定公司电话
  • 网站建设与维护试卷第九章建网站 是否 数据库
  • 企业网站 html模板如何做单页网站
  • 郑州正规的网站制作价钱关于网站排名优化需要怎么做
  • 宁波网站建设网页设计服装设计专业大学世界排名
  • 沈阳网站营销推广网站建1设公司
  • 重庆商务网站建设什么是网络营销设计
  • 壁纸网站设计制作专业项目建设备案网站
  • 厦门酒店网站建设泰安网站开发
  • 秘鲁网站后缀一个主机可以建设多少个网站