/// <summary> /// Adds or updates the serialized objects in the cache at the given cache keys. /// </summary> /// <param name="cacheKeysAndSerializedObjects">The cache keys and associated serialized objects.</param> /// <param name="tagName">The tag name.</param> /// <param name="absoluteExpiration">The absolute expiration. NOTE: if both absolute and sliding expiration are set, sliding expiration will be ignored.</param> /// <param name="slidingExpiration">The sliding expiration. NOTE: if both absolute and sliding expiration are set, sliding expiration will be ignored.</param> /// <param name="notifyRemoved">Whether or not to notify the client when the cached item is removed from the cache.</param> /// <param name="isInterned">Whether or not to intern the objects. NOTE: interned objects use significantly less memory when /// placed in the cache multiple times however cannot expire or be evicted. You must remove them manually when appropriate /// or else you will face a memory leak. If specified, absoluteExpiration, slidingExpiration, and notifyRemoved are ignored.</param> public void AddOrUpdate(IEnumerable <KeyValuePair <string, byte[]> > cacheKeysAndSerializedObjects, string tagName = null, DateTimeOffset?absoluteExpiration = null, TimeSpan?slidingExpiration = null, bool notifyRemoved = false, bool isInterned = false) { // Sanitize if (cacheKeysAndSerializedObjects == null) { throw new ArgumentNullException("cacheKeysAndSerializedObjects"); } // Determine cache item policy CacheItemPolicy cacheItemPolicy; CacheEntryRemovedCallback cacheEntryRemovedCallback = null; if (notifyRemoved) { cacheItemPolicy = _defaultRemovedCallbackCacheItemPolicy; cacheEntryRemovedCallback = CacheItemRemoved; } else { cacheItemPolicy = _defaultCacheItemPolicy; } if (absoluteExpiration.HasValue) { cacheItemPolicy = new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration.Value, RemovedCallback = cacheEntryRemovedCallback }; } else if (slidingExpiration.HasValue) { cacheItemPolicy = new CacheItemPolicy { SlidingExpiration = slidingExpiration.Value, RemovedCallback = cacheEntryRemovedCallback }; } // Iterate all cache keys and associated serialized objects foreach (var cacheKeysAndSerializedObjectKvp in cacheKeysAndSerializedObjects) { // Place object in cache if (isInterned) { _memCache.AddInterned(cacheKeysAndSerializedObjectKvp.Key, cacheKeysAndSerializedObjectKvp.Value); } else { _memCache.Add(cacheKeysAndSerializedObjectKvp.Key, cacheKeysAndSerializedObjectKvp.Value, cacheItemPolicy); } // Check if adding tag if (string.IsNullOrWhiteSpace(tagName)) { continue; } // Add to the local tag routing table _tagRoutingTable.AddOrUpdate(cacheKeysAndSerializedObjectKvp.Key, tagName); } }
/// <summary> /// Inserts or updates an interned byte array in the cache at the given key. /// Interned values cannot expire or be evicted unless removed manually. /// </summary> /// <param name="key">The key of the byte array. Null is not supported.</param> /// <param name="value">The byte array. Null is not supported.</param> /// <remarks> /// Passed byte array will be compressed by using <see cref="GZipStream"/>. /// </remarks> public void AddInterned(string key, byte[] value) { // Sanitize if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentException("cannot be null, empty, or white space", "key"); } if (value == null) { // GZipMemCache does not support null values throw new ArgumentNullException("value"); } _memCache.AddInterned(key, Compress(value)); }