loading

Support and cache inventory

IsSupported / Keys / Has

Caches are persisted per origin and outlive the page. Keys lists every cache name the origin owns (including ones created by service workers), and Has checks for a specific cache without opening it.

C#
@inject Bit.Butil.CacheStorage cacheStorage

var supported = await cacheStorage.IsSupported();

var names = await cacheStorage.Keys();          // string[]

var exists = await cacheStorage.Has("app-v1");  // bool
Live sample
Cache name
inventory output
Results will appear here when you interact with the samples.

Store client-side responses

PutText / PutBytes

Not everything worth caching comes from the network. PutText and PutBytes build a Response from data you already have - generated reports, downloaded blobs, precomputed JSON - and store it against any URL key you choose, with a configurable content type and status.

C#
await cacheStorage.PutText("butil-docs-cache", "/greetings.txt", "Hello from the cache!");

await cacheStorage.PutBytes("butil-docs-cache", "/report.bin",
    data: myBytes,
    contentType: "application/octet-stream");
Live sample
URL key
Text content
put output
Results will appear here when you interact with the samples.

Cache from the network

Add / AddAll

Add fetches a URL and stores the response in one step - the cache-side equivalent of fetch followed by put. AddAll does the same for many URLs atomically: if any fetch fails, nothing is stored. Cross-origin URLs work but yield opaque responses whose bodies cannot be read back.

C#
await cacheStorage.Add("butil-docs-cache", "css/app.css");

await cacheStorage.AddAll("butil-docs-cache", "css/app.css", "favicon.ico");
Live sample
URL to fetch and cache
add output
Results will appear here when you interact with the samples.

Look up cached entries

Match / EntryKeys

Match retrieves a cached response snapshot: status, headers and body bytes. Check Found before using the result - a miss is not an exception. EntryKeys lists the request URLs currently stored in a cache, which is handy for building cache inspectors.

C#
var response = await cacheStorage.Match("butil-docs-cache", "/greetings.txt");

if (response.Found)
{
    var text = System.Text.Encoding.UTF8.GetString(response.Body);
    // response.Status, response.StatusText, response.Url, response.Headers
}

var urls = await cacheStorage.EntryKeys("butil-docs-cache"); // string[]
Live sample
URL key
lookup output
Results will appear here when you interact with the samples.

Evict entries and caches

DeleteEntry / Delete

DeleteEntry removes a single request/response pair; Delete drops the whole named cache. Both return true when something was actually removed, so you can distinguish a cleanup from a no-op.

C#
var removedEntry = await cacheStorage.DeleteEntry("butil-docs-cache", "/greetings.txt");

var removedCache = await cacheStorage.Delete("butil-docs-cache");
Live sample
URL key to remove
evict output
Results will appear here when you interact with the samples.
Note:
Origin-scoped and persistent Caches belong to the origin and are shared with any service worker running on it. They survive reloads and restarts, but the browser may evict them under storage pressure - pair this API with StorageManager to check quota and request persistence.
Warning:
Opaque responses Responses fetched cross-origin without CORS are stored as opaque: Match reports them with an empty body and no useful status. The same applies to 204/304 responses, which legitimately have no body. A secure context (HTTPS or localhost) is required for the whole API.

API reference

Member
Signature
Description
IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes caches.
Keys
ValueTask<string[]> Keys()
Lists every cache name visible to the current origin.
Has
ValueTask<bool> Has(string cacheName)
Returns true when a cache with the given name exists.
Delete
ValueTask<bool> Delete(string cacheName)
Deletes the named cache. Returns true when something was removed.
Add
ValueTask Add(string cacheName, string url)
Fetches the URL and stores the response in the named cache.
AddAll
ValueTask AddAll(string cacheName, params string[] urls)
Adds many URLs to the cache atomically.
PutBytes
ValueTask PutBytes(string cacheName, string url, byte[] data, string contentType = "application/octet-stream", int status = 200, string statusText = "OK")
Stores a response built from raw bytes against the URL - no network fetch involved.
PutText
ValueTask PutText(string cacheName, string url, string text, string contentType = "text/plain;charset=utf-8", int status = 200, string statusText = "OK")
Stores a UTF-8 text response against the URL.
Match
ValueTask<CachedResponse> Match(string cacheName, string url)
Looks up a cached response. CachedResponse.Found is false when nothing matched.
DeleteEntry
ValueTask<bool> DeleteEntry(string cacheName, string url)
Removes a single entry. Returns true when something was removed.
EntryKeys
ValueTask<string[]> EntryKeys(string cacheName)
Lists the URLs currently stored in the named cache.
CachedResponse
class CachedResponse { bool Found; int Status; string StatusText; string Url; Dictionary<string, string> Headers; byte[] Body; }
Snapshot of a cached Response. Body is empty for 204/304 or opaque responses.
MatchAny
ValueTask<CachedResponse> MatchAny(string url)
Looks a URL up across every cache this origin owns, in creation order, so the caller need not know which cache holds it. Use Match when you do know - it avoids searching the others.
An unhandled error has occurred. Reload 🗙