
322
CHAPTER 10 Building ScalaBle applicationS inSharepoint
//time when the cache was loaded
private volatile DateTime _loaded;
//maximum amount of time before data is reloaded into the cache
private int _timeoutMinutes;
//maximum stale time for served files
private int _maxStaleMinutes;
//used to store the methods used to retrieve values and keys
private ListRetriever _getItems;
private KeyRetriever _getKey;
//used to indicate when a reload is running
private volatile bool _reloadRunning = false;
//used for thread synchronization
private object _syncRoot = new object();
Although the comments in the previous snippet give you some idea of how this object can work, the
following function implementations and their associated comments should make this more clear.
Insert the class constructor:
//main constructor, used to initialize the cache
public CachedDictionary(
ListRetriever GetItems,
KeyRetriever GetKey,
int TimeoutMinutes,
int MaximumStaleMinutes)
{
_getItems = GetItems;
_getKey = GetKey;
_timeoutMinutes = TimeoutMinutes;
_maxStaleMinutes = MaximumStaleMinutes;
ReloadCache();
}
Only four parameters are required to initialize the cache. The rst two parameters are methods
that can retrieve the items to be cached and their keys, respectively. The rst method must return
a generic list of type
SomeObject (your first generic type), whereas the second method must accept
an object of type
SomeObject ...