Exemple #1
0
        public void Setup()
        {
            //Demonstrated use of the eviction policy
            int maxCacheSize = 3;

            _evictionPolicy = new MaxCacheSizeEvictionPolicy(maxCacheSize);
            _cache          = new SlickCache <int, string>(null, _evictionPolicy);
        }
        /// <summary>
        /// Instantiate a new Cache based on the ICache(TKey,TValue) interface, can be instantiated with either parameter
        /// </summary>
        /// <param name="maxSize">Instantiate the cache using a max size eviction policy.</param>
        /// /// <param name="evictionPolicy">Instantiate the cache using your chosen eviction policy.</param>
        public SlickCache(int?maxSize = null, ICacheEvictionPolicy evictionPolicy = null)
        {
            //If maxSize has been specified and no eviction policy has been specified, then default to the max size eviction policy
            _evictionPolicy    = maxSize.HasValue && evictionPolicy == null ? new MaxCacheSizeEvictionPolicy(maxSize.Value) : evictionPolicy;
            _leastRecentlyUsed = new Node <TKey, TValue>(null, null, default(TKey), default(TValue));
            _mostRecentlyUsed  = _leastRecentlyUsed;

            //When the cache is constructed, it should take as an argument the maximum number of elements stored in the cache.
            _cache = new Dictionary <TKey, Node <TKey, TValue> >();
        }
 /// <summary>
 /// Creates a new directory cache
 /// </summary>
 /// <param name="directoryPath">The path to the directory to contain the cache. The directory will be created if it does not already exist</param>
 /// <param name="cacheMaxSize">The maximum size of the cache in bytes</param>
 /// <param name="cacheEvictionPolicy">The policy to use to maintain cache size</param>
 /// <param name="highwaterMark">The cache size (in bytes) above which the eviction policy will be applied</param>
 /// <param name="lowwaterMark">The cache size (in bytes) that the eviction policy will attempt to reduce the cache to when it runs</param>
 public DirectoryCache(string directoryPath, long cacheMaxSize, ICacheEvictionPolicy cacheEvictionPolicy, long highwaterMark = 0, long lowwaterMark = 0) : base(cacheMaxSize, cacheEvictionPolicy, highwaterMark, lowwaterMark)
 {
     _baseDir = new DirectoryInfo(directoryPath);
     if (!_baseDir.Exists) _baseDir.Create();
     _normalDir = new DirectoryInfo(Path.Combine(_baseDir.FullName, "normal"));
     _highDir = new DirectoryInfo(Path.Combine(_baseDir.FullName, "high"));
     if (!_normalDir.Exists) _normalDir.Create();
     if (!_highDir.Exists) _highDir.Create();
     CacheSize = _normalDir.GetFiles().Sum(f => f.Length);
     CacheSize += _highDir.GetFiles().Sum(f => f.Length);
 }
Exemple #4
0
 /// <summary>
 /// Creates a new cache instance
 /// </summary>
 /// <param name="cacheMaxSize">The maximum size of the cache in bytes</param>
 /// <param name="cacheEvictionPolicy">The policy used to maintain cache size</param>
 /// <param name="highwaterMark">The cache size at which the cache eviction policy will be run</param>
 /// <param name="lowwaterMark">The size that the cache eviction policy will attempt to achieve after it has run</param>
 protected AbstractCache(long cacheMaxSize, ICacheEvictionPolicy cacheEvictionPolicy, long highwaterMark = 0, long lowwaterMark = 0)
 {
     CacheSize      = 0;
     _cacheMaxSize  = cacheMaxSize;
     _highwaterMark = (long)(highwaterMark > 0 ? highwaterMark : cacheMaxSize * 0.9);
     _lowwaterMark  =
         (long)
         (lowwaterMark > 0
              ? lowwaterMark
              : (highwaterMark > 0 ? highwaterMark - (cacheMaxSize * 0.25) : cacheMaxSize * 0.65));
     if (_lowwaterMark <= 0)
     {
         _lowwaterMark = highwaterMark;
     }
     CacheEvictionPolicy = cacheEvictionPolicy;
 }
 /// <summary>
 /// Creates a new directory cache
 /// </summary>
 /// <param name="directoryPath">The path to the directory to contain the cache. The directory will be created if it does not already exist</param>
 /// <param name="cacheMaxSize">The maximum size of the cache in bytes</param>
 /// <param name="cacheEvictionPolicy">The policy to use to maintain cache size</param>
 /// <param name="highwaterMark">The cache size (in bytes) above which the eviction policy will be applied</param>
 /// <param name="lowwaterMark">The cache size (in bytes) that the eviction policy will attempt to reduce the cache to when it runs</param>
 public DirectoryCache(string directoryPath, long cacheMaxSize, ICacheEvictionPolicy cacheEvictionPolicy, long highwaterMark = 0, long lowwaterMark = 0) : base(cacheMaxSize, cacheEvictionPolicy, highwaterMark, lowwaterMark)
 {
     _baseDir = new DirectoryInfo(directoryPath);
     if (!_baseDir.Exists)
     {
         _baseDir.Create();
     }
     _normalDir = new DirectoryInfo(Path.Combine(_baseDir.FullName, "normal"));
     _highDir   = new DirectoryInfo(Path.Combine(_baseDir.FullName, "high"));
     if (!_normalDir.Exists)
     {
         _normalDir.Create();
     }
     if (!_highDir.Exists)
     {
         _highDir.Create();
     }
     CacheSize  = _normalDir.GetFiles().Sum(f => f.Length);
     CacheSize += _highDir.GetFiles().Sum(f => f.Length);
 }
Exemple #6
0
 public MemoryCacheBase(int capacity, ICacheEvictionPolicy <TKey, TValue> evictionPolicy)
 {
     _capacity       = capacity;
     _evictionPolicy = evictionPolicy;
 }
 /// <summary>
 /// Creates a new in-memory cache
 /// </summary>
 /// <param name="cacheSize">The maximum size of the cache in bytes</param>
 /// <param name="cacheEvictionPolicy">The eviction policy to use to maintain the cache size</param>
 /// <param name="highwaterMark">The cache size (in bytes) that will trigger the cache eviction policy to run</param>
 /// <param name="lowwaterMark">The cache size (in bytes) that the eviction policy will attempt to achieve when it is run</param>
 public MemoryCache(long cacheSize, ICacheEvictionPolicy cacheEvictionPolicy, long highwaterMark = 0, long lowwaterMark = 0) : base(cacheSize, cacheEvictionPolicy, highwaterMark, lowwaterMark)
 {
     _cache = new ConcurrentDictionary <string, MemoryCacheEntry>();
     CacheEvictionPolicy.Initialize(this);
 }
Exemple #8
0
 /// <summary>
 /// Creates a new in-memory cache
 /// </summary>
 /// <param name="cacheSize">The maximum size of the cache in bytes</param>
 /// <param name="cacheEvictionPolicy">The eviction policy to use to maintain the cache size</param>
 /// <param name="highwaterMark">The cache size (in bytes) that will trigger the cache eviction policy to run</param>
 /// <param name="lowwaterMark">The cache size (in bytes) that the eviction policy will attempt to achieve when it is run</param>
 public MemoryCache(long cacheSize, ICacheEvictionPolicy cacheEvictionPolicy, long highwaterMark = 0, long lowwaterMark = 0) : base(cacheSize, cacheEvictionPolicy, highwaterMark, lowwaterMark)
 {
     _cache = new ConcurrentDictionary<string, MemoryCacheEntry>();
     CacheEvictionPolicy.Initialize(this);
 }