Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Cache{TKey,Tvalue}"/> class.
        /// Creates n-way set associative cache with variable number of cache entries.
        /// It is possible to specify custom eviction algorithm.
        /// </summary>
        /// <param name="mainStore">Main fall back data store.</param>
        /// <param name="nWay">N-way parameter for the cache. Should be a power of 2.</param>
        /// <param name="totalCacheEntries">Total number of cache entries. Should be a power of 2.</param>
        /// <param name="evictionAlgorithm">Eviction algorithm.</param>
        public Cache(
            IMainStore <TKey, TValue> mainStore,
            uint nWay,
            uint totalCacheEntries,
            IEvictionAlgorithm <TKey, TValue> evictionAlgorithm)
        {
            if (nWay > MaxNWays)
            {
                throw new ArgumentException($"N-Way should be less or equal {MaxNWays}");
            }

            if (!CacheUtils.IsPowerOfTwo(nWay))
            {
                throw new ArgumentException("N-Way should be a power of two!");
            }

            if (totalCacheEntries > MaxCacheEntries)
            {
                throw new ArgumentException($"Number of total cache entries should be less or equal than {MaxCacheEntries}");
            }

            if (!CacheUtils.IsPowerOfTwo(totalCacheEntries))
            {
                throw new ArgumentException("Number of total cache entries should be a power of two!");
            }

            if (totalCacheEntries < nWay)
            {
                throw new ArgumentException($"Number of total cache entries {totalCacheEntries} should more or equal than {nWay} ways");
            }

            if (evictionAlgorithm == null)
            {
                throw new ArgumentException("Eviction algorithm isn't specified!");
            }

            this.mainStore = mainStore ?? throw new ArgumentException("Main data store isn't specified!");

            NWay = nWay;
            TotalCacheEntries = totalCacheEntries;
            CacheSets         = totalCacheEntries / nWay;

            cacheDictionaries = new CacheDictionary <TKey, TValue> [CacheSets];
            for (int i = 0; i < CacheSets; i++)
            {
                cacheDictionaries[i] = new CacheDictionary <TKey, TValue>(nWay, evictionAlgorithm);
                cacheDictionaries[i].EvictionListener += OnCacheEntryInvalidated;
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Cache set constructor.
 /// </summary>
 /// <param name="nWay">N-Way cache parameter</param>
 /// <param name="evictionAlgorithm">Eviction algorithm</param>
 internal CacheDictionary(uint nWay, IEvictionAlgorithm <TKey, TValue> evictionAlgorithm)
 {
     this.nWay = nWay;
     this.evictionAlgorithm = evictionAlgorithm;
 }