I have a problem where net.sf.ehcache.CacheManager appears returns invalid statistics.
I'm using ehcache-core v2.3.2 (latest version) with ehcache-spring-annotations.
The problem is that getMemoryStoreObjectCount returns 1 object while both getCacheHits and getCacheMisses returns 0. Isn't the total count supposed to be hits + misses ?
The unit test below should illustrate the problem (it's applied to an empty database):
#Test
public void testCache() {
Entity e = ..
dao.storeEntity(e);
dao.getEntity(e);
assertEquals(1, cache.getStatistics().getMemoryStoreObjectCount()); // ok
assertEquals(0, cache.getStatistics().getCacheHits()); // ok
assertEquals(1, cache.getStatistics().getCacheMisses()); // fails due to 0
}
For completeness I include all essential configuration:
Spring config
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
ehcache.xml
<ehcache>
<defaultCache eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
</ehcache>
dao
#Cacheable(keyGenerator=#KeyGenerator(name="StringCacheKeyGenerator"))
public Entity getEntity(Serializable key) {
return // sql ...
}
Add statistics="true" to your ehcache.xml, it's usually better to keep your configuration changes outside your code.
<ehcache>
<defaultCache ... statistics="true" />
...
</ehcache>
Found the solution to the problem by setting the following properties in net.sf.ehcache.hibernate.EhCache:
cache.setStatisticsEnabled(true);
cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
The <defaultCache ... statistics="true" /> works greatly
in contrast with the old way cache.setStatisticsEnabled(true); that needs the lower version of ehcache (core and etc).
Related
I want to use ehcache in my spring mvc web application.because my server reset every day so i want caching be permanent. do i save it in hard path? and hoe save it?
thanks.
in my dispatcher-servlet.xml i add this
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean>
and my ehcach.xml is
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="c:/tmp"/>
<defaultCache
maxElementsInMemory="500" eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LFU"/>
<cache name="mycache"
maxElementsInMemory="0"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU">
[1] <persistence strategy="localRestartable" synchronousWrites="false" />
</cache>
i add this [1] until caching be permanent and after server reset not be remove.but this exception occur Element does not allow nested elements.
also i use ehcach-core2.7.0.jar
Element <cache> does not allow nested <persistence> elements.
You should not mix legacy configuration options - attributes diskPersistent and overflowToDisk on the cache element with recommended persistence element.
However, to get to the open source disk persistent setting, you need to stick with the legacy options.
So your configuration should drop the persistence element to become:
<cache name="mycache"
maxElementsInMemory="0"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU">
</cache>
However, you should also give a meaningful value to maxElementsInMemory, so you can have a hot set of entries for which you do not need to pay the deserialization price when accessing them.
You also need to decide if you want eternal elements or have expiration. For this, remove either eternal="true" or the timeToLiveSeconds and timeToIdleSeconds pair. Having both is not an error in Ehcache for compatibility reasons, but makes it hard to know what you intended initially.
And as a last advice, I would move the cache content to a folder with a more descriptive name instead of c:/tmp.
Note that the open source disk persistent tier is not fault tolerant, so improper shutdown of the Cache or CacheManager or exceptions while doing IO can corrupt the data. If that happens, you will have to clear the data folder before you can restart your cache.
For more details, see the Ehcache 2.7 persistence documentation.
Have you tried this?
<cache eternal="true"
maxElementsInMemory="0"
name="<cache name>"
overflowToDisk="true"/>
We have a EHCache cluster, hibernate and Mysql.
Everything is working almost fine. Criteria searches are being cached and when records are modified on other members of the clusters the cached queries are updated instantly on the other servers.
however, my problem is when new records are inserted. The cached queries on that table do not know about it until the cached query are expired.
I probably have missed something on my EHcache.xml configuration, but I have no idea what could it be.
Any ideas?
EHCache.xml follows:
`
<!--<diskStore path="java.io.tmpdir"/>-->
<!-- means for cache replication -->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="connect=
TCP(bind_port=10700):
S3_PING(...):
MERGE2(max_interval=30000;min_interval=10000):
FD_SOCK(start_port=0):
FD(timeout=3000;max_tries=3):
VERIFY_SUSPECT(timeout=1500):
BARRIER():
pbcast.NAKACK(use_mcast_xmit=false;gc_lag=0;retransmit_timeout=300,600,1200,2400,4800;discard_delivered_msgs=true):
UNICAST(timeout=300,600,1200):
pbcast.STABLE(stability_delay=1000;desired_avg_gossip=50000;max_bytes=400K):
pbcast.GMS(print_local_addr=true;join_timeout=300;view_bundling=true):
FC(max_credits=2M;min_threshold=0.10):
FRAG2(frag_size=60K):
pbcast.STREAMING_STATE_TRANSFER()"
propertySeparator="::" />
<!-- default query cache to be used for all queries without an explicit cache -->
<cache
name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="100"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="false"
statistics="true">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>
<!-- timestamps of particular last update time to tables -->
<cache
name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="false"
statistics="true">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>
<!-- default cache to use for all cacheable entities without an explicit cache -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="600"
memoryStoreEvictionPolicy="LRU"
statistics="true">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />
</defaultCache>
`
I'm afraid it's a little too late for the author, but I thought my answer can be useful for anyone else with the same problem.
You should remember how the StandardQueryCache and the UpdateTimestampsCache work. When the query cache is checked for a query, the time the query was cached is compared to the timestamps of the last update of all tables in the query. If any tables were updated after the query was cached, the cached result gets discarded and the database is used instead. Timestamps of the last update for each table are stored in the UpdateTimestampsCache.
In the above configuration the values from the UpdateTimestampsCache are not copied to other members of the cluster, so Hibernate looks at the old timestamps and thinks that a cached query is up-to-date. As a result newly inserted records are neglected. To fix it simply set replicateUpdatesViaCopy for the UpdateTimestampsCache to true.
Reference: Ehcache configuration
Note that the eternal attribute, when set to "true", overrides timeToLive and timeToIdle so that no expiration can take place.
You have 1 eternal attribute setting to true. Maybe you can try setting it to false and see if it helps?
Using the same configuration, with only defaultCache, I get inconsistent data (case A). But if I add an additional cache entry will not get those errors of inconsistency (case B).
Do you know why?
I do not want to set the 130 caches that set aumaticamente defaultCache
Thanks in advance
P. S. I guess all the time that what is not specified, will EHCache as a template using defaultCache settings.
Case A:
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="0"
overflowToDisk="false"
>
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
/>
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
</defaultCache>
`
Case B:
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="0"
overflowToDisk="false"
>
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
/>
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
</defaultCache>
<cache
name="com.liferay.portal.model.impl.LayoutImpl"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="0"
overflowToDisk="false"
>
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
/>
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
</cache>
Since you haven't said how to you use cache it's hard to guess what you are really doing and what errors of inconsistency do you mean.
Maybe you are not caching query results with cached entities, which makes entities to be read from cache and queries from database? There are many possible cases.
We tried to run Liferay on the cluster env and got the same error: default config doesn't work.
I made some changes, maybe my config will help you.
hibernate-clustered.xml
liferay-multi-vm-clustered.xml
I think my Hibernate (3.5.3) Second Level Cache is well configured with EhCache (2.2).
At least, I observe following log entries:
20:15:28 DEBUG [net.sf.ehcache.Cache#searchInStoreWithStats] persistence.unit:unitName=#pu-pay.c.u.p.model.ActionCache: persistence.unit:unitName=#pu-pay.c.u.p.model.Action store hit for c.u.p.model.Action#TRT
20:15:28 DEBUG [net.sf.ehcache.Cache#searchInStoreWithStats] persistence.unit:unitName=#pu-pay.c.u.p.model.ActionCache: persistence.unit:unitName=#pu-pay.c.u.p.model.Action store hit for c.u.p.model.Action#PID
(... do these "store hits" really indicate that the cache is working?)
What puzzles me now is:
When I inspect the statistics as follows:
cacheManager.getCache("persistence.unit:unitName=#pu-pay.c.u.p.model.Action").getStatistics().toString()
... all I see is:
name = persistence.unit:unitName=#pu-pay.c.u.p.model.Action cacheHits = 0 onDiskHits = 0 inMemoryHits = 0 misses = 0 size = 0 averageGetTime = 0.0 evictionCount = 0
... no hits, no misses,... nothing...
In ehcache.xml, I have:
<defaultCache
...
statistics="true"
...
/>
<cache
name="persistence.unit:unitName=#pu-pay.c.u.p.model.Action"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
statistics="true"
/>
Does this ring a bell to any body? Do I have to enable statistics in another location as well? ...
Update:
persistence.xml specifies:
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
Weird indeed, these debug statement do mean you are having hits to the ersistence.unit:unitName=#pu-pay.c.u.p.model.Action Cacheā¦ I wonder whether your CacheManager is the right one, could it be the Hibernate Provider is using another instance ?
How does your hibernate config look like ?
Hi I've run into some problems with hibernate 2nd level cache.
As cache provider I use ehcache.
Part of config from persistence.xml
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
<property name="hibernate.cache.provider_configuration_file_resource_path" value="/ehcache.xml" />
I configure my entities using annotations so:
#Cache(region = "Kierunek", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Kierunek implements Serializable {
imports for those annotations are:
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
my ehcache.xml
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="Kierunek" maxElementsInMemory="1000"
eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />
And anyone idea why i get following error ?
WARNING: Could not find a specific ehcache configuration for cache named [persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.Kierunek]; using defaults.
19:52:57,313 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB state=Create
java.lang.IllegalArgumentException: Cache name cannot contain '/' characters.
solution is to add another property to persistence.xml
<property name="hibernate.cache.region_prefix" value=""/>
and that removes that faulty prefix big thx ruslan!
IMHO, you get the generated region name for your class. This generated name "persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje.Kierunek". And it's not defined in your's ehcache.xml configuration. Also it's looking for the predefined name, so it can't use default region.
As an option to solve this problem you can use #Cache annotation properties to predefine some region name, like
#Cache(region = 'Kierunek', usage = CacheConcurrencyStrategy.READ_WRITE)
public class Kierunek implements Serializable {
// ....
}
And in ehcache.xml
<cache name="Kierunek"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU" />
Hibernate add prefix to cache names based on appname or value of property hibernate.cache.region_prefix
If You set this property for "" (empty string) then You have regions named exactly like name in hibernate config.
EHCache needs a configuration that tells it how to cache the objects in your application (live time, cache type, cache size, caching behaviour etc). For every class you try to cache it will try to find an appropriate cache configuration and print the error above if it fails to do so.
See http://ehcache.sourceforge.net/documentation/configuration.html for how to configure EHCache.