[Logo] Terracotta Discussion Forums (LEGACY READ-ONLY ARCHIVE)
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
How to get cache "SimplePageCachingFilter" reference in web app?  XML
Forum Index -> Ehcache
Author Message
vivi715

neo

Joined: 07/04/2011 22:25:39
Messages: 3
Offline

Hi,

I managed to make ehcache web work in my web app. cache is writen to my disk.

I wonder if there is a way to get reference of cache "SimplePageCachingFilter" or cachemanager which manages cache "SimplePageCachingFilter".

If it is available, I would be able to check statistics of web cache.

My application is spring based, and ehCacheManager is configured as following

<bean id="ehCacheManager" class="net.sf.ehcache.CacheManager">
<constructor-arg value="E:\tomcat\webtest\lib\ehcache.xml" />
</bean>

In my program, I iterate all caches by ehCacheManager. I can see there is a cache which cache name is SimplePageCachingFilter. But If I print out how many elements (keys) in that cache, number always is 0. even I can see cache was writen to disk already. So I thought there might be a different echachemanager for web cache? If so ,how do I get that reference?


Thanks!

Vivi
ari

seraphim

Joined: 05/24/2006 14:23:21
Messages: 1665
Location: San Francisco, CA
Offline

Try the Ehcache Monitor...it will just _show_ you the statistics you are looking for graphically. Disclaimer: free for dev; paid for prod.

Cheers,

--Ari
[WWW]
gkeim

ophanim

Joined: 12/05/2006 10:22:37
Messages: 685
Location: Terracotta, Inc.
Offline

Did you enable statistics in your config?

<cache statistics="true"/>

Gary Keim (terracotta developer) Want to post to this forum? Join the Terracotta Community
vivi715

neo

Joined: 07/04/2011 22:25:39
Messages: 3
Offline

I am using echcache web with spring. In my simple CachingFilter, the getCacheManager define as following:

protected net.sf.ehcache.CacheManager getCacheManager() {

return CacheManager.getInstance();
}

It works fine, but only problem is that, I do not know how to get reference of that CacheManager. If it is avaialbe, I would be able to manage elements in cache programmatically.

Any one know how to do it?


So I defined one ehCacheManager bean and inject into CachingFilter

<bean id="ehCacheManager" class="net.sf.ehcache.CacheManager">
<constructor-arg value="E:\tomcat\test\webapps\ROOT\WEB-INF\ehcache.xml" />
</bean>



public class PageFilter extends CachingFilter{

public static final String DEFAULT_CACHE_NAME = "OSPageFilter";
public CacheManager dataManager;

protected net.sf.ehcache.CacheManager getCacheManager() {
return this.getDataManager();*/
}

As a result, web cache still work, I can see cache saved to disk. But I tried to get cache by dataManager, cache size always is 0. I am not sure what is the problem.

thanks
gkeim

ophanim

Joined: 12/05/2006 10:22:37
Messages: 685
Location: Terracotta, Inc.
Offline

I am using echcache web with spring. In my simple CachingFilter, the getCacheManager define as following:

protected net.sf.ehcache.CacheManager getCacheManager() {

return CacheManager.getInstance();
}

It works fine, but only problem is that, I do not know how to get reference of that CacheManager. If it is avaialbe, I would be able to manage elements in cache programmatically.

Any one know how to do it?
 


We must be having a language problem because what is quoted above doesn't make sense. Maybe try restating what you mean by "I do not know how to get reference of that CacheManager" because that method gives you the reference.

Another thing you can do is to show more of the details of your app/config so we can have more context.

Gary Keim (terracotta developer) Want to post to this forum? Join the Terracotta Community
vivi715

neo

Joined: 07/04/2011 22:25:39
Messages: 3
Offline

sorry gkeim,

I Implemented a filter called OSPageFilter.

Code:
package com.test.filter;
 
 import javax.servlet.http.HttpServletRequest;
 
 import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.constructs.web.filter.CachingFilter;
 
 public class OSPageFilter extends CachingFilter{
 
 	 public static final String NAME = "OSPageFilter";
 	 public CacheManager dataManager;
 	 
 	 protected String getCacheName() {
 		                return NAME;
 	 }
 	 
 	 protected CacheManager getCacheManager() {
 
 		 return CacheManager.getInstance();
 	 }
 
 	 protected String calculateKey(HttpServletRequest request) {
 		
 		return (request.getRequestURI().endsWith("/")?request.getRequestURI():request.getRequestURI()+"/");
 	     
          }
 	 
 }



In ehcache.xml I have:

Code:
<ehcache>
   <diskStore path="e:\cache2\"/>
   <defaultCache
       maxElementsInMemory="500"
       eternal="true"
       overflowToDisk="true"
       memoryStoreEvictionPolicy="LFU" />
     <cache name="OSPageFilter"
            maxElementsInMemory="5"
            maxElementsOnDisk="1000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="3000"
            timeToLiveSeconds="6000"
            memoryStoreEvictionPolicy="LFU"
             />
 </ehcache>


In web.xml:

<filter>
<filter-name>OSPageFilter</filter-name>
<filter-class>ccom.test.filter.OSPageFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>OSPageFilter</filter-name>
<url-pattern>/test/*</url-pattern>
</filter-mapping>


My question is, how can I get same CacheManager instance which is created in CachingFilter ? In web app, I want use it to manage elements in cache, also print out how many elements in cache OSPageFilter. I need to delete specific page cache in OSPageFilter cache programmatically instead of waiting it to be expired.

In addition, do I have to put echache.xml in the lib folder of tomcat? if I put it in webapp path\ROOT\WEB-INF, the web can not start up. I got following exception:

Code:
Exception starting filter OSPageFilter
 javax.servlet.ServletException: Could not initialise servlet filter.
 	....
 
 	at java.lang.reflect.Method.invoke(Method.java:597)
 	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
 Caused by: java.lang.NullPointerException
 	at net.sf.ehcache.CacheManager.replaceCacheWithDecoratedCache(CacheManager.java:937)
 	at net.sf.ehcache.constructs.web.filter.CachingFilter.doInit(CachingFilter.java:87)
 	at net.sf.ehcache.constructs.web.filter.Filter.init(Filter.java:196)
karthike

journeyman

Joined: 07/14/2011 03:17:31
Messages: 30
Offline

Please any person help me,

i am first time using EH cache now i am create web.xml and put the filtersample then create ehcache.xml at the time was i run tomcat, i got nullpointer and classnot found exception please help me.
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team