`
dato0123
  • 浏览: 914547 次
文章分类
社区版块
存档分类
最新评论

CacheManager的用法

 
阅读更多

<!-- [if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--><!-- [if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1026"/> </xml><![endif]--><!-- [if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> </o:shapelayout></xml><![endif]-->

1 CacheManager 对象的创建方式

(1)Create a singleton CacheManager using defaults, then list caches.

CacheManager.create();

String[] cacheNames = CacheManager.getInstance().getCacheNames();

(2)Create a CacheManager instance using defaults, then list caches.

CacheManager manager = new CacheManager();

String[] cacheNames = manager.getCacheNames();

(3)Create two CacheManagers, each with a different configuration, and list the caches in each.

CacheManager manager1 = new CacheManager("src/config/ehcache1.xml");

CacheManager manager2 = new CacheManager("src/config/ehcache2.xml");

String[] cacheNamesForManager1 = manager1.getCacheNames();

String[] cacheNamesForManager2 = manager2.getCacheNames();

2 Cache 配置文件的加载方式

CacheManager 的构造函数如下:

(1) 无参

CacheManager manager = new CacheManager();

(2) 通过配置文件

CacheManager manager = new CacheManager("src/config/ehcache.xml");

(3) 通过资源

URL url = getClass().getResource("/anotherconfigurationname.xml");

CacheManager manager = new CacheManager(url);

(4) 通过输入流

InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());

try {

CacheManager manager = new CacheManager(fis);

} finally {

fis.close();

}

3 、增加或删除 Cache

增加 Cache 有两种方式:

(1) 使用 CacheManager addCache(String)

CacheManager singletonManager = CacheManager.create();

singletonManager.addCache("testCache");

Cache test = singletonManager.getCache("testCache");

(2) 新增一个 Cache ,然后加到 CacheManager 中, Cache 在加入 CacheManager 之前是不能使用的

CacheManager singletonManager = CacheManager.create();

Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);

manager.addCache(memoryOnlyCache);

Cache test = singletonManager.getCache("testCache");

CachaManager 中删除 Cache

CacheManager singletonManager = CacheManager.create();

singletonManager.removeCache("sampleCache1");

4 、关闭 CacheManager

CacheManager 在使用之后应该关闭,虽然有自己的 shutdown hook ,建议在程序中手动关闭。

CacheManager.getInstance().shutdown();

分享到:
评论

相关推荐

    Springboot 2.X中Spring-cache与redis整合

    Springboot中Spring-cache与redis整合。这也是一个不错的框架,与spring的事务使用类似,只要添加一些注解方法,就可以动态的去操作缓存了,减少代码的操作。

    SpringBoot笔记-下篇.pdf

    当前方法调用使用的缓存列表(如 @Cacheable(value= {"cache1","cache2"}) ), 则有两 个cache #root.caches[0].name argument name evaluation context 方法参数的名字. 可以直接 #参数 名 ,也可以使用 #p0或#a0 ...

    flutter_cache_manager:颤振的通用缓存管理器

    缓存管理器可用于以多种方式获取文件 获取单个文件的最简单方法是调用.getSingleFile 。 var file = await DefaultCacheManager().getSingleFile&#40;url&#41;; getFileStream(url)返回一个流,第一个结果是缓存...

    Java课程实验 Spring Boot 缓存管理

    创建一个继承自org.springframework.cache.annotation.CachingConfigurerSupport的配置类,并重写其中的cacheManager()方法。 3.使用缓存注解: 在需要进行缓存的方法上添加缓存注解。例如,使用@Cacheable注解来...

    hcache:HHVMHack的Nazg缓存组件

    使用CacheManager &lt; ? hh use Nazg\HCache\Element ; use Nazg\HCache\CacheManager ; $manager = new CacheManager (); $cache = $manager -&gt; createCache( ' memcached ' ); $mc = new \Memcached ( ' mc ' );...

    springboot整合redis、mybatis以及redis和mybatis的联合使用

    springboot集成redis、mybatis 1、集成redis 2、集成mybatis 3、自定义redis KEY生成器/CacheManager来管理redis... 方式一:redis原生方法工具类(RedisService)+redis+mybatis 方式二:CacheManager+redis+mybatis

    oscache-2.4.1-full.rar

    网上已经有一个不错的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx 4、有关“用OSCache进行缓存对象”的研究 这个是我今天要说的东西。网上对于OSCache缓存Web页面很多说明和例子,但...

    node-cache-manager-mongodb:MongoDb的节点缓存存储

    新的摩卡测试( )用法示例以下示例演示了如何实现Mongodb缓存存储。单店var cacheManager = require ( 'cache-manager' ) ;var mongoStore = require ( 'cache-manager-mongodb' ) ;var mongoCache = cacheManager ...

    spymemcached-jcache:支持JCache的spymemcached

    spymemcached-jcache是​​使用spymemcached作为memcache客户端的JSR107 JCache提供程序。 特征 按缓存名称支持名称空间,缓存名称应为\ w + 添加分隔符支持,默认为$。 您可以在网址中设置分隔符,例如memcached...

    node-cache-manager-s3:S3的缓存管理器模块

    创建缓存对象并在缓存管理器中使用它: const cacheManager = require ( 'cache-manager' ) ; const s3CacheStore = new S3Cache ( { accessKey : 'AAAAAAAA' , secretKey : 'asdnbklajsndkj' , bucket : 'my-...

    springboot-cache.zip

    Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术;并支持使用JCache(JSR-107)注解简化我们开发;大家一起来学习cache的用法

    Spring Cloud Finchley SR2全套(集成Spring Gateway)

    RedisCacheManager cacheManager = RedisCacheManager.builder(factory) /* 使用自定义的缓存配置初始化一个cacheManager */ .initialCacheNames(cacheNames) /* 注意这两句的调用顺序,一定要先调用该方法设置...

    spring-request-cache

    用法只需将RequestScopedCacheManager注册为@Bean @Configuration@EnableCachingpublic class CacheConfig {@Beanpublic CacheManager requestScopedCacheManager () {RequestScopedCacheManager cacheManager = new...

    C# 常用类代码(封装)

    1.DataValidator.cs 数据验证类,提供如是否是整型数等类似的验证 ...22.UploadFile 提供上传文件时所使用的方法 23.UploadFileManager 文件上传管理类 24.ValidateCode 可以生成验证码图像的类,图像格式为gif

    node-cache-manager:Node.JS的缓存模块

    特征将任何函数包装在缓存中的简单方法。 分层缓存-数据存储在每个缓存中,并首先从优先级最高的缓存中获取。 只要具有相同的API,就可以使用任何所需的缓存。 通过100%的测试覆盖率,,和。Express.js示例请参阅以...

    LiteXCache:LiteXCache 是一种简单而强大的高性能缓存机制,将同步和异步用法与一些高级缓存用法相结合,可以帮助我们更轻松地处理缓存!

    LiteXCache LiteXCache 是一种简单而强大的高性能缓存机制,将同步和异步用法与一些高级缓存用法相结合,可以帮助我们更轻松地处理缓存! 为 ASP.NET Core(2.0 及更高版本)应用程序提供存储服务。 用于抽象缓存...

    zxframe的demo

    zxframe的demo ZxFrame为JAVA框架-支持JPA,多级缓存,读写分离,分库分表;支持通用分布式锁;配置热更新;防缓存击穿,缓存穿透,缓存雪崩; ...*....支持乐观锁 *.支持多层次缓存:本地...zxframe.cache.mgr.CacheManager

    android_cache_manager_demo:专案显示Android应用程式的简易快取和档案管理

    build.gradle文件中添加以下依赖项: implementation ' org.bitbucket.ashwin_dinesh:android_cache_manager:1.0.1 '用法缓存字符串 // save simple text in 'test.txt' file CacheManager . save...

    node-cache-manager-memcached-store:节点缓存管理器的memcached隐式

    Memcached的节点缓存管理器存储 的Memcached存储 模块可以使用其他兼容的Memcache客户端作为基础的Memcache库: 安装 从上面安装一个memcached客户端,然后安装cache-manager-memcached-store ...const cacheManager

    cache_manager:Rails Engine管理您的缓存存储

    draw do # your other routes # snipped for brevity mount CacheManager :: Engine =&gt; "/cache_manager"end用法安装完成后,您可以通过浏览器访问它 这将带您进入缓存管理器的“登陆”页面。 提供有关您的缓存统计...

Global site tag (gtag.js) - Google Analytics