在java中使用cacheManager清除缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18693780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
clearing cache using cacheManager in java
提问by mimo
I want to clear the cache using JAVA code.
我想使用 JAVA 代码清除缓存。
and for this goal I write this code:
为了这个目标,我写了这段代码:
public void clearCache(){
CacheManager.getInstance().clearAll();
}
is this code correct? and is there a way to confirm that it works good? thanks
这段代码正确吗?有没有办法确认它运行良好?谢谢
采纳答案by Kayz
Yes, your code cleares all caches you have in your cacheManager.
The ehcache-documentation says:void clearAll()
Clears the contents of all caches in the CacheManager, but without removing any caches
是的,您的代码会清除您在 cacheManager 中的所有缓存。ehcache 文档说:void clearAll()
Clears the contents of all caches in the CacheManager, but without removing any caches
If you want to test it, you can add some elements to your cache, call clearCache()
and then try to get the values. The get()
-method should only return null
.
如果你想测试它,你可以添加一些元素到你的缓存中,调用clearCache()
然后尝试获取值。本get()
-方法应该只返回null
。
You can't add values directly in your cacheManager, it just manages the caches you declared in your configuration file. (by default it's ehcache.xml, you can get that on the ehcache homepage.) You also can add caches programmatically, even without knowing anything about the configuration.
您不能直接在 cacheManager 中添加值,它只管理您在配置文件中声明的缓存。(默认情况下它是 ehcache.xml,您可以在 ehcache 主页上获得它。)您也可以以编程方式添加缓存,即使不知道有关配置的任何信息。
CacheManager cacheManager = CacheManager.getInstance();
Ehcache cache = new Cache(cacheManager.getConfiguration().getDefaultCacheConfiguration());
cache.setName("cacheName");
cacheManager.addCache(cache);
To add a value to the cache you have to make an Element:
Element element = new Element(key, value)
and simply call cache.put(element)
.
If your cache-variable isn't visible anymore, but your cacheManager is, you can do the same with cacheManager.getCache(cacheName).put(element)
要将值添加到缓存中,您必须创建一个 Element:
Element element = new Element(key, value)
并简单地调用cache.put(element)
. 如果您的缓存变量不再可见,但您的 cacheManager 是可见的,您可以执行相同的操作cacheManager.getCache(cacheName).put(element)
I hope this helps...
我希望这有帮助...
回答by Vishnu
There are 2 ways to achieve this:
有两种方法可以实现这一点:
- Using dev-console: If your are using an enterprise version of ehcache which comes bundled with Terracotta Server Array (TSA), in the installed directory one can find the dev-console.bat or dev-console.sh based on the flavor of the OS, just run this file and connect to the TSA server ( if defaults for installation are used then you can connect to localhost:9520 ) select the named cache-manager and then in the overview tab select clear caches button and select the required caches to clear in the listing. This will clear the cache contents of the selected caches and can be verified in the sizing tab, where it shows the available memory pie chart.
- Using open source ehCache: using the cache manager api to clear cache as described in the question and then verify by using a get from the cache for a particular key is null.
- 使用 dev-console:如果您使用的是与 Terracotta Server Array (TSA) 捆绑在一起的企业版 ehcache,则可以在安装目录中找到 dev-console.bat 或 dev-console.sh 基于操作系统,只需运行此文件并连接到 TSA 服务器(如果使用默认安装,则您可以连接到 localhost:9520 )选择命名的缓存管理器,然后在概览选项卡中选择清除缓存按钮并选择所需的缓存清单中明确。这将清除所选缓存的缓存内容,并且可以在显示可用内存饼图的大小选项卡中进行验证。
- 使用开源 ehCache:使用缓存管理器 api 按照问题中的描述清除缓存,然后通过使用从缓存中获取特定键来验证是否为空。
回答by Ga???
If you know the cache name, you can retrieve it from the CacheManagerand use removeAll().
如果您知道缓存名称,则可以从CacheManager检索它并使用removeAll()。
CacheManager manager = CacheManager.getInstance();
cache = manager.getCache(cacheName);
cache.removeAll();
Your approach works, but it will clear allcaches' objects.
您的方法有效,但它会清除所有缓存的对象。