简体   繁体   English

EHCache似乎不起作用

[英]EHCache doesn't seem to work

I've set up ehcache on our Java application, which uses Spring and Hibernate. 我已经在使用Spring和Hibernate的Java应用程序上设置了ehcache。 However, when I run Junit tests and print the stats, it seems there is nothing in cache: 但是,当我运行Junit测试并打印统计信息时,似乎缓存中没有任何内容:

OUTPUT OF CACHE MANAGER STATS ON EVERY CACHE: 每次缓存的缓存管理器统计数据输出:

COM.****.SERVICES.CLARITY.DOMAIN.ACTIONITEM.BYRESOURCEUNIQUENAME: getCacheHits: 0 getCacheMisses: 0 getObjectCount: 0 COM。****。SERVICES.CLARITY.DOMAIN.ACTIONITEM.BYRESOURCEUNIQUENAME:getCacheHits:0 getCacheMisses:0 getObjectCount:0

COM.****.SERVICES.CLARITY.DOMAIN.ACTIONITEM: getCacheHits: 0 getCacheMisses: 0 getObjectCount: 0 COM。****。SERVICES.CLARITY.DOMAIN.ACTIONITEM:getCacheHits:0 getCacheMisses:0 getObjectCount:0

COM.****.SERVICES.CLARITY.DOMAIN.RESOURCE: getCacheHits: 0 getCacheMisses: 0 getObjectCount: 0 COM。****。SERVICES.CLARITY.DOMAIN.RESOURCE:getCacheHits:0 getCacheMisses:0 getObjectCount:0

CONTENT OF THE MAPPING FILE (ONLY PARTS, TOO BIG TO PASTE ALL): 映射文件的内容(仅部分,太大而无法粘贴):

<class name="ActionItem" table="CAL_ACTION_ITEMS" mutable="false" lazy="false" >
<cache region="com.****.services.clarity.domain.ActionItem" usage="read-only" include="all" />

[...] [...]

<query name="byResourceUniqueName" cacheable="true" cache-region="com.****.services.clarity.domain.ActionItem.byResourceUniqueName" read-only="true">
    FROM ActionItem WHERE id IN (
        SELECT DISTINCT actionItemId FROM ActionItemAssignee as aia WHERE assigneeId IN (
            SELECT userId FROM Resource WHERE uniqueName = :uniqueName
        )
    )
    ORDER BY dueDate
</query>

CONTENT OF EHCACHE.XML: EHCACHE.XML的内容:

<cache
    name="com.****.services.clarity.domain.ActionItem"
    maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="0"
    timeToLiveSeconds="600" overflowToDisk="false" />

<cache
    name="com.****.services.clarity.domain.ActionItem.byResourceUniqueName"
    maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="0"
    timeToLiveSeconds="60" overflowToDisk="false" />

<defaultCache maxElementsInMemory="200" eternal="false"
    timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
    memoryStoreEvictionPolicy="LRU" />

HIBERNATE CONFIG: HIBERNATE CONFIG:

    <property name="hibernateProperties">
        <value>
            hibernate.format_sql=true
            hibernate.dialect=org.hibernate.dialect.OracleDialect
            hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
            hibernate.cache.use_query_cache=true
            hibernate.show_sql=true
        </value>
    </property>
</bean>

Any ideas on how to populate the cache ? 关于如何填充缓存的任何想法? Thanks. 谢谢。

Normally the items get populated to the cache by Hibernate automatically. 通常,这些项目会由Hibernate自动填充到缓存中。

One thing I noticed in your configuration is that you didn't enable the statistics. 我在您的配置中注意到的一件事是您没有启用统计信息。 Add the property 添加属性

hibernate.generate_statistics=true

to your session factory's configuration and see, if numbers occur in your output. 到会话工厂的配置,然后查看输出中是否出现数字。

yeah,when you use second cache in hibernate , when you will query you must set like this: 是的,当您在hibernate中使用第二个缓存时,要进行查询时,必须这样设置:

Query q=....
q.setCacheable(tre);

if you Spring you must write like this 如果你是春天,你必须这样写

getHibernateTemplate().setCacheQueries(true);

尝试使用net.sf.ehcache.hibernate.SingletonEhCacheProvider代替net.sf.ehcache.hibernate.EhCacheProvider

First of all, are you sure cache usage for ActionItem s should be read-only ? 首先,您确定ActionItem的缓存使用情况应该是read-only吗? How are the those items populated initially? 这些项目最初是如何填充的? And, even if that is correct for your business model, consider changing it to nonstrict-read-write , to see if it changes anything in your test. 而且,即使这对您的业务模型是正确的,也可以考虑将其更改为nonstrict-read-write ,以查看它在测试中是否有任何更改。

Then, have you tried executing byResourceUniqueName query with caching enabled: 然后,您是否尝试在启用缓存的情况下执行byResourceUniqueName查询:

Query q = getSession().getNamedQuery("byResourceUniqueName");
q.setCacheable(true);
List result = q.list();

Even though cacheable parameter should influence only caching of the result set of the query, again, see if it changes anything. 即使cacheable参数仅影响查询结果集的缓存,请再次查看其是否有任何变化。

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

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