Пример #1
0
        public AssociativeCache(
            int way,
            int totalCacheCapacity,
            ICachePolicyFactory <K, V> cachePolicyFactory   = null,
            ICacheSetHashCalculator <K, V> hashSetCacluator = null,
            AppenderSkeleton appender = null)
        {
            this.appender = appender ?? new ConsoleAppender();

            // Implement as a one way cache
            if (way <= 0)
            {
                LoggingUtility.LogData(this.appender, "Number of cache sets cannot be 0 or negative", Level.Fatal);
                throw new ArgumentOutOfRangeException("Number of cache sets cannot be 0 or negative", "way");
            }

            if (totalCacheCapacity <= 0)
            {
                LoggingUtility.LogData(this.appender, "Number of cache sets cannot be 0 or negative", Level.Fatal);
                throw new ArgumentOutOfRangeException("Number of cache sets cannot be 0 or negative", "totalCacheCapacity");
            }

            this.cachePolicyFactory = cachePolicyFactory;
            this.cacheSetLookUp     = new Dictionary <int, CacheSet <K, V> >();
            this.cachePolicyFactory = cachePolicyFactory ?? new LRUPolicyFactory <K, V>();
            this.hashSetCalculator  = hashSetCalculator ?? new DefaultCacheSetHashCalculator <K, V>(way);

            for (int i = 0; i < way; i++)
            {
                this.cacheSetLookUp.Add(i, new CacheSet <K, V>(this.cachePolicyFactory.CreatePolicy(), totalCacheCapacity / way, this.appender));
            }
        }
Пример #2
0
 public void Clear()
 {
     try
     {
         foreach (CacheSet <K, V> cacheSet in cacheSetLookUp.Values)
         {
             cacheSet.Clear();
         }
     }
     catch (Exception ex)
     {
         LoggingUtility.LogData(appender, ex.Message, Level.Fatal);
         throw;
     }
 }
Пример #3
0
 public void Remove(K key)
 {
     try
     {
         int             setIndex = this.hashSetCalculator.GetSetIndex(key);
         CacheSet <K, V> cacheSet = cacheSetLookUp[setIndex];
         cacheSet.RemoveItemFromCacheSet(key);
         LoggingUtility.LogData(appender, $"Adding CacheIem in {setIndex} cache set", Level.Trace);
     }
     catch (Exception ex)
     {
         LoggingUtility.LogData(appender, ex.Message, Level.Fatal);
         throw;
     }
 }
Пример #4
0
        public V Get(K key)
        {
            try
            {
                int setIndex = this.hashSetCalculator.GetSetIndex(key);

                if (cacheSetLookUp[setIndex] != null)
                {
                    LoggingUtility.LogData(appender, $"Possible cache hit in set {setIndex}", Level.Trace);
                    return(cacheSetLookUp[setIndex].GetItemFromCacheSet(key));
                }
                else
                {
                    LoggingUtility.LogData(appender, $"Cache miss in set {setIndex} for key {key}", Level.Trace);
                    return(default(V));
                }
            }
            catch (Exception ex)
            {
                LoggingUtility.LogData(appender, ex.Message, Level.Fatal);
                throw;
            }
        }