Table of Contents

Class Cache

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The Cache interface provides a persistent storage mechanism for Request / Response object pairs that are cached in long lived memory. How long a Cache object lives is browser dependent, but a single origin's scripts can typically rely on the presence of a previously populated Cache object. Note that the Cache interface is exposed to windowed scopes as well as workers. You don't have to use it in conjunction with service workers, even though it is defined in the service worker spec.

[Value("Cache")]
public class Cache
Inheritance
Cache
Inherited Members

Remarks

An origin can have multiple, named Cache objects. You are responsible for implementing how your script (e.g., in a ServiceWorker) handles Cache updates. Items in a Cache do not get updated unless explicitly requested; they don't expire unless deleted. Use Open(string) to open a specific named Cache object and then call any of the Cache methods to maintain the Cache.

You are also responsible for periodically purging cache entries. Each browser has a hard limit on the amount of cache storage that a given origin can use. Cache quota usage estimates are available via the Estimate() method. The browser does its best to manage disk space, but it may delete the Cache storage for an origin. The browser will generally delete all of the data for an origin or none of the data for an origin. Make sure to version caches by name and use the caches only from the version of the script that they can safely operate on. See Deleting old caches for more information.

NOTE

The key matching algorithm depends on the VARY header in the value. So matching a new key requires looking at both key and value for entries in the Cache object.

NOTE

The caching API doesn't honor HTTP caching headers.

-Using Service Workers
-Service workers basic code example
-Using web workers

See also on MDN

Constructors

Cache()

public Cache()

Methods

Add(Union42)

The add() method of the Cache interface takes a URL, retrieves it, and adds the resulting response object to the given cache.

[Value("add")]
public Task<GlobalObject.Undefined> Add(Union42 request)

Parameters

request Union42

Returns

Task<GlobalObject.Undefined>

A Promise that resolves with undefined.

Remarks

The add() method is functionally equivalent to the following:

For more complex operations, you'll need to use Put(Union42, Response) directly.

NOTE

add() will overwrite any key/value pair previously stored in the cache that matches the request.

-Using Service Workers
-Cache
-Window.Caches and WorkerGlobalScope.Caches

See also on MDN

AddAll(List<Union42>)

The addAll() method of the Cache interface takes an array of URLs, retrieves them, and adds the resulting response objects to the given cache. The request objects created during retrieval become keys to the stored response operations.

[Value("addAll")]
public Task<GlobalObject.Undefined> AddAll(List<Union42> requests)

Parameters

requests List<Union42>

Returns

Task<GlobalObject.Undefined>

A Promise that resolves with undefined.

Remarks

NOTE

addAll() will overwrite any key/value pairs
previously stored in the cache that match the request, but will fail if a
resulting put() operation would overwrite a previous cache entry stored by the same addAll() method.

-Using Service Workers
-Cache
-Window.Caches and WorkerGlobalScope.Caches

See also on MDN

Delete(Union42, CacheQueryOptions)

The delete() method of the Cache interface finds the Cache entry whose key is the request, and if found, deletes the Cache entry and returns a {{jsxref("Promise")}} that resolves to true.
If no Cache entry is found, it resolves to false.

[Value("delete")]
public Task<bool> Delete(Union42 request, CacheQueryOptions options = null)

Parameters

request Union42
options CacheQueryOptions

Returns

Task<bool>

a Promise that resolves to true if the cache entry is
deleted, or false otherwise.

Remarks

-Using Service Workers
-Cache
-Window.Caches and WorkerGlobalScope.Caches

See also on MDN

Keys(Union42, CacheQueryOptions)

The keys() method of the Cache interface returns a
{{jsxref("Promise")}} that resolves to an array of Request objects
representing the keys of the Cache.

[Value("keys")]
public Task<Request[]> Keys(Union42 request = default, CacheQueryOptions options = null)

Parameters

request Union42
options CacheQueryOptions

Returns

Task<Request[]>

A {{jsxref("Promise")}} that resolves to an array of Request
objects.

Remarks

The requests are returned in the same order that they were inserted.

NOTE

Requests with duplicate URLs but different headers can be
returned if their responses have the VARY header set on them.

-Using Service Workers
-Cache
-Window.Caches and WorkerGlobalScope.Caches

See also on MDN

Match(Union42, CacheQueryOptions)

The match() method of the Cache interface returns a {{jsxref("Promise")}} that resolves to the Response associated with the first matching request in the Cache object.
If no match is found, the Promise resolves to GlobalObject.Undefined.

[Value("match")]
public Task<Response> Match(Union42 request, CacheQueryOptions options = null)

Parameters

request Union42
options CacheQueryOptions

Returns

Task<Response>

A {{jsxref("Promise")}} that resolves to the first Response that matches
the request or to GlobalObject.Undefined if no match is found.

NOTE
Cache.match() is basically identical to
MatchAll(Union42, CacheQueryOptions), except that rather than resolving with an array of
all matching responses, it resolves with the first matching response only (that is,
response[0]).

Remarks

-Using Service Workers
-Cache
-Window.Caches and WorkerGlobalScope.Caches

See also on MDN

MatchAll(Union42, CacheQueryOptions)

The matchAll() method of the Cache
interface returns a Promise that resolves to an array of all matching
responses in the Cache object.

[Value("matchAll")]
public Task<Response[]> MatchAll(Union42 request = default, CacheQueryOptions options = null)

Parameters

request Union42
options CacheQueryOptions

Returns

Task<Response[]>

A Promise that resolves to an array of all matching responses in the
Cache object.

NOTE
Match(Union42, CacheQueryOptions) is basically identical to
Cache.matchAll(), except that rather than resolving with an array of all
matching responses, it resolves with the first matching response only (that is,
response[0]).

Remarks

-Using Service Workers
-Cache
-Window.Caches and WorkerGlobalScope.Caches

See also on MDN

Put(Union42, Response)

The put() method of the
Cache interface allows key/value pairs to be added to the current
Cache object.

[Value("put")]
public Task<GlobalObject.Undefined> Put(Union42 request, Response response)

Parameters

request Union42
response Response

Returns

Task<GlobalObject.Undefined>

A Promise that resolves with undefined.

Remarks

Often, you will just want to Windowfetch
one or more requests, then add the result straight to your cache. In such cases you are
better off using
Add(Union42)/AddAll(List<Union42>), as
they are shorthand functions for one or more of these operations.

NOTE

put() will overwrite any key/value pair
previously stored in the cache that matches the request.

NOTE

Add(Union42)/AddAll(List<Union42>) do not
cache responses with Response.status values that are not in the 200
range, whereas Cache.put lets you store any request/response pair. As a
result, Add(Union42)/AddAll(List<Union42>) can't be used to store
opaque responses, whereas Cache.put can.

-Using Service Workers
-Cache
-Window.Caches and WorkerGlobalScope.Caches

See also on MDN