/// <summary> /// Set Requestbody to to cache (Request body contains CacheKey, CacheEndDate, CacheValue items) /// </summary> /// <param name="context">Current HttpListener Context</param> private void SetCache(HttpListenerContext context) { string path = context.Request.Url.AbsolutePath; if (context.Request.HasEntityBody) { try { string source = GetSoureHeaderParam(context); string requestBody = GetBodyRequestBodyAsString(context); CacheRequestViewModel cacheForTheSet = GetAsCacheRequest(requestBody); if (source != null && source == this.Id) { return; } if (cacheForTheSet != null && !String.IsNullOrWhiteSpace(cacheForTheSet.key) && !String.IsNullOrWhiteSpace(cacheForTheSet.key)) { AddToMemoryCache(cacheForTheSet); Task.Run(() => persister.AddToPersistentCache(cacheForTheSet)); Task.Run(() => nodeSynconizer.AddToNodes(cacheForTheSet, source)); Task.Run(() => AllocateMemory()); WriteStringToHttpResult("{\"result\":\"OK\"}", context); } } catch (Exception) { WriteStringToHttpResult("{\"result\":\"FAIL\"}", context, HttpStatusCode.InternalServerError); } } }
public CacheRequestViewModel TryToGetFromPersistent(string key) { CacheRequestViewModel cachedFileItem = null; try { var cachedFile = Path.Combine(this.CacheFolder, key + ".txt"); if (File.Exists(cachedFile)) { cachedFileItem = JsonConvert.DeserializeObject <CacheRequestViewModel>(File.ReadAllText(cachedFile, Encoding.UTF8)); if (cachedFileItem != null) { if (cachedFileItem.expiresAt < DateTime.Now) { lock (lockObj) { File.Delete(cachedFile); } } else { //AddToMemoryCache(cachedFileItem); } } } } finally { } return(cachedFileItem); }
/// <summary> /// Remove only item which key's equal to CacheKey /// </summary> /// <param name="context">Current HttpListener Context</param> private void RemoveCache(HttpListenerContext context) { string path = context.Request.Url.AbsolutePath; if (context.Request.HasEntityBody) { string requestBody = GetBodyRequestBodyAsString(context); CacheRequestViewModel cacheForRemove = GetAsCacheRequest(requestBody); string source = GetSoureHeaderParam(context); if (source != null && source == this.Id) { return; } if (cacheForRemove != null && !String.IsNullOrWhiteSpace(cacheForRemove.key)) { cache.Remove(cacheForRemove.key); persister.DeleteCache(cacheForRemove.key); nodeSynconizer.DeleteFromNodes(cacheForRemove, source); } } WriteStringToHttpResult("{\"result\":\"OK\"}", context); }
/// <summary> /// Flush Removes all cache keys /// </summary> /// <param name="context">Current HttpListener Context</param> private void RemoveAllCache(HttpListenerContext context) { string requestBody = GetBodyRequestBodyAsString(context); CacheRequestViewModel cacheForRemove = GetAsCacheRequest(requestBody); string source = GetSoureHeaderParam(context); if (source != null && source == this.Id) { return; } lock (lockObj) { cache = new Dictionary <string, CacheRequestViewModel>(); } Task.Run(() => GC.Collect()); persister.DeleteCacheBulk(); Task.Run(() => nodeSynconizer.DeleteFromAllNodes(cacheForRemove, source)); WriteStringToHttpResult("{\"result\":\"OK\"}", context); }
public void DeleteFromAllNodes(CacheRequestViewModel cacheSource, string fromNode) { if (fromNode == null) { foreach (var node in this.nodes) { node.ClearAllCache(currentNodeId); } } }
public void DeleteFromNodesStartsWith(CacheRequestViewModel cacheToRemove, string fromNode) { if (fromNode == null) { foreach (var node in this.nodes) { node.RemoveKeyStartsWith(cacheToRemove.key, fromNode); } } }
public void DeleteFromNodes(CacheRequestViewModel cacheToRemove, string fromNode) { if (fromNode == null)//check if it sub request for infinite loop { foreach (var node in this.nodes) { node.Remove(cacheToRemove.key, currentNodeId); } } }
private void AddToMemoryCache(CacheRequestViewModel cacheForTheSet) { lock (lockObj) { if (cache.ContainsKey(cacheForTheSet.key)) { cache.Remove(cacheForTheSet.key); } ; cache.Add(cacheForTheSet.key, cacheForTheSet); } }
public void AddToPersistentCache(CacheRequestViewModel item) { try { lock (lockObj) { File.WriteAllText(Path.Combine(this.CacheFolder, item.key + ".txt"), JsonConvert.SerializeObject(item), Encoding.UTF8); } } finally { } }
public void AddToNodes(CacheRequestViewModel cacheForTheSet, string fromNode) { if (fromNode == null)//check if it sub request for infinite loop { foreach (var node in this.nodes.Where(c => c.Id != this.currentNodeId)) { if (cacheForTheSet.expiresAt.HasValue) { node.Set <string>(cacheForTheSet.key, cacheForTheSet.value, cacheForTheSet.expiresAt.Value, fromNode: currentNodeId); } else { node.Set <string>(cacheForTheSet.key, cacheForTheSet.value, fromNode: currentNodeId); } } } }
/// <summary> /// Removes only matched cache items which starts with key CacheKey /// </summary> /// <param name="context">Current HttpListener Context</param> private void RemoveCacheStartsWith(HttpListenerContext context) { string path = context.Request.Url.AbsolutePath; if (context.Request.HasEntityBody) { string requestBody = GetBodyRequestBodyAsString(context); CacheRequestViewModel cacheForRemove = GetAsCacheRequest(requestBody); string source = GetSoureHeaderParam(context); if (source != null && source == this.Id) { return; } if (cacheForRemove != null && !String.IsNullOrWhiteSpace(cacheForRemove.key)) { lock (lockObj) { foreach (var item in cache.ToList()) { if (item.Key.StartsWith(cacheForRemove.key)) { cache.Remove(item.Key); } } } Task.Run(() => AllocateMemory()); Task.Run(() => nodeSynconizer.DeleteFromNodesStartsWith(cacheForRemove, source)); persister.DeleteCacheBulk(cacheForRemove.key); } } WriteStringToHttpResult("{\"result\":\"OK\"}", context); }
/// <summary> /// Get Cached Item with CacheKey /// </summary> /// <param name="context">Current HttpListener Context</param> private void GetCache(HttpListenerContext context) { string requestBody = GetBodyRequestBodyAsString(context); CacheRequestViewModel cacheRequest = GetAsCacheRequest(requestBody); if (cacheRequest != null && !String.IsNullOrWhiteSpace(cacheRequest.key)) { CacheRequestViewModel cachedContent = null; cache.TryGetValue(cacheRequest.key, out cachedContent); if (cachedContent != null && cachedContent.expiresAt < DateTime.Now) { lock (lockObj) { cache.Remove(cachedContent.key); } Task.Run(() => AllocateMemory()); } //for persistent mode if (cachedContent == null) { cachedContent = persister.TryToGetFromPersistent(cacheRequest.key); if (cachedContent != null) { AddToMemoryCache(cachedContent); } } if (cachedContent != null) { WriteStringToHttpResult(JsonConvert.SerializeObject(cachedContent), context); } } }