public byte[] Store(string commandName, string key, ulong flags, DateTime exptime, byte[] data) { if ((ulong)data.Length > _cache.Capacity) { return(Encoding.ASCII.GetBytes("ERROR Over capacity\r\n")); } bool stored = false; switch (commandName) { case "set": stored = _cache.Store(key, flags, data, exptime); break; case "replace": stored = _cache.Replace(key, flags, exptime, data); break; case "add": stored = _cache.Add(key, flags, exptime, data); break; case "append": case "prepend": stored = _cache.Append(key, flags, exptime, data, commandName == "append"); break; } return(stored ? Encoding.ASCII.GetBytes("STORED\r\n") : Encoding.ASCII.GetBytes("NOT_STORED\r\n")); }
/// <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); } }
public async Task <IHttpActionResult> Get(int?id = null, string name = null) { var result = new List <Sample>(); var cache = new List <Sample>(); cache = _memCache.GetValue(_key); if (cache == null) { result = (List <Sample>) await Task.Run(() => _sampleService.Get(new Sample())); _memCache.Add(_key, result, DateTimeOffset.UtcNow.AddHours(1)); result = new List <Sample>(); cache = new List <Sample>(); cache = _memCache.GetValue(_key); if (id != null || !string.IsNullOrEmpty(name)) { result = cache.FindAll(x => x.Id == id || x.Name.ToLower().Contains(name.ToLower())); } else { result = cache; } } else { if (id != null || !string.IsNullOrEmpty(name)) { result = cache.FindAll(x => x.Id == id || x.Name.ToLower().Contains(name.ToLower())); } else { result = cache; } } if (!result.Any()) { return(NotFound()); } return(Ok(new BaseResponse <IEnumerable <Sample> > { IsSuccess = true, Message = HttpResponseCode.OK.GetDescription(), Rows = result.Count(), Data = result })); }
/// <summary> /// Inserts or updates a byte array in the cache at the given key with the specified cache item policy. /// </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> /// <param name="cacheItemPolicy">The cache item policy.</param> /// <remarks> /// Passed byte array will be compressed by using <see cref="GZipStream"/>. /// </remarks> public void Add(string key, byte[] value, CacheItemPolicy cacheItemPolicy) { // 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"); } if (cacheItemPolicy == null) { throw new ArgumentNullException("cacheItemPolicy"); } _memCache.Add(key, Compress(value), cacheItemPolicy); }
public void RestoreCache() { if (_fileSystem.File.Exists(_path)) { var newLog = RebuildLog().ToArray(); var tempFileName = Path.GetTempFileName(); WriteToFile(tempFileName, newLog); ReplacePreviousLogFile(tempFileName); var storeNotifications = newLog.Where(e => e.Store != null).Select(e => e.Store); foreach (var storeNotification in storeNotifications) { _memCache.Add(storeNotification.Key, storeNotification.Flags, storeNotification.Expiry, storeNotification.Data); } } }
/// <summary> /// Only add if not already added /// return whether it was added /// </summary> public bool Add(IList <MapPoint> points, TimeSpan timespan, string pointType = null) { var key = GetPointKey(pointType); return(_memCache.Add(key, RandomizePointOrder(points), timespan)); }
private string Add(IMemCache cache) { var itemPolicy = new CacheItemPolicy(); var key = Guid.NewGuid().ToString(); cache.Add(key, new byte[100], itemPolicy); return key; }