Exemple #1
0
        private async Task <CacheResult> ProcessAsync(IAsyncResponsePlan e)
        {
            //Verify web.config exists in the cache folder.
            writer.CheckWebConfigEvery5();

            //Cache the data to disk and return a path.
            CacheResult r = await asyncCache.GetCachedFile(e.RequestCachingKey, e.EstimatedFileExtension, async delegate(Stream outStream){
                await e.CreateAndWriteResultAsync(outStream, e);
            }, CacheAccessTimeout, AsyncWrites);


            //Fail
            if (r.Result == CacheQueryResult.Failed)
            {
                GlobalPerf.Singleton.IncrementCounter("diskcache_timeout");
                throw new ImageResizer.ImageProcessingException("Failed to acquire a lock on file \"" + r.PhysicalPath + "\" within " + CacheAccessTimeout + "ms. Caching failed.");
            }

            if (r.Result == CacheQueryResult.Hit && cleaner != null)
            {
                cleaner.UsedFile(r.RelativePath, r.PhysicalPath);
            }

            GlobalPerf.Singleton.IncrementCounter((r.Result == CacheQueryResult.Hit) ? "diskcache_hit" : "diskcache_miss");

            return(r);
        }
        public async Task ProcessAsync(HttpContext context, IAsyncResponsePlan e)
        {
            var file = await GetFileAsync(e.RequestCachingKey, null);

            var reprocessCache = false;

            if (file != null)
            {
                try
                {
                    var stream = await file.OpenAsync();

                    await e.CreateAndWriteResultAsync(stream, e);
                }
                catch (FileNotFoundException)
                {
                    reprocessCache = true;
                }
            }

            if (file == null || reprocessCache)
            {
                var stream = new MemoryStream(4096);
                await e.CreateAndWriteResultAsync(stream, e);

                Upload(e.RequestCachingKey, stream, null);
            }

            context.RemapHandler(new NoCacheAsyncHandler(e));
        }
Exemple #3
0
        public async Task ProcessAsync(HttpContext context, IAsyncResponsePlan e)
        {
            if (!this.AsyncModuleMode)
            {
                throw new InvalidOperationException("DiskCache cannot be used in asynchronous mode if AsyncModuleMode=false");
            }
            CacheResult r = await ProcessAsync(e);

            context.Items["FinalCachedFile"] = r.PhysicalPath;

            if (r.Data == null)
            {
                //Calculate the virtual path
                string virtualPath = VirtualCacheDir.TrimEnd('/') + '/' + r.RelativePath.Replace('\\', '/').TrimStart('/');

                //Rewrite to cached, resized image.
                context.RewritePath(virtualPath, false);
            }
            else
            {
                //Remap the response args writer to use the existing stream.
                e.CreateAndWriteResultAsync = delegate(Stream s, IAsyncResponsePlan plan)
                {
                    return(((MemoryStream)r.Data).CopyToAsync(s));
                };
                context.RemapHandler(new NoCacheAsyncHandler(e));
            }
        }
Exemple #4
0
        public Task ProcessAsync(HttpContext current, IAsyncResponsePlan e)
        {
            //Use alternate cache key if provided
            string key = e.RequestCachingKey;

            //If cached, serve it.
            var c = cache.Get(key);

            if (c != null)
            {
                Serve(current, e, c);
                return(Task.FromResult(0));
            }
            //If not, let's cache it.
            return(asyncLocks.TryExecuteAsync(key, 3000, async delegate()
            {
                c = cache.Get(key);
                if (c == null)
                {
                    using (var data = new MemoryStream(4096))
                    {
                        await e.CreateAndWriteResultAsync(data, e);//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;
            }));
        }
Exemple #5
0
 public void FirePreHandleImageAsync(IHttpModule sender, HttpContext context, IAsyncResponsePlan e)
 {
     System.Threading.Interlocked.Increment(ref processedCount);
     if (PreHandleImageAsync != null)
     {
         PreHandleImageAsync(sender, context, e);
     }
 }
Exemple #6
0
 public bool CanProcess(HttpContext current, IAsyncResponsePlan e)
 {
     //Disk caching will 'pass on' caching requests if 'cache=no'.
     if (new Instructions(e.RewrittenQuerystring).Cache == ServerCacheMode.No)
     {
         return(false);
     }
     return(Started);//Add support for nocache
 }
Exemple #7
0
        public bool CanProcess(HttpContext context, IAsyncResponsePlan e)
        {
            //Disk caching will 'pass on' caching requests if 'cache=no'.
            if (new Instructions(e.RewrittenQuerystring).Cache == ServerCacheMode.No)
            {
                return false;
            }

            return true;
        }
Exemple #8
0
        public async Task ProcessAsync(HttpContext current, IAsyncResponsePlan e)
        {
            var blobName = ResolveBlobName(e.RequestCachingKey, e.EstimatedFileExtension);

            var cachedStream = await this.cacheProvider.ResolveAsync(blobName, async outputStream =>
            {
                await e.CreateAndWriteResultAsync(outputStream, e); // long-running call
            });

            Serve(current, e, cachedStream);
        }
Exemple #9
0
        public IAsyncTyrantCache GetAsyncCacheFor(HttpContext context, IAsyncResponsePlan plan)
        {
            IAsyncTyrantCache defaultCache = null;

            //Grab the last cache that claims it can process the request.
            foreach (IAsyncTyrantCache cache in c.Plugins.GetAll <IAsyncTyrantCache>())
            {
                if (cache.CanProcess(context, plan))
                {
                    defaultCache = cache;
                }
            }

            return(defaultCache);
        }
Exemple #10
0
 public bool CanProcess(HttpContext current, IAsyncResponsePlan e)
 {
     return(true);
 }
 public NoCacheAsyncHandler(IAsyncResponsePlan e)
 {
     this.e = e;
 }
Exemple #12
0
 public System.Threading.Tasks.Task ProcessAsync(System.Web.HttpContext context, IAsyncResponsePlan e)
 {
     context.RemapHandler(new NoCacheAsyncHandler(e));
     return(Task.FromResult(true));
 }
Exemple #13
0
 public bool CanProcess(HttpContext current, IAsyncResponsePlan e)
 {
     return("true".Equals(e.RewrittenQuerystring["mcache"], StringComparison.OrdinalIgnoreCase));
 }
Exemple #14
0
 private void Serve(HttpContext context, IAsyncResponsePlan e, MemCacheResult result)
 {
     context.RemapHandler(new MemCacheHandler(e, result.Data));
 }
 public AzureBlobHandler(IAsyncResponsePlan asyncResponse, Stream data)
 {
     this.asyncResponse = asyncResponse;
     this.data          = data;
 }
Exemple #16
0
 private static void Serve(HttpContext context, IAsyncResponsePlan e, Stream data)
 {
     context.RemapHandler(new AzureBlobHandler(e, data));
 }
Exemple #17
0
 public MemCacheHandler(IAsyncResponsePlan p, byte[] data)
 {
     this.p    = p;
     this.data = data;
 }