public void Process(System.Web.HttpContext current, IResponseArgs e) { //Use alternate cache key if provided string key = e.RequestKey; //If cached, serve it. var c = cache.Get(key); if (c != null) { Serve(current, e, c); return; } //If not, let's cache it. locks.TryExecute(key, 3000, delegate() { c = cache.Get(key); if (c == null) { using (var data = new MemoryStream()){ e.ResizeImageToStream(data);//Very long-running call c = new MemCacheResult(data.CopyToBytes(true)); } cache.Set(key, c);//Save to cache (may trigger cleanup) } Serve(current, e, c); return; }); }
public IVirtualFile GetFileIfCached(string virtualPath, System.Collections.Specialized.NameValueCollection queryString, IVirtualFile original) { //Use alternate cache key if provided string key = original is IVirtualFileSourceCacheKey ? ((IVirtualFileSourceCacheKey)original).GetCacheKey(true) : original.VirtualPath; //If cached, serve it. CachedVirtualFile c = cache.Get(key); if (c != null) { return(c); } //If not, let's cache it. if ("mem".Equals(queryString["scache"], StringComparison.OrdinalIgnoreCase)) { locks.TryExecute(key, 3000, delegate() { c = cache.Get(key); if (c == null) { using (Stream data = original.Open()) {//Very long-running call c = new CachedVirtualFile(original.VirtualPath, StreamExtensions.CopyToBytes(data, true)); } cache.Set(key, c);//Save to cache (may trigger cleanup) } }); return(c); } return(null); }
public void TestTryExecuteThread() { for (int i = 0; i < loopCount; i++) { string key = "thekey"; p.TryExecute(key, timeoutMs, delegate() { if (!Monitor.TryEnter(lockCheck)) { testFailed = false; } else { if (sleepTime > 0) { Thread.Sleep(sleepTime); } Monitor.Exit(lockCheck); } }); } }