public void TestAccess() { CacheResult r = cache.GetCachedFile(new Random().Next(0, quantity).ToString(), "test", delegate(Stream s) { Assert.Fail("No files have been modified, this should not execute"); }, defaultDate, 100); Assert.IsTrue(System.IO.File.Exists(r.PhysicalPath)); Assert.IsTrue(r.Result == CacheQueryResult.Hit); }
public CustomDiskCacheTest(int subfolders, int totalFiles, bool hashModifiedDate) { char c = System.IO.Path.DirectorySeparatorChar; string folder = System.IO.Path.GetTempPath().TrimEnd(c) + c + System.IO.Path.GetRandomFileName(); cache = new CustomDiskCache(this, folder,subfolders,hashModifiedDate); this.quantity = totalFiles; for (int i = 0; i < quantity;i++){ cache.GetCachedFile(i.ToString(),"test",delegate(Stream s){ s.WriteByte(32); //Just one space },defaultDate, 10); } }
public CustomDiskCacheTest(int subfolders, int totalFiles, bool hashModifiedDate) { char c = System.IO.Path.DirectorySeparatorChar; string folder = System.IO.Path.GetTempPath().TrimEnd(c) + c + System.IO.Path.GetRandomFileName(); cache = new CustomDiskCache(this, folder, subfolders, hashModifiedDate); this.quantity = totalFiles; for (int i = 0; i < quantity; i++) { cache.GetCachedFile(i.ToString(), "test", delegate(Stream s){ s.WriteByte(32); //Just one space }, defaultDate, 10); } }
public IVirtualFile GetFileIfCached(string virtualPath, System.Collections.Specialized.NameValueCollection queryString, IVirtualFile original) { if (!"disk".Equals(queryString["scache"], StringComparison.OrdinalIgnoreCase)) { return(null); } if (this.AsyncModuleMode) { throw new InvalidOperationException("SourceDiskCache cannot be used in synchronous mode if AsyncModuleMode=true"); } //Verify web.config exists in the cache folder. writer.CheckWebConfigEvery5(); //Use alternate cache key if provided string key = original is IVirtualFileSourceCacheKey ? ((IVirtualFileSourceCacheKey)original).GetCacheKey(false) : original.VirtualPath; //If cached, serve it. var r = cache.GetCachedFile(key, ".cache", delegate(Stream target) { using (Stream data = original.Open()) { //Very long-running call data.CopyToStream(target); } }, 15 * 1000, true); if (r.Result == CacheQueryResult.Failed) { return(null); } if (r.Result == CacheQueryResult.Hit && cleaner != null) { cleaner.UsedFile(r.RelativePath, r.PhysicalPath); } return(new SourceVirtualFile(original.VirtualPath, delegate(){ return r.Data ?? File.Open(r.PhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read); })); }
public IVirtualFile GetFileIfCached(string virtualPath, System.Collections.Specialized.NameValueCollection queryString, IVirtualFile original) { if (!"disk".Equals(queryString["scache"], StringComparison.OrdinalIgnoreCase)) { return(null); } //Verify web.config exists in the cache folder. writer.CheckWebConfigEvery5(); //Use alternate cache key if provided string key = original is IVirtualFileSourceCacheKey ? ((IVirtualFileSourceCacheKey)original).GetCacheKey(false) : original.VirtualPath; //If cached, serve it. var r = cache.GetCachedFile(key, ".cache", delegate(Stream target) { using (Stream data = original.Open()) {//Very long-running call StreamExtensions.CopyToStream(data, target); } }, DateTime.MinValue, 15 * 1000, true); if (r.Result == CacheQueryResult.Failed) { throw new ImageResizer.ImageProcessingException("Failed to acquire a lock on file \"" + r.PhysicalPath + "\" within 15 seconds. Source caching failed."); } if (r.Result == CacheQueryResult.Hit && cleaner != null) { cleaner.UsedFile(r.RelativePath, r.PhysicalPath); } return(new SourceVirtualFile(original.VirtualPath, delegate(){ return r.Data ?? File.Open(r.PhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read); })); }