This is a disked backed lru cache which makes it non volatile in nature.
While designing this, I have considered following things :
- The Lru property is maintined on basis of entry counts.
- The disk space is not limited and so I have not taken total disk space into consideration.
- If any new entry is made then the least recently used entry is removed, so only one entry is removed.
- If disk space has to be considered then the removal of entires from the cache can be on basis of the byets size taken by new entry i.e. entries would be deleted from cache until the total space freed by them is less than the total space required by the new entry. Right now this is not implemented as disk space is considered to be unlimited.
- Records file on disk maintains the access order of entries.
- There was a choice between availability and performace. For cache performance matters, so write to record file is made async. So a case may arise where memory cache had an entry while it fails to write to records so while rebuilding cache from disk that entry may be missed.
- The collisions are minimum i.e. for every key a different hashcode is generated.
- Cache does return the key object.
- Program using this cache has write access on the specified directory path.
TestNG framework is used for unit testing
DiskCache is a generic cache implementation, to use this it needs to be extended by another class which will be returning its singeltion object. Also cache size and directory to store cache files need to be passed.
Eg:
public class CategoryCache extends DiskCache<Category, ArrayList<SubCategory>> {
private static CategoryCache CACHE;
private CategoryCache(File directory, int size) {
super(directory, size);
}
public static CategoryCache getCategoryCache(File directory, int size) {
if (CACHE == null) {
synchronized (CategoryCache.class) {
if (CACHE == null) {
CACHE = new CategoryCache(directory, size);
}
}
}
return CACHE;
}
public static CategoryCache getCategoryCache() {
return CACHE;
}
}