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

西安高科鱼化建设有限公司网站网页设计阶段

西安高科鱼化建设有限公司网站,网页设计阶段,百度怎么做公司的网站,杭州seo相关网站目录 Caffeine工具类方式 SpringBoot 整合 Caffeine 缓存 #xff08;SpringCache模式#xff09; 驱逐策略 开发使用 Caffeine是一种高性能的缓存库#xff0c;是基于Java 8的最佳#xff08;最优#xff09;缓存框架#xff0c;性能各方面优于guava。 Caffeine工具… 目录 Caffeine工具类方式 SpringBoot 整合 Caffeine 缓存 SpringCache模式 驱逐策略 开发使用 Caffeine是一种高性能的缓存库是基于Java 8的最佳最优缓存框架性能各方面优于guava。 Caffeine工具类方式 原文链接https://www.cnblogs.com/wudiffs/p/11585757.html 代码仓库如下 dependencygroupIdcom.github.ben-manes.caffeine/groupIdartifactIdcaffeine/artifactIdversion2.4.0/version /dependency 代码详细示例如下 public class CaffeineCacheManagerService {private static LoadingCacheString, CacheVO cache;private static AsyncLoadingCacheString, CacheVO asyncCache;private static AsyncLoadingCacheString, CacheVO asyncCache1;private static ExecutorService executorService new ThreadPoolExecutor(8, 8, 8, TimeUnit.SECONDS, newLinkedBlockingQueueRunnable(1204));static {cache Caffeine.newBuilder()// 初始化缓存长度.initialCapacity(1024 * 10)// 最大长度.maximumSize(1024 * 10)// 更新策略.refreshAfterWrite(10, TimeUnit.SECONDS)// 设置缓存的过期时间.expireAfterWrite(10, TimeUnit.SECONDS).build(new CacheLoaderString, CacheVO() {// 同步加载CheckForNullOverridepublic CacheVO load(Nonnull String key) throws Exception {return createCacheVO(key);}// getAll将会对缓存中没有值的key分别调用CacheLoader.load方法来构建缓存的值。// 我们可以重写CacheLoader.loadAll方法来提高getAll的效率。NonnullOverridepublic MapString, CacheVO loadAll(Nonnull Iterable? extends String keys) throws Exception {return createBatchCacheVOs(keys);}});// 异步加载 同步load写法最后也会转异步asyncCache Caffeine.newBuilder().maximumSize(1024 * 10).expireAfterWrite(10, TimeUnit.SECONDS).buildAsync(new CacheLoaderString, CacheVO() {CheckForNullOverridepublic CacheVO load(Nonnull String key) throws Exception {return createCacheVO(key);}NonnullOverridepublic MapString, CacheVO loadAll(Nonnull Iterable? extends String keys) {return createBatchCacheVOs(keys);}});// 异步加载 异步load写法asyncCache1 Caffeine.newBuilder().maximumSize(1024 * 10).expireAfterWrite(10, TimeUnit.SECONDS).buildAsync(new AsyncCacheLoaderString, CacheVO() {NonnullOverridepublic CompletableFutureCacheVO asyncLoad(Nonnull String key, Nonnull Executor executor) {return asyncCreateCacheVO(key, executor);}NonnullOverridepublic CompletableFutureMapString, CacheVO asyncLoadAll(Nonnull Iterable? extends String keys, Nonnull Executor executor) {return asyncCreateBatchCacheVOs(keys, executor);}});}public static CompletableFutureCacheVO asyncCreateCacheVO(String key, Executor executor) {return CompletableFuture.supplyAsync(() - createCacheVO(key), executor);}public static CompletableFutureMapString, CacheVO asyncCreateBatchCacheVOs(Iterable? extends String keys, Executor executor) {return CompletableFuture.supplyAsync(() - createBatchCacheVOs(keys), executor);}public static CacheVO createCacheVO(String key) {return new CacheVO(key);}public static MapString, CacheVO createBatchCacheVOs(Iterable? extends String keys) {MapString, CacheVO result new HashMap();for (String key : keys) {result.put(key, new CacheVO(key));}return result;}public static void main(String[] args) throws Exception {CacheVO cacheVO1 cache.get(AA);ListString list new ArrayList();list.add(BB);list.add(CC);MapString, CacheVO map cache.getAll(list);// 如果有缓存则返回否则运算、缓存、然后返回,整个过程是阻塞的// 即使多个线程同时请求该值也只会调用一次Function方法CacheVO cacheVO2 cache.get(DD, (k) - createCacheVO(k));System.out.println(JSON.toJSONString(cacheVO2));// 单个清除cache.invalidate(AA);// 批量清除cache.invalidateAll(list);// 全部清除cache.invalidateAll();// 返回一个CompletableFutureCompletableFutureCacheVO future asyncCache.get(EE);CacheVO asyncCacheVO future.get();System.out.println(JSON.toJSONString(asyncCacheVO));// 返回一个CompletableFutureMAPCompletableFutureMapString, CacheVO allFuture asyncCache.getAll(list);MapString, CacheVO asyncMap allFuture.get();System.out.println(JSON.toJSONString(asyncMap));CompletableFutureCacheVO future1 asyncCache1.get(FF);CacheVO asyncCacheVO1 future1.get();System.out.println(JSON.toJSONString(asyncCacheVO1));CompletableFutureMapString, CacheVO allFuture1 asyncCache1.getAll(list);MapString, CacheVO asyncMap1 allFuture.get();System.out.println(JSON.toJSONString(asyncMap1));}}或者使用下发方式实现Caffeine 工具类 支持同步、异步读写缓存实现 import com.github.benmanes.caffeine.cache.AsyncCache; import com.github.benmanes.caffeine.cache.Caffeine;import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit;public class CaffeineCacheUtils {private static com.github.benmanes.caffeine.cache.CacheObject, Object syncCache;private static AsyncCacheObject, Object asyncCache;private CaffeineCacheUtils() {}public static void initCache() {syncCache Caffeine.newBuilder().initialCapacity(100).maximumSize(1000).expireAfterWrite(30, TimeUnit.MINUTES).build();asyncCache Caffeine.newBuilder().initialCapacity(100).maximumSize(1000).expireAfterWrite(30, TimeUnit.MINUTES).buildAsync();}public static void putSync(Object key, Object value) {syncCache.put(key, value);}public static Object getSync(Object key) {return syncCache.getIfPresent(key);}public static CompletableFutureObject getAsync(Object key, Executor executor) {return asyncCache.get(key, k - CompletableFuture.supplyAsync(() - fetchDataFromDataSource(k), executor));}public static CompletableFutureVoid putAsync(Object key, Object value, Executor executor) {return asyncCache.put(key, CompletableFuture.completedFuture(value), executor);}public static void removeSync(Object key) {syncCache.invalidate(key);}public static void clearSync() {syncCache.invalidateAll();}private static Object fetchDataFromDataSource(Object key) {// 模拟从数据源获取数据的操作// 这里可以根据具体业务需求进行实现return null;} } SpringBoot 整合 Caffeine 缓存 SpringCache模式 原文链接https://blog.csdn.net/Listening_Wind/article/details/110085228 添加依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId /dependency dependencygroupIdcom.github.ben-manes.caffeine/groupIdartifactIdcaffeine/artifactIdversion2.6.2/version /dependency 缓存配置 如果使用了多个cahce比如redis、caffeine等必须指定某一个CacheManage为primary import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import org.assertj.core.util.Lists; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary;import java.util.ArrayList; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit;/*** Author: Wxy* Date: 2020/11/7 16:56* Description*/ Configuration EnableCaching // 开启缓存否则无效 public class CaffeineConfig {/*** 创建基于Caffeine的Cache Manager** return*/BeanPrimarypublic CacheManager caffeineCacheManager() {SimpleCacheManager cacheManager new SimpleCacheManager();ArrayListCaffeineCache caches Lists.newArrayList();MapString, Object map getCacheType();for (String name : map.keySet()) {caches.add(new CaffeineCache(name, (CacheObject, Object) map.get(name)));}cacheManager.setCaches(caches);return cacheManager;}/*** 初始化自定义缓存策略** return*/private static MapString, Object getCacheType() {MapString, Object map new ConcurrentHashMap();map.put(name1, Caffeine.newBuilder().recordStats().expireAfterWrite(10, TimeUnit.SECONDS).maximumSize(100).build());map.put(name2, Caffeine.newBuilder().recordStats().expireAfterWrite(50, TimeUnit.SECONDS).maximumSize(50).build());return map;} } 驱逐策略 基于大小的回收策略有两种方式基于缓存大小基于权重基于时间。 maximumSize 根据缓存的计数进行驱逐 同一缓存策略 缓存的数据量以访问先后顺序以最大100为例超出100驱逐最晚访问的数据缓存。 maximumWeight 根据缓存的权重来进行驱逐权重只是用于确定缓存大小不会用于决定该缓存是否被驱逐。 maximumWeight与maximumSize不可以同时使用。 Caffeine提供了三种定时驱逐策略 expireAfterAccess(long, TimeUnit):在最后一次访问或者写入后开始计时在指定的时间后过期。假如一直有请求访问该key那么这个缓存将一直不会过期。 expireAfterWrite(long, TimeUnit): 在最后一次写入缓存后开始计时在指定的时间后过期。 expireAfter(Expiry): 自定义策略过期时间由Expiry实现独自计算。 缓存的删除策略使用的是惰性删除和定时删除。这两个删除策略的时间复杂度都是O(1) 开发使用 主要基于Spring缓存注解Cacheable、CacheEvict、CachePut的方式使用 Cacheable 改注解修饰的方法若不存在缓存则执行方法并将结果写入缓存若存在缓存则不执行方法直接返回缓存结果。CachePut 执行方法更新缓存该注解下的方法始终会被执行。CacheEvict 删除缓存Caching 将多个缓存组合在一个方法上该注解可以允许一个方法同时设置多个注解CacheConfig 在类级别设置一些缓存相关的共同配置与其它缓存配合使用 注意 Cacheable 默认使用标primary 注释的CacheManage /*** 先查缓存如果查不到执行方法体并将结果写入缓存若查到不执行方法体直接返回缓存结果* param id*/ Cacheable(value name1, key #id, sync true) public void getUser(long id){//TODO 查找数据库 }/*** 更新缓存每次都会执行方法体* param user*/ CachePut(value name1, key #user.id) public void saveUser(User user){//todo 保存数据库 }/*** 删除* param user*/ CacheEvict(value name1,key #user.id) public void delUser(User user){//todo 保存数据库 }参考博客https://www.cnblogs.com/wudiffs/p/11585757.html (23条消息) SpringBoot 集成 Caffeine咖啡因最优秀的本地缓存_springboot caffeine_Listening_Wind的博客-CSDN博客https://blog.csdn.net/Listening_Wind/article/details/110085228
http://www.tj-hxxt.cn/news/218187.html

相关文章:

  • 普象工业设计网站佛山新网站建设代理商
  • 做网站徐州品牌推广策略有哪些
  • 做网站服务器软文写作范例大全
  • 广州建设厅官方网站wordpress post fonts
  • 苏州网站制作公司排名机械建设网站制作
  • 杨浦网站建设 网站外包打开百度一下网页版
  • 上海网站推广有哪些wordpress 更改模板路径
  • 中国建设报官方网站怎么做企业官网
  • 西安网站开发公司怎么选python制作的网站
  • 网站模板下载模板下载最新黑帽seo教程
  • 重庆手机网站方案设计中国网站排名前100
  • 程序员和网站建设wordpress写文章更新失败
  • 网站开发需要几个人亚马逊雨林十大恐怖动物
  • 什么是网站开发框架南宁企业自助建站系统
  • 做网站客户给不了素材一个人的网站建设
  • 深一网站建设招聘百度在线
  • 商标注册 网站建设如何入账网站建设公司江苏
  • 手机网站底部电话图片编辑软件
  • 最好的网站建设组织阳谷网站建设价格
  • 郑州手机网站制作公司赣州微网站建设费用
  • dw旅游网站怎么做柳市网
  • 网站广告图片在线制作wordpress移动导航菜单
  • 建设局网站更改法人所需材料百度推广公司地址
  • 婚纱摄影网站定制姑苏区最新通告
  • 先注册域名后建设网站可以吗青岛城阳网站开发
  • 青岛网站定制开发网站版权问题
  • 怎么选一个适合自己的网站公司网站制作申请报告
  • 做大数据和网站开发的前景wordpress可以做手机网
  • 做会议活动的网站越秀做网站
  • wwe中文官网站a00000网站建设丽丽