示例#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;
            }
        }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Cache{TKey,Tvalue}"/> class.
 /// Creates 4-way set associative cache with 128 cache entries.
 /// <see cref="LruEvictionAlgorithm{TKey,Tvalue}"/> is set as default.
 /// </summary>
 /// <param name="mainStore">Main fall back data store.</param>
 /// <inheritdoc />
 public Cache(IMainStore <TKey, TValue> mainStore) : this(mainStore, 4, 128)
 {
 }
示例#3
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.
 /// <see cref="LruEvictionAlgorithm{TKey,Tvalue}"/> is set as default.
 /// </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>
 /// <inheritdoc />
 public Cache(IMainStore <TKey, TValue> mainStore, uint nWay, uint totalCacheEntries) :
     this(mainStore, nWay, totalCacheEntries, new LruEvictionAlgorithm <TKey, TValue>())
 {
 }
 public UsersRepository(IMainStore store)
 {
     this._userData = store;
 }