/// <summary>A 304 not modified response.</summary> public void NotModified(CachedContent cacheEntry) { long age = (long)((DateTime.UtcNow - cacheEntry.SavedAt).TotalSeconds); statusCode = 304; responseHeaders.status = "HTTP/2.0 304 Not Modified"; responseHeaders["Age"] = age.ToString(); responseHeaders["Date"] = cacheEntry.SavedAt.ToString(Cookie.DateTimePattern); if (cacheEntry.HasExpiry) { responseHeaders["Expires"] = cacheEntry.Expiry.ToString(Cookie.DateTimePattern); } if (!string.IsNullOrEmpty(cacheEntry.ETag)) { responseHeaders["ETag"] = cacheEntry.ETag; } responseHeaders["X-Spark-Cache"] = "OK"; // Received: byte[] data = cacheEntry.Data; ReceivedData(data, 0, data.Length); }
/// <summary>Gets binary data, checking the cache first.</summary> public void OnGetData(ContentPackage package) { // Attempt to hit the cache: CachedContent e = GetCached(package.location); if (e != null) { if (e.Expired) { // Expire it! e.Set.Entries.Remove(e.Path); // Save the set: e.Set.Save(); } else { // Successful hit - respond with a 304 (not modified): package.NotModified(e); return; } } // Ordinary request: OnGetDataNow(package); }
/// <summary>Attempts to get cached data for the given location.</summary> public CachedContent GetCached(Dom.Location location, bool create) { // Get the cache domain data (never null): DomainData domain = GetDomain(location); if (domain.Content == null) { if (create) { // Create the content set: domain.Content = new CachedContentSet(domain); } else { return(null); } } // Get the entry: string path = GetRelative(location); CachedContent e; if (!domain.Content.Entries.TryGetValue(path, out e) && create) { // Create the entry: e = new CachedContent(domain.Content, path); // Add it: domain.Content.Add(path, e); } return(e); }
/// <summary>Adds a cached file to the index.</summary> public void Add(string url, CachedContent content) { Entries[url] = content; }