/// <summary> /// Creates a new LRU (Least-Recently Used) cache of GrainReferences. /// </summary> /// <param name="maxSize">Maximum number of entries to allow.</param> /// <param name="maxAge">Maximum age of an entry.</param> /// <param name="f"> Delegate for fetching the value associated with a given key</param> /// <param name="c"> Delegate for casting IAddressable to TValue</param> public GrainReferenceCache(int maxSize, TimeSpan maxAge, FetchValueDelegate f, CastDelegate c) { maximumCount = maxSize; requiredFreshness = maxAge; fetcher = f; cache = new Dictionary <TKey, TimestampedValue>(); rwLock = new ReaderWriterLockSlim(); }
/// <summary> /// Creates a new LRU cache. /// </summary> /// <param name="maxSize">Maximum number of entries to allow.</param> /// <param name="maxAge">Maximum age of an entry.</param> /// <param name="f"></param> public LRU(int maxSize, TimeSpan maxAge, FetchValueDelegate f) { if (maxSize <= 0) { throw new ArgumentOutOfRangeException("maxSize", "LRU maxSize must be greater than 0"); } MaximumSize = maxSize; requiredFreshness = maxAge; fetcher = f; cache = new ConcurrentDictionary <TKey, TimestampedValue>(); }
/// <summary> /// Creates a new LRU cache. /// </summary> /// <param name="maxSize">Maximum number of entries to allow.</param> /// <param name="ttl">Time of live of an entry.</param> /// <param name="maxAge">Maximum age of an entry.</param> /// <param name="fetcher"></param> public LruCache(int maxSize, TimeSpan ttl, TimeSpan maxAge, FetchValueDelegate fetcher = null) { if (maxSize <= 0) { throw new ArgumentOutOfRangeException(nameof(maxSize), "LRU maxSize must be greater than 0"); } MaximumSize = maxSize; _maxAge = maxAge; _fetcher = fetcher; _cache = new ConcurrentDictionary <TKey, TimestampedValue>(); }