net.sourceforge.java.feednread.backend.cache
Class NewsFeedCache

java.lang.Object
  extended bynet.sourceforge.java.feednread.backend.cache.NewsFeedCache
All Implemented Interfaces:
FeedFetcherCache

public class NewsFeedCache
extends Object
implements FeedFetcherCache

Simple cache for SyndFeedInfo wrapper objects. The class combines a live cache in memory and a persistent cache on a persistent medium, e.g. a cache directory on a harddisk. The live cache size is limited by ConfigurationManager.MAX_LIVE_CACHE_SIZE_KEY where this value denotes a maximum number of SyndFeedInfo objects to hold in memory at the same time. The persistent cache size is limited by ConfigurationManager.MAX_PERSISTENT_CACHE_SIZE_KEY. This value is specified in kilobytes.

If a SyndFeedInfo object is requested from the cache the lookup mechanism first tries to query the live cache in memory. Only if the requested object was not found in that first level cache the persistent cache is queried. If then the requested object was found it is automatically put into the live cache.

A SyndFeedInfo object can be requested from the cache by invoking the getFeedInfo(URL) method whereas a SyndFeedInfo object can be put into the cache by invoking the setFeedInfo(URL, SyndFeedInfo) method.

The live cache cleanup routine cleanupLiveCache() is invoked automatically if the configured size limit has been reached. In contrast the persistent cache cleanup routine cleanupPersistentCache() has to be called programatically to adjust the persistent cache to the configured size limit. Both cleanup routines remove the oldest items first, i.e. the SyndFeedInfo with the oldest modification date returned by SyndFeedInfo.getLastModified(). By default the application calls this method when it shuts down.

Author:
Sebastian Machhausen

Nested Class Summary
private  class NewsFeedCache.CachedFilenameFilter
           
 
Field Summary
private  Map cachedNewsFeeds
           
private static NewsFeedCache SINGLE_INSTANCE
           
 
Constructor Summary
private NewsFeedCache()
           Creates a new NewsFeedCache.
 
Method Summary
private  void cache(URL feedUrl, SyndFeedInfo syndFeedInfo)
           Puts the specified SyndFeedInfo object into the live cache in memory.
private  void cachePersistent(SyndFeedInfo syndFeedInfo)
           Caches the specified SyndFeedInfo object to a persistent medium.
private  void cleanupLiveCache()
           Cleans up this the live cache.
 void cleanupPersistentCache()
           Cleans up the persistent cache.
 void clearPersistentCache()
           Clears this NewsFeedCache's persistent cache completely, i.e. removes all cached NewsFeeds.
private  double deletePersistent(String id)
           Delete the SyndFeedInfo denoted by the specified id from the underlying persistent cache.
 SyndFeedInfo getFeedInfo(URL feedUrl)
           Gets a SyndFeedInfo object from the cache.
private  SyndFeedInfo getFromPersistentCache(String id)
           Gets the SyndFeedInfo object identified by the specified unique id from the persistent cache.
private  String getId(File f)
           Gets the id of the specified persistent SyndFeedInfo.
static NewsFeedCache getInstance()
           Gets the singleton instance of this NewsFeedCache.
 void setFeedInfo(URL feedUrl, SyndFeedInfo syndFeedInfo)
           Puts the specified SyndFeedInfo object in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cachedNewsFeeds

private Map cachedNewsFeeds

SINGLE_INSTANCE

private static NewsFeedCache SINGLE_INSTANCE
Constructor Detail

NewsFeedCache

private NewsFeedCache()

Creates a new NewsFeedCache.

Method Detail

getInstance

public static NewsFeedCache getInstance()

Gets the singleton instance of this NewsFeedCache.

Returns:
the singleton instance of this NewsFeedCache

getFeedInfo

public SyndFeedInfo getFeedInfo(URL feedUrl)

Gets a SyndFeedInfo object from the cache. The lookup mechanism first tries to query the live cache in memory. Only if the requested object was not found in that first level cache the persistent cache is queried. If then the requested object was found it is automatically put into the live cache.

Specified by:
getFeedInfo in interface FeedFetcherCache
Parameters:
feedUrl - The url of the feed
Returns:
A SyndFeedInfo object or null if it is not in the cache

setFeedInfo

public void setFeedInfo(URL feedUrl,
                        SyndFeedInfo syndFeedInfo)

Puts the specified SyndFeedInfo object in the cache. The SyndFeedInfo object is put in the live cache and written out to the persistent cache. If the persistance operation failed the SyndFeedInfo is put in the live cache nevertheless.

Specified by:
setFeedInfo in interface FeedFetcherCache
Parameters:
feedUrl - the url of the feed
syndFeedInfo - a SyndFeedInfo object for the feed

cleanupPersistentCache

public void cleanupPersistentCache()

Cleans up the persistent cache.


clearPersistentCache

public void clearPersistentCache()

Clears this NewsFeedCache's persistent cache completely, i.e. removes all cached NewsFeeds.


cache

private void cache(URL feedUrl,
                   SyndFeedInfo syndFeedInfo)

Puts the specified SyndFeedInfo object into the live cache in memory. This methods cleans up the live cache if the size limit configured through ConfigurationManager.MAX_LIVE_CACHE_SIZE_KEY

Parameters:
feedUrl - the url of the feed
syndFeedInfo - the SyndFeedInfo object to put into the live cache in memory

cachePersistent

private void cachePersistent(SyndFeedInfo syndFeedInfo)

Caches the specified SyndFeedInfo object to a persistent medium.

Parameters:
syndFeedInfo - the SyndFeedInfo object to cache persistent

getFromPersistentCache

private SyndFeedInfo getFromPersistentCache(String id)
                                     throws IOException,
                                            ClassNotFoundException

Gets the SyndFeedInfo object identified by the specified unique id from the persistent cache.

Parameters:
id - the unique id of the SyndFeedInfo object
Returns:
the SyndFeedInfo from the persistent cache
Throws:
IOException - if an io error occured during the cache operation
ClassNotFoundException - if a Class mismatch was detected

cleanupLiveCache

private void cleanupLiveCache()

Cleans up this the live cache.


deletePersistent

private double deletePersistent(String id)

Delete the SyndFeedInfo denoted by the specified id from the underlying persistent cache.

Parameters:
id - the id of the persistent SyndFeedInfo entry to delete
Returns:
the amount of freed persistent space in kilobytes

getId

private String getId(File f)

Gets the id of the specified persistent SyndFeedInfo.

Parameters:
f - the persistent SyndFeedInfo File
Returns:
the id or null if no valid persistent SyndFeedInfo File