Author |
Message |
|
Hi,
we are using ehCache for lookup purpose, so basically what king of approach shall we go for?
Scenario->
We have a singleton lookup class which basically populates the cache during application start up. All the elements present in the cache wont be evicted.
Suggest me what kind of approach shall i go for
1>A single cache, with multiple keys(where a single key holds list of objects)
2>or different cache each having a single key which in turn holds list of objects
example shall i have a single cache which stores countries details via a single key and states details via another key. or different key for countries and state.
|
|
|
Hi,
is there any way to provide a user defined key via annotation while caching the objects in ehcache via annotation
eg @cacheable(...) can we provide key for caching here.
same required if we can provide a key during
@TriggerRemove(..)
|
|
|
Hi,
How do i use clusturing in ehcache?
1)if i dont use hibernate.
2)if i use spring for our application.
|
|
|
Hi can you provide me the complete list of attributes used in
<cache .../>, along with their meanings
eg>
<cache name="demoCache"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" >
</cache>
what are all the attributes, along with their meanings, thank you
|
|
|
Hi,
if using spring,after we define cache manager in ApplicationContext, does it shutsdown when the spring context get's destroyed, how to check that wether it does or not, please provide a suggestion
-----------------------------------------------------------------------------
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>/WEB-INF/ehcache.xml</value>
</property>
</bean>
--------------------------------------------------------------------------------
|
|
|
Hi,
we are using ehcahe as our server side cache, can you suggest, any ehcache monitor.
|
|
|
Hi,
completed eager loading successfully, with the suggestions from dwai, and few modification done by me below are the approaches->>
(Spring based approach)
1) configure your ApplicationContext.xml as below
<bean id="statesDAO" class="com.testing.sales.input.investor.StatesDAO"
parent="baseDAOBean" />
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>/WEB-INF/ehcache.xml</value>
</property>
</bean>
<bean id="ehCacheFactory" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref bean="ehCacheManager" />
</property>
<property name="bootstrapCacheLoader">
<ref bean="myBootstrapCacheLoaderFactory" />
</property>
</bean>
<bean id="myBootstrapCacheLoaderFactory" class="com.testing.sales.input.investor.MyBootstrapCacheLoaderFactory">
<property name="cacheManager">
<ref bean="ehCacheManager" />
</property>
<property name="statesDAO">
<ref bean="statesDAO" />
</property>
</bean>
ehCache.xml->
enter your cache entry->
<cache name="stateCache"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" >
</cache>
Configure your custom bootstraploader
package com.testing.sales.input.investor;
import java.util.List;
import java.util.Properties;
import com.googlecode.ehcache.annotations.CacheException;
import com.ltg.db.DAOException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
import net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;
import net.sf.ehcache.loader.CacheLoaderFactory;
public class MyBootstrapCacheLoaderFactory extends BootstrapCacheLoaderFactory
implements BootstrapCacheLoader
{
public MyBootstrapCacheLoaderFactory() {
super();
// TODO Auto-generated constructor stub
}
StatesDAO statesDAO;
public StatesDAO getStatesDAO() {
return statesDAO;
}
public void setStatesDAO(StatesDAO statesDAO) {
this.statesDAO = statesDAO;
}
CacheManager cacheManager;
public CacheManager getCacheManager() {
return cacheManager;
}
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
@Override
public BootstrapCacheLoader createBootstrapCacheLoader(Properties properties) {
// TODO Auto-generated method stub
return new MyBootstrapCacheLoaderFactory();
}
public Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
public boolean isAsynchronous() {
// TODO Auto-generated method stub
return false;
}
public void load(Ehcache myCache) throws CacheException {
// TODO Auto-generated method stub
System.out.println("load your cache with whatever you want....");
List keys = myCache.getKeys();
for (int i = 0; i < keys.size(); i++) {
System.out.println("keys->"+keys.get(i));
}
try {
getStatesDAO().findAllStates();
} catch (DAOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("load complete!");
}
}
StatesDAO->
package com.testing.sales.input.investor;
import java.util.ArrayList;
import java.util.List;
import com.ltg.db.*;
import com.ltg.db.cache.CacheManager;
import net.sf.ehcache.Cache;
public class StatesDAO{
//annotation based caching and the name of cache should be defined in ehcache.xml.
@Cacheable(cacheName="stateCache")
public List findAllStates() throws DAOException {
List dataList=new ArrayList();
//your call to database that returns a list of objects
return dataList;
}
}
|
|
|
Hi,
the createBootstrapCacheLoader takes properties as arguments as you said but problem is wheni create a customBootstrapLoader and it has some data members as datasource, and some jdbcConnectiontemplate, which are required for database connectivity, but these dependent datamembers are defined in applicationContext.xml. and the dependency injection is done spring.
But the bootstrap loader is called after the caches are initialised, so how does bootstraploader come to know that all the dependency injection are set to the datamembers of custom bootstraploader and now it needs to invoke the load method. because it doesnt know about the spring dependency injection it only knows that caches are initialised.And it might happen that caches are initialised before the beans are initialised and injected by spring container.
|
|
|
thank you very much for the answer.
|
|
|
Hi,
i am using spring annotation based caching, the cache is created but on subsequent request to same method the values are not cached. can you suggest what can be the problem
---------------------------------------------
//StatesDAO class
@Cacheable(cacheName="stateCache")
public List findAllStates() throws DAOException {
//some code that returns a list of states.
return list;
}
------------------------------------------
//ehcache.xml
<cache name="stateCache"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" >
</cache>
---------------------------------------------
//ApplicationContext
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name="configLocation">
<value>/WEB-INF/ehcache.xml</value>
</property>
</bean>
<bean id="statesDAO" class="StatesDAO" parent="baseDAOBean"/>
-----------------------------------------------------
//pom.xml
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.springmodules</groupId>
<artifactId>spring-modules-cache</artifactId>
<version>0.9</version>
</dependency>
-----------------------------------------------------
Here the cache is getting created but the values that the method returns ,doesnt get cached. can you please suggest what else to do
|
|
|
thank you, please let me know the solution asap
|
|
|
Sure will let you know, thank you for the approach
|
|
|
There is one issue in this. my custom bootstraploaderFactory has some properties which are configured in spring's xml file, how does the bootstrap loader makes sure that all the properties of bootstraploader is instantiated before it calls the load method,
|
|
|
Hi,
i studied ehcache documentation, and i came accross if we use
annotation
@Cacheable(cacheName="cacheName" ) before a method then the values the method returns gets stored in cache whose name is provided in the annotation. It also said that if the method has a parameter then the parameter name becomes the key.
eg-
@Cacheable(cacheName="studentCache" )
getStudent(String studentName)
{
.....
}
here student name becomes the key for all the values the method returns.
how does this automatic key assigning works when there are
1)No parameters
eg-
@Cacheable(cacheName="studentCache" )
getStudent()
{
//returns all the students
.....
}
2)Multiple Parameters
eg-
@Cacheable(cacheName="studentCache" )
getStudent(String studentName,int marks)
{
//returns selected students
.....
}
Kindly suggest if this methodology is appropriate for all cases.
|
|
|
Hi,
Actually i had thought of writing a servlet to do the prepolulating of cache, but according to our design we want to go for bootstrapCacheLoaderFactory to do the same for us, i tried to override the load method of bootstrapCacheLoaderFactory. Our Database is called from the dao and then dao is configured via spring and its init method(to get the data from database) is called after the load method of bootstrapCacheLoaderFactory. can you suggest a way how to do it via bootstrapCacheLoaderFactory
|
|
|