Skip to content

Commit 253b998

Browse files
authored
Memory LRU GC (#6943)
Add LRU GC for memory cache.
1 parent 2d141ed commit 253b998

31 files changed

+792
-166
lines changed

.changeset/olive-cycles-count.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': minor
3+
'@firebase/firestore': minor
4+
---
5+
6+
Introduces a new LRU garbage document collector for memory cache.

common/api-review/firestore.api.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,42 @@ export interface LoadBundleTaskProgress {
331331

332332
export { LogLevel }
333333

334+
// @public
335+
export interface MemoryCacheSettings {
336+
garbageCollector?: MemoryGarbageCollector;
337+
}
338+
339+
// @public
340+
export interface MemoryEagerGarbageCollector {
341+
// (undocumented)
342+
kind: 'memoryEager';
343+
}
344+
345+
// @public
346+
export function memoryEagerGarbageCollector(): MemoryEagerGarbageCollector;
347+
348+
// @public
349+
export type MemoryGarbageCollector = MemoryEagerGarbageCollector | MemoryLruGarbageCollector;
350+
334351
// @public
335352
export interface MemoryLocalCache {
336353
// (undocumented)
337354
kind: 'memory';
338355
}
339356

340357
// @public
341-
export function memoryLocalCache(): MemoryLocalCache;
358+
export function memoryLocalCache(settings?: MemoryCacheSettings): MemoryLocalCache;
359+
360+
// @public
361+
export interface MemoryLruGarbageCollector {
362+
// (undocumented)
363+
kind: 'memoryLru';
364+
}
365+
366+
// @public
367+
export function memoryLruGarbageCollector(settings?: {
368+
cacheSizeBytes?: number;
369+
}): MemoryLruGarbageCollector;
342370

343371
// @public
344372
export function namedQuery(firestore: Firestore, name: string): Promise<Query | null>;

docs-devsite/firestore_.md

+65-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ https://github.com/firebase/firebase-js-sdk
4242
| [deleteField()](./firestore_.md#deletefield) | Returns a sentinel for use with [updateDoc()](./firestore_lite.md#updatedoc) or [setDoc()](./firestore_lite.md#setdoc) with <code>{merge: true}</code> to mark a field for deletion. |
4343
| [documentId()](./firestore_.md#documentid) | Returns a special sentinel <code>FieldPath</code> to refer to the ID of a document. It can be used in queries to sort or filter by the document ID. |
4444
| [getFirestore()](./firestore_.md#getfirestore) | Returns the existing default [Firestore](./firestore_.firestore.md#firestore_class) instance that is associated with the default [FirebaseApp](./app.firebaseapp.md#firebaseapp_interface)<!-- -->. If no instance exists, initializes a new instance with default settings. |
45-
| [memoryLocalCache()](./firestore_.md#memorylocalcache) | Creates an instance of <code>MemoryLocalCache</code>. The instance can be set to <code>FirestoreSettings.cache</code> to tell the SDK which cache layer to use. |
45+
| [memoryEagerGarbageCollector()](./firestore_.md#memoryeagergarbagecollector) | Creates an instance of <code>MemoryEagerGarbageCollector</code>. This is also the default garbage collector unless it is explicitly specified otherwise. |
4646
| [persistentMultipleTabManager()](./firestore_.md#persistentmultipletabmanager) | Creates an instance of <code>PersistentMultipleTabManager</code>. |
4747
| [serverTimestamp()](./firestore_.md#servertimestamp) | Returns a sentinel used with [setDoc()](./firestore_lite.md#setdoc) or [updateDoc()](./firestore_lite.md#updatedoc) to include a server-generated timestamp in the written data. |
4848
| <b>function(elements...)</b> |
@@ -101,6 +101,8 @@ https://github.com/firebase/firebase-js-sdk
101101
| [updateDoc(reference, data)](./firestore_.md#updatedoc) | Updates fields in the document referred to by the specified <code>DocumentReference</code>. The update will fail if applied to a document that does not exist. |
102102
| [updateDoc(reference, field, value, moreFieldsAndValues)](./firestore_.md#updatedoc) | Updates fields in the document referred to by the specified <code>DocumentReference</code> The update will fail if applied to a document that does not exist.<!-- -->Nested fields can be updated by providing dot-separated field path strings or by providing <code>FieldPath</code> objects. |
103103
| <b>function(settings...)</b> |
104+
| [memoryLocalCache(settings)](./firestore_.md#memorylocalcache) | Creates an instance of <code>MemoryLocalCache</code>. The instance can be set to <code>FirestoreSettings.cache</code> to tell the SDK which cache layer to use. |
105+
| [memoryLruGarbageCollector(settings)](./firestore_.md#memorylrugarbagecollector) | Creates an instance of <code>MemoryLruGarbageCollector</code>.<!-- -->A target size can be specified as part of the setting parameter. The collector will start deleting documents once the cache size exceeds the given size. The default cache size is 40MB (40 \* 1024 \* 1024 bytes). |
104106
| [persistentLocalCache(settings)](./firestore_.md#persistentlocalcache) | Creates an instance of <code>PersistentLocalCache</code>. The instance can be set to <code>FirestoreSettings.cache</code> to tell the SDK which cache layer to use. |
105107
| [persistentSingleTabManager(settings)](./firestore_.md#persistentsingletabmanager) | Creates an instance of <code>PersistentSingleTabManager</code>. |
106108
| <b>function(snapshot...)</b> |
@@ -153,7 +155,10 @@ https://github.com/firebase/firebase-js-sdk
153155
| [IndexConfiguration](./firestore_.indexconfiguration.md#indexconfiguration_interface) | <b><i>(BETA)</i></b> A list of Firestore indexes to speed up local query execution.<!-- -->See [JSON Format](https://firebase.google.com/docs/reference/firestore/indexes/#json_format) for a description of the format of the index definition. |
154156
| [IndexField](./firestore_.indexfield.md#indexfield_interface) | <b><i>(BETA)</i></b> A single field element in an index configuration. |
155157
| [LoadBundleTaskProgress](./firestore_.loadbundletaskprogress.md#loadbundletaskprogress_interface) | Represents a progress update or a final state from loading bundles. |
158+
| [MemoryCacheSettings](./firestore_.memorycachesettings.md#memorycachesettings_interface) | An settings object to configure an <code>MemoryLocalCache</code> instance. |
159+
| [MemoryEagerGarbageCollector](./firestore_.memoryeagergarbagecollector.md#memoryeagergarbagecollector_interface) | A garbage collector deletes documents whenever they are not part of any active queries, and have no local mutations attached to them.<!-- -->This collector tries to ensure lowest memory footprints from the SDK, at the risk of documents not being cached for offline queries or for direct queries to the cache.<!-- -->Use factory function to create an instance of this collector. |
156160
| [MemoryLocalCache](./firestore_.memorylocalcache.md#memorylocalcache_interface) | Provides an in-memory cache to the SDK. This is the default cache unless explicitly configured otherwise.<!-- -->To use, create an instance using the factory function , then set the instance to <code>FirestoreSettings.cache</code> and call <code>initializeFirestore</code> using the settings object. |
161+
| [MemoryLruGarbageCollector](./firestore_.memorylrugarbagecollector.md#memorylrugarbagecollector_interface) | A garbage collector deletes Least-Recently-Used documents in multiple batches.<!-- -->This collector is configured with a target size, and will only perform collection when the cached documents exceed the target size. It avoids querying backend repeated for the same query or document, at the risk of having a larger memory footprint.<!-- -->Use factory function to create a instance of this collector. |
157162
| [PersistenceSettings](./firestore_.persistencesettings.md#persistencesettings_interface) | Settings that can be passed to <code>enableIndexedDbPersistence()</code> to configure Firestore persistence. |
158163
| [PersistentCacheSettings](./firestore_.persistentcachesettings.md#persistentcachesettings_interface) | An settings object to configure an <code>PersistentLocalCache</code> instance. |
159164
| [PersistentLocalCache](./firestore_.persistentlocalcache.md#persistentlocalcache_interface) | Provides a persistent cache backed by IndexedDb to the SDK.<!-- -->To use, create an instance using the factory function , then set the instance to <code>FirestoreSettings.cache</code> and call <code>initializeFirestore</code> using the settings object. |
@@ -182,6 +187,7 @@ https://github.com/firebase/firebase-js-sdk
182187
| [DocumentChangeType](./firestore_.md#documentchangetype) | The type of a <code>DocumentChange</code> may be 'added', 'removed', or 'modified'. |
183188
| [FirestoreErrorCode](./firestore_.md#firestoreerrorcode) | The set of Firestore status codes. The codes are the same at the ones exposed by gRPC here: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md<!-- -->Possible values: - 'cancelled': The operation was cancelled (typically by the caller). - 'unknown': Unknown error or an error from a different error domain. - 'invalid-argument': Client specified an invalid argument. Note that this differs from 'failed-precondition'. 'invalid-argument' indicates arguments that are problematic regardless of the state of the system (e.g. an invalid field name). - 'deadline-exceeded': Deadline expired before operation could complete. For operations that change the state of the system, this error may be returned even if the operation has completed successfully. For example, a successful response from a server could have been delayed long enough for the deadline to expire. - 'not-found': Some requested document was not found. - 'already-exists': Some document that we attempted to create already exists. - 'permission-denied': The caller does not have permission to execute the specified operation. - 'resource-exhausted': Some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system is out of space. - 'failed-precondition': Operation was rejected because the system is not in a state required for the operation's execution. - 'aborted': The operation was aborted, typically due to a concurrency issue like transaction aborts, etc. - 'out-of-range': Operation was attempted past the valid range. - 'unimplemented': Operation is not implemented or not supported/enabled. - 'internal': Internal errors. Means some invariants expected by underlying system has been broken. If you see one of these errors, something is very broken. - 'unavailable': The service is currently unavailable. This is most likely a transient condition and may be corrected by retrying with a backoff. - 'data-loss': Unrecoverable data loss or corruption. - 'unauthenticated': The request does not have valid authentication credentials for the operation. |
184189
| [FirestoreLocalCache](./firestore_.md#firestorelocalcache) | Union type from all supported SDK cache layer. |
190+
| [MemoryGarbageCollector](./firestore_.md#memorygarbagecollector) | Union type from all support gabage collectors for memory local cache. |
185191
| [NestedUpdateFields](./firestore_.md#nestedupdatefields) | For each field (e.g. 'bar'), find all nested keys (e.g. {<!-- -->'bar.baz': T1, 'bar.qux': T2<!-- -->}<!-- -->). Intersect them together to make a single map containing all possible keys that are all marked as optional |
186192
| [OrderByDirection](./firestore_.md#orderbydirection) | The direction of a [orderBy()](./firestore_.md#orderby) clause is specified as 'desc' or 'asc' (descending or ascending). |
187193
| [PartialWithFieldValue](./firestore_.md#partialwithfieldvalue) | Similar to Typescript's <code>Partial&lt;T&gt;</code>, but allows nested fields to be omitted and FieldValues to be passed in as property values. |
@@ -807,18 +813,18 @@ export declare function getFirestore(): Firestore;
807813

808814
The [Firestore](./firestore_.firestore.md#firestore_class) instance of the provided app.
809815

810-
## memoryLocalCache()
816+
## memoryEagerGarbageCollector()
811817

812-
Creates an instance of `MemoryLocalCache`<!-- -->. The instance can be set to `FirestoreSettings.cache` to tell the SDK which cache layer to use.
818+
Creates an instance of `MemoryEagerGarbageCollector`<!-- -->. This is also the default garbage collector unless it is explicitly specified otherwise.
813819

814820
<b>Signature:</b>
815821

816822
```typescript
817-
export declare function memoryLocalCache(): MemoryLocalCache;
823+
export declare function memoryEagerGarbageCollector(): MemoryEagerGarbageCollector;
818824
```
819825
<b>Returns:</b>
820826

821-
[MemoryLocalCache](./firestore_.memorylocalcache.md#memorylocalcache_interface)
827+
[MemoryEagerGarbageCollector](./firestore_.memoryeagergarbagecollector.md#memoryeagergarbagecollector_interface)
822828

823829
## persistentMultipleTabManager()
824830

@@ -1954,6 +1960,50 @@ Promise&lt;void&gt;
19541960

19551961
A `Promise` resolved once the data has been successfully written to the backend (note that it won't resolve while you're offline).
19561962

1963+
## memoryLocalCache()
1964+
1965+
Creates an instance of `MemoryLocalCache`<!-- -->. The instance can be set to `FirestoreSettings.cache` to tell the SDK which cache layer to use.
1966+
1967+
<b>Signature:</b>
1968+
1969+
```typescript
1970+
export declare function memoryLocalCache(settings?: MemoryCacheSettings): MemoryLocalCache;
1971+
```
1972+
1973+
### Parameters
1974+
1975+
| Parameter | Type | Description |
1976+
| --- | --- | --- |
1977+
| settings | [MemoryCacheSettings](./firestore_.memorycachesettings.md#memorycachesettings_interface) | |
1978+
1979+
<b>Returns:</b>
1980+
1981+
[MemoryLocalCache](./firestore_.memorylocalcache.md#memorylocalcache_interface)
1982+
1983+
## memoryLruGarbageCollector()
1984+
1985+
Creates an instance of `MemoryLruGarbageCollector`<!-- -->.
1986+
1987+
A target size can be specified as part of the setting parameter. The collector will start deleting documents once the cache size exceeds the given size. The default cache size is 40MB (40 \* 1024 \* 1024 bytes).
1988+
1989+
<b>Signature:</b>
1990+
1991+
```typescript
1992+
export declare function memoryLruGarbageCollector(settings?: {
1993+
cacheSizeBytes?: number;
1994+
}): MemoryLruGarbageCollector;
1995+
```
1996+
1997+
### Parameters
1998+
1999+
| Parameter | Type | Description |
2000+
| --- | --- | --- |
2001+
| settings | { cacheSizeBytes?: number; } | |
2002+
2003+
<b>Returns:</b>
2004+
2005+
[MemoryLruGarbageCollector](./firestore_.memorylrugarbagecollector.md#memorylrugarbagecollector_interface)
2006+
19572007
## persistentLocalCache()
19582008

19592009
Creates an instance of `PersistentLocalCache`<!-- -->. The instance can be set to `FirestoreSettings.cache` to tell the SDK which cache layer to use.
@@ -2172,6 +2222,16 @@ Union type from all supported SDK cache layer.
21722222
export declare type FirestoreLocalCache = MemoryLocalCache | PersistentLocalCache;
21732223
```
21742224

2225+
## MemoryGarbageCollector
2226+
2227+
Union type from all support gabage collectors for memory local cache.
2228+
2229+
<b>Signature:</b>
2230+
2231+
```typescript
2232+
export declare type MemoryGarbageCollector = MemoryEagerGarbageCollector | MemoryLruGarbageCollector;
2233+
```
2234+
21752235
## NestedUpdateFields
21762236

21772237
For each field (e.g. 'bar'), find all nested keys (e.g. {<!-- -->'bar.baz': T1, 'bar.qux': T2<!-- -->}<!-- -->). Intersect them together to make a single map containing all possible keys that are all marked as optional
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# MemoryCacheSettings interface
13+
An settings object to configure an `MemoryLocalCache` instance.
14+
15+
<b>Signature:</b>
16+
17+
```typescript
18+
export declare interface MemoryCacheSettings
19+
```
20+
21+
## Properties
22+
23+
| Property | Type | Description |
24+
| --- | --- | --- |
25+
| [garbageCollector](./firestore_.memorycachesettings.md#memorycachesettingsgarbagecollector) | [MemoryGarbageCollector](./firestore_.md#memorygarbagecollector) | The garbage collector to use, for the memory cache layer. A <code>MemoryEagerGarbageCollector</code> is used when this is undefined. |
26+
27+
## MemoryCacheSettings.garbageCollector
28+
29+
The garbage collector to use, for the memory cache layer. A `MemoryEagerGarbageCollector` is used when this is undefined.
30+
31+
<b>Signature:</b>
32+
33+
```typescript
34+
garbageCollector?: MemoryGarbageCollector;
35+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# MemoryEagerGarbageCollector interface
13+
A garbage collector deletes documents whenever they are not part of any active queries, and have no local mutations attached to them.
14+
15+
This collector tries to ensure lowest memory footprints from the SDK, at the risk of documents not being cached for offline queries or for direct queries to the cache.
16+
17+
Use factory function to create an instance of this collector.
18+
19+
<b>Signature:</b>
20+
21+
```typescript
22+
export declare interface MemoryEagerGarbageCollector
23+
```
24+
25+
## Properties
26+
27+
| Property | Type | Description |
28+
| --- | --- | --- |
29+
| [kind](./firestore_.memoryeagergarbagecollector.md#memoryeagergarbagecollectorkind) | 'memoryEager' | |
30+
31+
## MemoryEagerGarbageCollector.kind
32+
33+
<b>Signature:</b>
34+
35+
```typescript
36+
kind: 'memoryEager';
37+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# MemoryLruGarbageCollector interface
13+
A garbage collector deletes Least-Recently-Used documents in multiple batches.
14+
15+
This collector is configured with a target size, and will only perform collection when the cached documents exceed the target size. It avoids querying backend repeated for the same query or document, at the risk of having a larger memory footprint.
16+
17+
Use factory function to create a instance of this collector.
18+
19+
<b>Signature:</b>
20+
21+
```typescript
22+
export declare interface MemoryLruGarbageCollector
23+
```
24+
25+
## Properties
26+
27+
| Property | Type | Description |
28+
| --- | --- | --- |
29+
| [kind](./firestore_.memorylrugarbagecollector.md#memorylrugarbagecollectorkind) | 'memoryLru' | |
30+
31+
## MemoryLruGarbageCollector.kind
32+
33+
<b>Signature:</b>
34+
35+
```typescript
36+
kind: 'memoryLru';
37+
```

0 commit comments

Comments
 (0)