I have a TableHandler which implements table loading / reloading logic. By table I mean the data I load from either disc or memory when a user request for it in the form of table object. If a new request arrives and the instance of table is in memory then I just loads the table from memory and don’t reloads it from disc. If the table I am looking for is not present in memory I just loads the table from disc. Now As my request are increasing a lot of tables are getting stored in memory so I want to release some tables automatically from memory after say 5 min which aren’t been excessed for a duration of 5 min. How can I implement it?
I have tablehandler class structure like this
final class TableHandler{
/*
Data struct for storing information required for management
of the loaded table instances
*/
final static class Tableinfo{
/*
Table instance.. and other things like table when
last modified and whether to referench the table
when a next request arrives.
*/
}
getTable(){someInternalTableHandler.getTable()}
replacetable {someInternalTableHandler.replacetable()}
//Handles loading/reloading logic.
final static class someInternalTablehandler{
getTable(){...}
createTableInstance(){...}
replaceTable{...}
}
}
How can I implement the above said logic into my code….??
You can use the Guava MapMaker to do this:
http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/MapMaker.html
If you don’t want to hard code it to a time, you can use soft references for your values in the created map. This will cause your map’s values to be dropped if they are no longer referenced and the JVM is running out of memory.
Rolling your own map that uses weak/soft references to clear itself isn’t too hard either. Take a look at ReferenceQueue, WeakReference, and SoftReference
There are some projects that allow you to annotate a method with
@Cacheand all return values of that method will be cached and cleared when no longer referenced. http://code.google.com/p/tantalizing-eats/