java EHCache 如何检查缓存中是否有内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6439476/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:53:55  来源:igfitidea点击:

EHCache how to check if something is in the cache or not?

javaspringcachingehcache

提问by Marco

Is there a way of checking if an object is inside an EHCache managed cache?

有没有办法检查对象是否在 EHCache 托管缓存内?

The challenge i face is that i have implemented a method that retrieves a single value from the database (a find(key) method). The result of that find method is nicely cached by EHCache, but now i want to reduce the number of sql queries that result from calling the method several times.

我面临的挑战是我已经实现了一种从数据库中检索单个值的方法(一个 find(key) 方法)。EHCache 很好地缓存了该 find 方法的结果,但现在我想减少多次调用该方法导致的 sql 查询的数量。

So the achieve this we implemented a new method wich as argument takes a list of keys, but as the argument is different for every method call EHCache does a bad job on caching the results. EHCache used the method parameters as entry point to the cache.

因此,为了实现这一点,我们实现了一个新方法,因为参数采用键列表,但是由于每个方法调用的参数不同,所以 EHCache 在缓存结果方面做得很差。EHCache 使用方法参数作为缓存的入口点。

So i would like to re-engineer some stuff. The idea was that i take the arguments in the find(list of keys) method, execute a large sql query and then stuff the results inside the cache, i have not wrapped my head around it, but after writing this down it feels like manually modifying the cache is als a no go..

所以我想重新设计一些东西。这个想法是我在 find(list of keys) 方法中获取参数,执行一个大的 sql 查询,然后将结果填充到缓存中,我没有把我的头放在它周围,但是写下来之后感觉就像手动修改缓存也是不行的..

Any insight or hints are appreciated!

任何见解或提示表示赞赏!

采纳答案by jtahlborn

perhaps isKeyInCache?

也许是KeyInCache

回答by vsingh

It is possible to access the hibernate statistics + ehcache stats etc via jmx. EhcacheHibernateMBean is the main interface that exposes all the API's via jmx. It basically extends two interfaces -- EhcacheStats and HibernateStats. And as the name implies EhcacheStats contains methods related with Ehcache and HibernateStats related with Hibernate. You can see cache hit/miss/put rates, change config element values dynamically -- like maxElementInMemory, TTI, TTL, enable/disable statistics collection etc and various other things. This can be achieved in your application by overriding buildSessionFactory() method on LocalSessionFactoryBean by adding tc.active as "true" System property when second level cache is enabled in Hibernate configuration

可以通过 jmx 访问休眠统计信息 + ehcache 统计信息等。EhcacheHibernateMBean 是通过 jmx 公开所有 API 的主要接口。它基本上扩展了两个接口——EhcacheStats 和 HibernateStats。顾名思义,EhcacheStats 包含与 Ehcache 相关的方法和与 Hibernate 相关的 HibernateStats。您可以查看缓存命中/未命中/放置率、动态更改配置元素值——例如 maxElementInMemory、TTI、TTL、启用/禁用统计信息收集等以及其他各种内容。这可以通过在 Hibernate 配置中启用二级缓存时添加 tc.active 为“true”系统属性来覆盖 LocalSessionFactoryBean 上的 buildSessionFactory() 方法,从而在您的应用程序中实现

  @Override
        protected SessionFactory buildSessionFactory() throws Exception {
                Properties properties = this.getHibernateProperties();
                String secondLevelCache = (String) properties
                                .get("hibernate.cache.use_second_level_cache");
                if (secondLevelCache.equals("true")) {
                        System.setProperty("tc.active", "true");
                }
                return super.buildSessionFactory();
        }

No when you access your application via JMX, go to tab Mbeans , on left go to net.sf.ehcache.hibernate --> net.sf.ehcache.Cachemanager@..

不,当您通过 JMX 访问应用程序时,转到选项卡 Mbeans ,在左侧转到 net.sf.ehcache.hibernate --> net.sf.ehcache.Cachemanager@..

Under this go to attributes. Click on attributes and on right side, inspect RegionCacheAttriutes.

在此下转到属性。单击属性并在右侧检查 RegionCacheAttriutes。

enter image description here

在此处输入图片说明

Note: The view has changed with JDK1.7 . After logging into JMX Console, navigate to net.sf.ehcache.hibernate under Mbeans tab. Click on the CacheRegionStats Clicking on it will open bring up the screen on the right. Double click on top section and it brings up the tabular navigation as shown below. You will have to navigate in the tabular navigation to find the count of any object you are interested in.

注意:该视图在 JDK1.7 中发生了变化。登录 JMX 控制台后,导航到 Mbeans 选项卡下的 net.sf.ehcache.hibernate。单击 CacheRegionStats 单击它会打开右侧的屏幕。双击顶部,它会显示如下所示的表格导航。您必须在表格导航中导航才能找到您感兴趣的任何对象的数量。

回答by Steven Harris

Have you looked at SelfPopulatingCache and or BlockingCache? They will block all secondary requesters after a db retrieve is initiated until the cache is populated

你看了SelfPopulatingCache and or BlockingCache吗?在启动数据库检索后,它们将阻止所有辅助请求者,直到填充缓存