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

桂林漓江阳朔优化设计四年级上册数学答案

桂林漓江阳朔,优化设计四年级上册数学答案,网站建设有什么意见,做电商网站货源ehcache使用及缓存不生效处理方法 ehcache是什么 ehcache是一个纯java的进程内的缓存框架, 具有快速,精干等特点。java中最应用广泛的一款缓存框架(java’s most widely-used cache) ehcache是一个基于标准、开源的高性能缓存,扩展简单。因…

ehcache使用及缓存不生效处理方法

ehcache是什么

ehcache是一个纯java的进程内的缓存框架, 具有快速,精干等特点。java中最应用广泛的一款缓存框架(java’s most widely-used cache)
ehcache是一个基于标准、开源的高性能缓存,扩展简单。因为它健壮、经过验证、功能齐全、方便的和第三方框架与库集成。Ehcache 从进程内缓存一直扩展到具有 TB 大小缓存的进程内/进程外混合部署。

(ehcache is an open source, standards-based cache that boosts performance, offloads your databases, and simplifies scalability, it’s the most
widely-used java-based cache because it’s robust, proven, full-featured, and integrates with other popular libraries and frameworks, ehcache
from in-prrocess caching, all the way to mixed in process/out-of process deployments with terabyte-sized caches.)

优缺点

优点:

 1.纯java开发,便于学习,集成进主流的spring boot2.不依赖中间件3.快速,简单4.缓存数据由两级: 内存和磁盘,无需担心容量问题

缺点:

 1.多节点不能同步,缓存共享麻烦2.一般在架构中应用于二级缓存(一级缓存redis ---->二级缓存ehcache  -> database)

怎么用

这里我们使用ehcache3

引入依赖包

        <!-- ehcache 缓存 --><dependency><groupId>org.ehcache</groupId><artifactId>ehcache</artifactId></dependency><dependency><groupId>javax.cache</groupId><artifactId>cache-api</artifactId></dependency>

ehcache.xml配置

<configxmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xmlns='http://www.ehcache.org/v3'xmlns:jsr107='http://www.ehcache.org/v3/jsr107'><cache-template name="heap-cache"><listeners></listeners><resources><heap unit="entries">2000</heap><offheap unit="MB">100</offheap></resources></cache-template><cache-template name="defaultCache"><heap unit="entries">100</heap></cache-template><cache alias="allViewScenesCount" uses-template="defaultCache"><expiry><ttl unit="hours">4</ttl></expiry></cache></config>

集成到springboot

ehcache集成到springboot。 springboot启动类增加注解@EnableCaching

@Configuration
public class CacheConfig {@Beanpublic CacheManager cacheManager() throws FileNotFoundException, URISyntaxException {return new JCacheCacheManager(jCacheManager());}@Beanpublic javax.cache.CacheManager jCacheManager() throws FileNotFoundException, URISyntaxException {EhcacheCachingProvider ehcacheCachingProvider = new EhcacheCachingProvider();URI uri = ResourceUtils.getURL("classpath:ehcache.xml").toURI();javax.cache.CacheManager cacheManager  = ehcacheCachingProvider.getCacheManager(uri, this.getClass().getClassLoader());return cacheManager;}
}

缓存结果

@Slf4j
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {@RequestMapping("/allViewScenesCount")public RestResult<Object> allViewScenesCount(@RequestBody StatisticsDTO statisticsDTO) {StatisticsViewAllVO viewAllVO = statisticsService.allViewScenesCount(statisticsDTO);return RestResult.ok(viewAllVO);}
}
@Service
@Slf4j
public class StatisticsService {// 这里会使用allViewScenesCount作为key, statisticsDTO的所有属性作为field, StatisticsViewAllVO的所有属性作为value// 这里注意StatisticsViewAllVO 需要实现 Serializable 接口,否则无法序列化到磁盘@Cacheable(value = "allViewScenesCount")public StatisticsViewAllVO allViewScenesCount(StatisticsDTO statisticsDTO){//do something}
}

原理

请求流程

用户发起请求---> StatisticsController.allViewScenesCount() ---> CglibAopProxy 动态代理 ---> cacheInterceptor 缓存查询与存储 ---> StatisticsService.allViewScenesCount() -->  cacheInterceptor 缓存存储  -->  CglibAopProxy 代理返回结果

CglibAopProxy(spring)
-------cacheInterceptor(ehcache)
-----------StatisticsService(我们的代码)
-------cacheInterceptor(ehcache)
CglibAopProxy(spring)		

ehcache数据结构

ehcache的数据结构:  key field  value  (类似redis的hash)key: 为注解 @Cacheable(value = "diffMoneyTrend")
filed: 为@Cacheable(value = "diffMoneyTrend", key = '')的key, 如果没有则是方法的参数

spring cglib代理

CglibAopProxyretVal = (new CglibAopProxy.CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy)).proceed();
proxy:   CglibAopProxy
target: statistiocsService
chain:  cacheInterceptor
 @Nullablepublic Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {Object oldProxy = null;boolean setProxyContext = false;Object target = null;TargetSource targetSource = this.advised.getTargetSource();Object var16;try {if (this.advised.exposeProxy) {oldProxy = AopContext.setCurrentProxy(proxy);setProxyContext = true;}// 代理目标bean,我们的statistiocsServicetarget = targetSource.getTarget();Class<?> targetClass = target != null ? target.getClass() : null;// 缓存切面List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);Object retVal;// 判断是否有其他调用链,没有,直接代理我们beanif (chain.isEmpty() && CglibAopProxy.CglibMethodInvocation.isMethodProxyCompatible(method)) {Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);retVal = CglibAopProxy.invokeMethod(target, method, argsToUse, methodProxy);} else {//有其他调用链 retVal = (new CglibAopProxy.CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy)).proceed();}// 返回结果retVal = CglibAopProxy.processReturnType(proxy, target, method, retVal);var16 = retVal;} finally {if (target != null && !targetSource.isStatic()) {targetSource.releaseTarget(target);}if (setProxyContext) {AopContext.setCurrentProxy(oldProxy);}}return var16;}

CacheAspectSupport缓存切面

	@Nullableprivate Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) {// Special handling of synchronized invocationif (contexts.isSynchronized()) {CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next();if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) {Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);Cache cache = context.getCaches().iterator().next();try {return wrapCacheValue(method, handleSynchronizedGet(invoker, key, cache));}catch (Cache.ValueRetrievalException ex) {// Directly propagate ThrowableWrapper from the invoker,// or potentially also an IllegalArgumentException etc.ReflectionUtils.rethrowRuntimeException(ex.getCause());}}else {// No caching required, only call the underlying methodreturn invokeOperation(invoker);}}// Process any early evictionsprocessCacheEvicts(contexts.get(CacheEvictOperation.class), true,CacheOperationExpressionEvaluator.NO_RESULT);// Check if we have a cached item matching the conditionsCache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class));// Collect puts from any @Cacheable miss, if no cached item is foundList<CachePutRequest> cachePutRequests = new ArrayList<>();if (cacheHit == null) {collectPutRequests(contexts.get(CacheableOperation.class),CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests);}Object cacheValue;Object returnValue;if (cacheHit != null && !hasCachePut(contexts)) {// If there are no put requests, just use the cache hit// 查到了缓存,使用缓存cacheValue = cacheHit.get();returnValue = wrapCacheValue(method, cacheValue);}else {// Invoke the method if we don't have a cache hit// 没有查到缓存,调用我们的代码,执行实际逻辑returnValue = invokeOperation(invoker);cacheValue = unwrapReturnValue(returnValue);}// Collect any explicit @CachePutscollectPutRequests(contexts.get(CachePutOperation.class), cacheValue, cachePutRequests);// Process any collected put requests, either from @CachePut or a @Cacheable missfor (CachePutRequest cachePutRequest : cachePutRequests) {cachePutRequest.apply(cacheValue);}// Process any late evictionsprocessCacheEvicts(contexts.get(CacheEvictOperation.class), false, cacheValue);return returnValue;}

//CacheAspectSupport 
private Object execute(){if (cacheHit != null && !hasCachePut(contexts)) {// If there are no put requests, just use the cache hit// 查到了缓存,使用缓存cacheValue = cacheHit.get();returnValue = wrapCacheValue(method, cacheValue);}else {// Invoke the method if we don't have a cache hit// 没有查到缓存,调用我们的代码,执行实际逻辑returnValue = invokeOperation(invoker);cacheValue = unwrapReturnValue(returnValue);}
}

@cacheable不生效,处理

注意:因为控制器调用service通过了cglib代理,只对调用生成代理方法: 因此ehcahce只对一级方法生效:如seviceA,有A,B方法, A内部调用B方法,B加@cacheable缓存不生效。

例如:

B方法注解不生效

@Slf4j
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {@RequestMapping("/allViewScenesCount")public RestResult<Object> allViewScenesCount(@RequestBody StatisticsDTO statisticsDTO) {StatisticsViewAllVO viewAllVO = statisticsService.A(statisticsDTO);return RestResult.ok(viewAllVO);}
}
@Service
@Slf4j
public class StatisticsService {public StatisticsViewAllVO A(StatisticsDTO statisticsDTO){// 这里调用不会有cglib动态代理,因此也就没有缓存切面了return b();}@Cacheable(value = "B")public StatisticsViewAllVO b(StatisticsDTO statisticsDTO){//do something}
}

修改使B方法缓存生效

@Slf4j
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {@RequestMapping("/allViewScenesCount")public RestResult<Object> allViewScenesCount(@RequestBody StatisticsDTO statisticsDTO) {StatisticsViewAllVO viewAllVO = statisticsService.A(statisticsDTO);return RestResult.ok(viewAllVO);}
}
@Service
@Slf4j
public class StatisticsService {public StatisticsViewAllVO A(StatisticsDTO statisticsDTO){// 我们强制这里用动态代理, 这里执行就会走切面了StatisticsService statisticsService = SpringUtil.getBean(StatisticsService.class);return  statisticsService.b();}@Cacheable(value = "B")public StatisticsViewAllVO b(StatisticsDTO statisticsDTO){//do something}
}

参考

https://www.jianshu.com/p/154c82073b07

https://www.ehcache.org/

https://www.ehcache.org/documentation/3.10/xml.html

http://www.lbrq.cn/news/2488915.html

相关文章:

  • 中国代理网官网重庆seo公司
  • wordpress适合做网页seo搜索引擎优化是通过优化答案
  • 网站对接微信接口智能网站推广优化
  • 淘宝找做网站株洲企业seo优化
  • 游戏网站怎么赚钱chinaz站长素材
  • 企业网站建设的上市公司电商运营seo
  • 互联网有多少网站济南网站推广
  • wordpress投稿者用户权限seo怎么优化步骤
  • 外贸公司起名seo优化
  • 遵义做网站哪家好哪家好seo优化推广技巧
  • 网站是否必须做认证山西seo排名厂家
  • 国家企业信用网企业查询标题关键词优化报价
  • 枣庄高端网站定制厦门关键词排名提升
  • 如何利用网站做产品推广旺道网站排名优化
  • 厦门网站制作seo流量的提升的软件
  • 企业网站建设百度浏览器下载
  • 电商代运营公司怎么样长沙百度网站优化
  • 网站范例自媒体十大平台
  • 萧山做网站设计抖音优化公司
  • 网站关于我们怎么做会计培训班要多少钱一般要学多久
  • 免费自己做网站手机网络推广seo是什么
  • 上海网安网站建设网站建设定制
  • 北京公司网站制作微信软文范例100字
  • 网站ftp根目录电商运营去哪里学比较好
  • 网站CDN怎么做防攻击吗电子商务网站开发
  • 管理登陆网站开发软件谷歌网站推广
  • 狗和女主人做爰网站百度标注平台怎么加入
  • 设计师互动平台完善的seo网站
  • wordpress和域名seo关键字优化软件
  • 响应式网站应该怎么做麒麟seo软件
  • [STM32][HAL]stm32wbxx 超声波测距模块实现(HY-SRF05)
  • vulkan从小白到专家——YUV处理
  • Android CameraX 使用指南:简化相机开发
  • logstash采集springboot微服务日志
  • 【LLM】Kimi-K2模型架构(MuonClip 优化器等)
  • Chessboard and Queens