简体   繁体   English

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

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

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

So to achieve this we implemented a new method which 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 在缓存结果方面做得很差。 EHCache used the method parameters as entry point to the cache. 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 also a no go.这个想法是我在 find(list of keys) 方法中获取参数,执行一个大型 SQL 查询,然后将结果填充到缓存中,我没有把头放在它周围,但是写下来之后感觉就像手动修改缓存也是不行的。

Any insight or hints are appreciated!任何见解或提示表示赞赏!

也许是KeyInCache

It is possible to access the hibernate statistics + ehcache stats etc via jmx.可以通过 jmx 访问休眠统计信息 + ehcache 统计信息等。 EhcacheHibernateMBean is the main interface that exposes all the API's via jmx. EhcacheHibernateMBean 是通过 jmx 公开所有 API 的主要接口。 It basically extends two interfaces -- EhcacheStats and HibernateStats.它基本上扩展了两个接口——EhcacheStats 和 HibernateStats。 And as the name implies EhcacheStats contains methods related with Ehcache and HibernateStats related with Hibernate.顾名思义,EhcacheStats 包含与 Ehcache 相关的方法和与 Hibernate 相关的 HibernateStats。 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.您可以查看缓存命中/未命中/放置率、动态更改配置元素值——例如 maxElementInMemory、TTI、TTL、启用/禁用统计信息收集等以及其他各种内容。 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这可以通过在 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。

在此处输入图片说明

Note : The view has changed with JDK1.7 .注意:该视图在 JDK1.7 中发生了变化。 After logging into JMX Console, navigate to net.sf.ehcache.hibernate under Mbeans tab.登录 JMX 控制台后,导航到 Mbeans 选项卡下的 net.sf.ehcache.hibernate。 Click on the CacheRegionStats Clicking on it will open bring up the screen on the right.单击 CacheRegionStats 单击它会打开右侧的屏幕。 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.您必须在表格导航中导航才能找到您感兴趣的任何对象的数量。

Have you looked at SelfPopulatingCache and or BlockingCache ?你看过SelfPopulatingCache and or BlockingCache吗? They will block all secondary requesters after a db retrieve is initiated until the cache is populated在启动数据库检索后,它们将阻止所有辅助请求者,直到填充缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM