コード例 #1
0
        /// <summary>
        /// Adds a new item to the cache
        /// </summary>
        /// <param name="key">The cache key for the item. Must not be null</param>
        /// <param name="data">The data to be stored as a byte array. Must not be null</param>
        /// <param name="cachePriority">The priority of the item in the cache</param>
        public void Insert(string key, byte[] data, CachePriority cachePriority)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            Remove(key);
            if (CacheSize + data.Length > _highwaterMark)
            {
                CacheEvictionPolicy.Run(this, CacheSize - _lowwaterMark);
                if (CacheSize + data.Length > _cacheMaxSize)
                {
                    // If even after cache eviction there is not enough space, return without caching the object
                    return;
                }
            }
            var newEntry = AddEntry(key, data, cachePriority);

            if (newEntry != null)
            {
                CacheEvictionPolicy.NotifyInsert(key, data.Length, cachePriority);
                lock (_cacheLock)
                {
                    CacheSize += newEntry.Size;
                }
            }
        }