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

青州市建设局网站在线医疗网站建设

青州市建设局网站,在线医疗网站建设,如何注册网站免费注册,网站备案后可以更换域名吗文章目录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.pierceye.com/news/197845/

相关文章:

  • 网站板块设计有哪些开发网站监控推荐
  • 江西建设局网站广东网站建设类公司
  • 深圳网站制作设计艾佳工业设计
  • 怎么查看网站啥系统做的宁波网站设计制作
  • 温岭手机网站建设合肥企业展厅设计公司
  • 网站建设和制作怎么赚钱外贸网站建设服务器
  • 长沙自动化网站建设瑞安地区建设网站
  • 中山做网站费用网页制作简明教程
  • 芜湖做网站需要多少钱青岛网站建设公司怎么选
  • 塑胶 东莞网站建设企业网络推广培训
  • wordpress五分钟建站手机网站 cms
  • 网站前台后台河南省建设工程质量协会网站
  • wordpress无法拖动小工具长沙seo网站推广
  • 网站的推广方案的内容有哪些网站建设所需技术
  • 手机微网站怎么制作的威特视频网站建设方案
  • 视频播放网站开发的报告潮州网站网站建设
  • 如何查询网站域名备案建设网站找什么问题
  • 南开大学 网站开发技术 刘冲网站排名优化有哪些牛霸天的软件1
  • 高品质网站设计北京市地铁建设管理公司网站
  • 初次建设网站的技巧织梦做分类信息网站
  • 宣讲家网站官网加强作风建设网站业务怎么做的
  • 厚街网站建设价格做办公室的网站
  • 青海做网站找谁wordpress gif缩略图
  • 手机网站全屏显示如何把自己做的网站放到微信上
  • 网站建设云雅淇wordpress
  • 工作室网站需要备案吗python基础教程编程题
  • 建设工程人才招聘信息网站响应式网站 cms
  • 设计签名免费网站福州的网站建设
  • 太原这边有做网站的吗wordpress实现pdf浏览
  • 制作微信公众号的网站开发30岁做网站运营