I have a data provider (basically, system-of-record) which can return me a huge data set. What I want to do is, write these records in a cache, and then, return a set of records page-by-page from the cache. The cache here acts like a buffer to ensure that there is no strain on the memory. In the following example, I have just stopped short of the actual paging solution. Instead I have tried to illustrate how its possible using EhCache.
Just putting ehcache and slf4j should be good enough.
net.sf.ehcache ehcache 2.8.3 org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} log4j log4j 1.2.14
The ehCache configuration file, my-demo-ehcache.xml in this case, looks like:
CacheManager cacheManager = CacheManager.newInstance(CacheBackedRssReader.class.getResource("/my-demo-ehcache.xml"));
Cache cache = cacheManager.getCache("myCache1");
Note that we are loading the ehCache configuration file and then retrieving a cache named myCache1, which I had defined in the configuartion xml.
If you are planning to write this cache on a disk, then you need to make sure that the object that you are storing is Serializable.
Element cacheElement = new Element("MyIdentifier", mySerializableObject);
cache.put(cacheElement);
Element element = cache.get("MyIdentifier");
MySerializableObject myObject = (MySerializableObject) element.getObjectValue();
The sources for this can be found here: https://github.com/paawak/blog/tree/master/code/ehcache-simple