@fluidframework/driver-web-cache 2.0.0-dev.3.1.0.125672 → 2.0.0-dev.4.1.0.148229
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/FluidCache.d.ts +0 -4
- package/dist/FluidCache.d.ts.map +1 -1
- package/dist/FluidCache.js +32 -31
- package/dist/FluidCache.js.map +1 -1
- package/dist/FluidCacheIndexedDb.d.ts +4 -2
- package/dist/FluidCacheIndexedDb.d.ts.map +1 -1
- package/dist/FluidCacheIndexedDb.js +2 -2
- package/dist/FluidCacheIndexedDb.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/lib/FluidCache.d.ts +0 -4
- package/lib/FluidCache.d.ts.map +1 -1
- package/lib/FluidCache.js +32 -31
- package/lib/FluidCache.js.map +1 -1
- package/lib/FluidCacheIndexedDb.d.ts +4 -2
- package/lib/FluidCacheIndexedDb.d.ts.map +1 -1
- package/lib/FluidCacheIndexedDb.js +2 -2
- package/lib/FluidCacheIndexedDb.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/package.json +30 -35
- package/src/FluidCache.ts +32 -35
- package/src/FluidCacheIndexedDb.ts +7 -3
- package/src/packageVersion.ts +1 -1
package/dist/FluidCache.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
1
|
import { IPersistedCache, ICacheEntry, IFileEntry } from "@fluidframework/odsp-driver-definitions";
|
|
6
2
|
import { ITelemetryBaseLogger } from "@fluidframework/common-definitions";
|
|
7
3
|
export interface FluidCacheConfig {
|
package/dist/FluidCache.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCache.d.ts","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FluidCache.d.ts","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,yCAAyC,CAAC;AACnG,OAAO,EAAE,oBAAoB,EAAoB,MAAM,oCAAoC,CAAC;AAqB5F,MAAM,WAAW,gBAAgB;IAChC;;;;;;OAMG;IAEH,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAE5B;;OAEG;IACH,MAAM,CAAC,EAAE,oBAAoB,CAAC;IAE9B;;;OAGG;IACH,eAAe,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,qBAAa,UAAW,YAAW,eAAe;IACjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmB;IAE1C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAgB;IAE7C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;gBAE7B,MAAM,EAAE,gBAAgB;IA2DvB,aAAa,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAwB9C,GAAG,CAAC,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;YAmBzC,gBAAgB;IAkDjB,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;CAiC/D"}
|
package/dist/FluidCache.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/*!
|
|
3
|
-
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
4
|
-
* Licensed under the MIT License.
|
|
5
|
-
*/
|
|
6
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
3
|
exports.FluidCache = void 0;
|
|
8
4
|
const telemetry_utils_1 = require("@fluidframework/telemetry-utils");
|
|
@@ -40,13 +36,14 @@ class FluidCache {
|
|
|
40
36
|
}
|
|
41
37
|
});
|
|
42
38
|
(0, scheduleIdleTask_1.scheduleIdleTask)(async () => {
|
|
39
|
+
let db;
|
|
43
40
|
// Delete entries that have not been accessed recently to clean up space
|
|
44
41
|
try {
|
|
45
|
-
|
|
42
|
+
db = await (0, FluidCacheIndexedDb_1.getFluidCacheIndexedDbInstance)(this.logger);
|
|
46
43
|
const transaction = db.transaction(FluidCacheIndexedDb_1.FluidDriverObjectStoreName, "readwrite");
|
|
47
|
-
const index = transaction.store.index("
|
|
48
|
-
// Get items
|
|
49
|
-
const keysToDelete = await index.getAllKeys(IDBKeyRange.upperBound(new Date().getTime() -
|
|
44
|
+
const index = transaction.store.index("createdTimeMs");
|
|
45
|
+
// Get items which were cached before the maxCacheItemAge.
|
|
46
|
+
const keysToDelete = await index.getAllKeys(IDBKeyRange.upperBound(new Date().getTime() - this.maxCacheItemAge));
|
|
50
47
|
await Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));
|
|
51
48
|
await transaction.done;
|
|
52
49
|
}
|
|
@@ -55,11 +52,15 @@ class FluidCache {
|
|
|
55
52
|
eventName: "FluidCacheDeleteOldEntriesError" /* FluidCacheDeleteOldEntriesError */,
|
|
56
53
|
}, error);
|
|
57
54
|
}
|
|
55
|
+
finally {
|
|
56
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
57
|
+
}
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
60
|
async removeEntries(file) {
|
|
61
|
+
let db;
|
|
61
62
|
try {
|
|
62
|
-
|
|
63
|
+
db = await (0, FluidCacheIndexedDb_1.getFluidCacheIndexedDbInstance)(this.logger);
|
|
63
64
|
const transaction = db.transaction(FluidCacheIndexedDb_1.FluidDriverObjectStoreName, "readwrite");
|
|
64
65
|
const index = transaction.store.index("fileId");
|
|
65
66
|
const keysToDelete = await index.getAllKeys(file.docId);
|
|
@@ -71,6 +72,9 @@ class FluidCache {
|
|
|
71
72
|
eventName: "FluidCacheDeleteOldEntriesError" /* FluidCacheDeleteOldEntriesError */,
|
|
72
73
|
}, error);
|
|
73
74
|
}
|
|
75
|
+
finally {
|
|
76
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
77
|
+
}
|
|
74
78
|
}
|
|
75
79
|
async get(cacheEntry) {
|
|
76
80
|
const startTime = performance.now();
|
|
@@ -80,17 +84,23 @@ class FluidCache {
|
|
|
80
84
|
cacheHit: cachedItem !== undefined,
|
|
81
85
|
type: cacheEntry.type,
|
|
82
86
|
duration: performance.now() - startTime,
|
|
87
|
+
dbOpenPerf: cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.dbOpenPerf,
|
|
88
|
+
dbClosePerf: cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.dbClosePerf,
|
|
83
89
|
});
|
|
84
90
|
// Value will contain metadata like the expiry time, we just want to return the object we were asked to cache
|
|
85
91
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
86
92
|
return cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.cachedObject;
|
|
87
93
|
}
|
|
88
94
|
async getItemFromCache(cacheEntry) {
|
|
95
|
+
let db;
|
|
89
96
|
try {
|
|
90
97
|
const key = (0, FluidCacheIndexedDb_1.getKeyForCacheEntry)(cacheEntry);
|
|
91
|
-
const
|
|
98
|
+
const dbOpenStartTime = performance.now();
|
|
99
|
+
db = await (0, FluidCacheIndexedDb_1.getFluidCacheIndexedDbInstance)(this.logger);
|
|
100
|
+
const dbOpenPerf = performance.now() - dbOpenStartTime;
|
|
92
101
|
const value = await db.get(FluidCacheIndexedDb_1.FluidDriverObjectStoreName, key);
|
|
93
102
|
if (!value) {
|
|
103
|
+
db.close();
|
|
94
104
|
return undefined;
|
|
95
105
|
}
|
|
96
106
|
// If the data does not come from the same partition, don't return it
|
|
@@ -99,44 +109,32 @@ class FluidCache {
|
|
|
99
109
|
eventName: "FluidCachePartitionKeyMismatch" /* FluidCachePartitionKeyMismatch */,
|
|
100
110
|
subCategory: "FluidCache" /* FluidCache */,
|
|
101
111
|
});
|
|
112
|
+
db.close();
|
|
102
113
|
return undefined;
|
|
103
114
|
}
|
|
104
115
|
const currentTime = new Date().getTime();
|
|
105
116
|
// If too much time has passed since this cache entry was used, we will also return undefined
|
|
106
117
|
if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {
|
|
118
|
+
db.close();
|
|
107
119
|
return undefined;
|
|
108
120
|
}
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
.get(key)
|
|
114
|
-
.then(async (valueToUpdate) => {
|
|
115
|
-
// This value in the database could have been updated concurrently by other tabs/iframes
|
|
116
|
-
// since we first read it. Only update the last accessed time if the current value in the
|
|
117
|
-
// DB was the same one we returned.
|
|
118
|
-
if (valueToUpdate !== undefined &&
|
|
119
|
-
valueToUpdate.createdTimeMs === value.createdTimeMs &&
|
|
120
|
-
(valueToUpdate.lastAccessTimeMs === undefined ||
|
|
121
|
-
valueToUpdate.lastAccessTimeMs < currentTime)) {
|
|
122
|
-
await transaction.store.put(Object.assign(Object.assign({}, valueToUpdate), { lastAccessTimeMs: currentTime }), key);
|
|
123
|
-
}
|
|
124
|
-
await transaction.done;
|
|
125
|
-
db.close();
|
|
126
|
-
})
|
|
127
|
-
.catch(() => { });
|
|
128
|
-
return value;
|
|
121
|
+
const dbCloseStartTime = performance.now();
|
|
122
|
+
db.close();
|
|
123
|
+
const dbClosePerf = performance.now() - dbCloseStartTime;
|
|
124
|
+
return Object.assign(Object.assign({}, value), { dbOpenPerf, dbClosePerf });
|
|
129
125
|
}
|
|
130
126
|
catch (error) {
|
|
131
127
|
// We can fail to open the db for a variety of reasons,
|
|
132
128
|
// such as the database version having upgraded underneath us. Return undefined in this case
|
|
133
129
|
this.logger.sendErrorEvent({ eventName: "FluidCacheGetError" /* FluidCacheGetError */ }, error);
|
|
130
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
134
131
|
return undefined;
|
|
135
132
|
}
|
|
136
133
|
}
|
|
137
134
|
async put(entry, value) {
|
|
135
|
+
let db;
|
|
138
136
|
try {
|
|
139
|
-
|
|
137
|
+
db = await (0, FluidCacheIndexedDb_1.getFluidCacheIndexedDbInstance)(this.logger);
|
|
140
138
|
const currentTime = new Date().getTime();
|
|
141
139
|
await db.put(FluidCacheIndexedDb_1.FluidDriverObjectStoreName, {
|
|
142
140
|
cachedObject: value,
|
|
@@ -154,6 +152,9 @@ class FluidCache {
|
|
|
154
152
|
// such as the database version having upgraded underneath us
|
|
155
153
|
this.logger.sendErrorEvent({ eventName: "FluidCachePutError" /* FluidCachePutError */ }, error);
|
|
156
154
|
}
|
|
155
|
+
finally {
|
|
156
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
157
|
+
}
|
|
157
158
|
}
|
|
158
159
|
}
|
|
159
160
|
exports.FluidCache = FluidCache;
|
package/dist/FluidCache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAIH,qEAA8D;AAC9D,yDAAsD;AACtD,+DAI+B;AAoC/B;;GAEG;AACH,MAAa,UAAU;IAOtB,YAAY,MAAwB;QACnC,IAAI,CAAC,MAAM,GAAG,6BAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAE9C,IAAA,mCAAgB,EAAC,KAAK,IAAI,EAAE;;YAC3B,oEAAoE;YACpE,wGAAwG;YACxG,gGAAgG;YAChG,IAAI,MAAA,SAAS,CAAC,OAAO,0CAAE,QAAQ,EAAE;gBAChC,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;gBAEpD,gEAAgE;gBAChE,6DAA6D;g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) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IPersistedCache, ICacheEntry, IFileEntry } from \"@fluidframework/odsp-driver-definitions\";\nimport { ITelemetryBaseLogger, ITelemetryLogger } from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n\tgetFluidCacheIndexedDbInstance,\n\tFluidDriverObjectStoreName,\n\tgetKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n\tFluidCacheErrorEvent,\n\tFluidCacheEventSubCategories,\n\tFluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n\tindexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n\t/**\n\t * A string to specify what partition of the cache you wish to use (e.g. a user id).\n\t * Null can be used to explicity indicate no partitioning, and has been chosen\n\t * vs undefined so that it is clear this is an intentional choice by the caller.\n\t * A null value should only be used when the host can ensure that the cache is not able\n\t * to be shared with multiple users.\n\t */\n\t// eslint-disable-next-line @rushstack/no-new-null\n\tpartitionKey: string | null;\n\n\t/**\n\t * A logger that can be used to get insight into cache performance and errors\n\t */\n\tlogger?: ITelemetryBaseLogger;\n\n\t/**\n\t * A value in milliseconds that determines the maximum age of a cache entry to return.\n\t * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n\t */\n\tmaxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n\tprivate readonly logger: ITelemetryLogger;\n\n\tprivate readonly partitionKey: string | null;\n\n\tprivate readonly maxCacheItemAge: number;\n\n\tconstructor(config: FluidCacheConfig) {\n\t\tthis.logger = ChildLogger.create(config.logger);\n\t\tthis.partitionKey = config.partitionKey;\n\t\tthis.maxCacheItemAge = config.maxCacheItemAge;\n\n\t\tscheduleIdleTask(async () => {\n\t\t\t// Log how much storage space is currently being used by indexed db.\n\t\t\t// NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n\t\t\t// Exception added when eslint rule was added, this should be revisited when modifying this code\n\t\t\tif (navigator.storage?.estimate) {\n\t\t\t\tconst estimate = await navigator.storage.estimate();\n\n\t\t\t\t// Some browsers have a usageDetails property that will tell you\n\t\t\t\t// more detailed information on how the storage is being used\n\t\t\t\tlet indexedDBSize: number | undefined;\n\t\t\t\tif (\"usageDetails\" in estimate) {\n\t\t\t\t\tindexedDBSize = ((estimate as any).usageDetails as StorageQuotaUsageDetails)\n\t\t\t\t\t\t.indexedDB;\n\t\t\t\t}\n\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t\tquota: estimate.quota,\n\t\t\t\t\tusage: estimate.usage,\n\t\t\t\t\tindexedDBSize,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\tscheduleIdleTask(async () => {\n\t\t\t// Delete entries that have not been accessed recently to clean up space\n\t\t\ttry {\n\t\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\t\tconst index = transaction.store.index(\"lastAccessTimeMs\");\n\t\t\t\t// Get items that have not been accessed in 4 weeks\n\t\t\t\tconst keysToDelete = await index.getAllKeys(\n\t\t\t\t\tIDBKeyRange.upperBound(new Date().getTime() - 4 * 7 * 24 * 60 * 60 * 1000),\n\t\t\t\t);\n\n\t\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\t\tawait transaction.done;\n\t\t\t} catch (error: any) {\n\t\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t\t{\n\t\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t\t},\n\t\t\t\t\terror,\n\t\t\t\t);\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic async removeEntries(file: IFileEntry): Promise<void> {\n\t\ttry {\n\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\tconst index = transaction.store.index(\"fileId\");\n\n\t\t\tconst keysToDelete = await index.getAllKeys(file.docId);\n\n\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\tawait transaction.done;\n\t\t} catch (error: any) {\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{\n\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t},\n\t\t\t\terror,\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic async get(cacheEntry: ICacheEntry): Promise<any> {\n\t\tconst startTime = performance.now();\n\n\t\tconst cachedItem = await this.getItemFromCache(cacheEntry);\n\n\t\tthis.logger.sendPerformanceEvent({\n\t\t\teventName: \"FluidCacheAccess\",\n\t\t\tcacheHit: cachedItem !== undefined,\n\t\t\ttype: cacheEntry.type,\n\t\t\tduration: performance.now() - startTime,\n\t\t});\n\n\t\t// Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\treturn cachedItem?.cachedObject;\n\t}\n\n\tprivate async getItemFromCache(cacheEntry: ICacheEntry) {\n\t\ttry {\n\t\t\tconst key = getKeyForCacheEntry(cacheEntry);\n\n\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst value = await db.get(FluidDriverObjectStoreName, key);\n\n\t\t\tif (!value) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\t// If the data does not come from the same partition, don't return it\n\t\t\tif (value.partitionKey !== this.partitionKey) {\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t});\n\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\t// If too much time has passed since this cache entry was used, we will also return undefined\n\t\t\tif (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\t// We don't want to block the get return of this function on updating the last accessed time\n\t\t\t// We catch this promise because there is no user bad if this is rejected.\n\t\t\ttransaction.store\n\t\t\t\t.get(key)\n\t\t\t\t.then(async (valueToUpdate) => {\n\t\t\t\t\t// This value in the database could have been updated concurrently by other tabs/iframes\n\t\t\t\t\t// since we first read it. Only update the last accessed time if the current value in the\n\t\t\t\t\t// DB was the same one we returned.\n\t\t\t\t\tif (\n\t\t\t\t\t\tvalueToUpdate !== undefined &&\n\t\t\t\t\t\tvalueToUpdate.createdTimeMs === value.createdTimeMs &&\n\t\t\t\t\t\t(valueToUpdate.lastAccessTimeMs === undefined ||\n\t\t\t\t\t\t\tvalueToUpdate.lastAccessTimeMs < currentTime)\n\t\t\t\t\t) {\n\t\t\t\t\t\tawait transaction.store.put(\n\t\t\t\t\t\t\t{ ...valueToUpdate, lastAccessTimeMs: currentTime },\n\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tawait transaction.done;\n\n\t\t\t\t\tdb.close();\n\t\t\t\t})\n\t\t\t\t.catch(() => {});\n\t\t\treturn value;\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us. Return undefined in this case\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCacheGetError },\n\t\t\t\terror,\n\t\t\t);\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tpublic async put(entry: ICacheEntry, value: any): Promise<void> {\n\t\ttry {\n\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\tawait db.put(\n\t\t\t\tFluidDriverObjectStoreName,\n\t\t\t\t{\n\t\t\t\t\tcachedObject: value,\n\t\t\t\t\tfileId: entry.file.docId,\n\t\t\t\t\ttype: entry.type,\n\t\t\t\t\tcacheItemId: entry.key,\n\t\t\t\t\tpartitionKey: this.partitionKey,\n\t\t\t\t\tcreatedTimeMs: currentTime,\n\t\t\t\t\tlastAccessTimeMs: currentTime,\n\t\t\t\t},\n\t\t\t\tgetKeyForCacheEntry(entry),\n\t\t\t);\n\n\t\t\tdb.close();\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCachePutError },\n\t\t\t\terror,\n\t\t\t);\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":";;;AAOA,qEAA8D;AAC9D,yDAAsD;AACtD,+DAK+B;AAoC/B;;GAEG;AACH,MAAa,UAAU;IAOtB,YAAY,MAAwB;QACnC,IAAI,CAAC,MAAM,GAAG,6BAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAE9C,IAAA,mCAAgB,EAAC,KAAK,IAAI,EAAE;;YAC3B,oEAAoE;YACpE,wGAAwG;YACxG,gGAAgG;YAChG,IAAI,MAAA,SAAS,CAAC,OAAO,0CAAE,QAAQ,EAAE;gBAChC,MAAM,QAAQ,G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,6DAA6D;YAC7D,IAAI,CAAC,MAAM,CAAC,cAAc,CACzB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACL,CAAC;SACF;gBAAS;YACT,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,KAAK,EAAE,CAAC;SACZ;IACF,CAAC;CACD;AAhMD,gCAgMC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { IDBPDatabase } from \"idb\";\nimport { IPersistedCache, ICacheEntry, IFileEntry } from \"@fluidframework/odsp-driver-definitions\";\nimport { ITelemetryBaseLogger, ITelemetryLogger } from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n\tgetFluidCacheIndexedDbInstance,\n\tFluidCacheDBSchema,\n\tFluidDriverObjectStoreName,\n\tgetKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n\tFluidCacheErrorEvent,\n\tFluidCacheEventSubCategories,\n\tFluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n\tindexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n\t/**\n\t * A string to specify what partition of the cache you wish to use (e.g. a user id).\n\t * Null can be used to explicity indicate no partitioning, and has been chosen\n\t * vs undefined so that it is clear this is an intentional choice by the caller.\n\t * A null value should only be used when the host can ensure that the cache is not able\n\t * to be shared with multiple users.\n\t */\n\t// eslint-disable-next-line @rushstack/no-new-null\n\tpartitionKey: string | null;\n\n\t/**\n\t * A logger that can be used to get insight into cache performance and errors\n\t */\n\tlogger?: ITelemetryBaseLogger;\n\n\t/**\n\t * A value in milliseconds that determines the maximum age of a cache entry to return.\n\t * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n\t */\n\tmaxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n\tprivate readonly logger: ITelemetryLogger;\n\n\tprivate readonly partitionKey: string | null;\n\n\tprivate readonly maxCacheItemAge: number;\n\n\tconstructor(config: FluidCacheConfig) {\n\t\tthis.logger = ChildLogger.create(config.logger);\n\t\tthis.partitionKey = config.partitionKey;\n\t\tthis.maxCacheItemAge = config.maxCacheItemAge;\n\n\t\tscheduleIdleTask(async () => {\n\t\t\t// Log how much storage space is currently being used by indexed db.\n\t\t\t// NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n\t\t\t// Exception added when eslint rule was added, this should be revisited when modifying this code\n\t\t\tif (navigator.storage?.estimate) {\n\t\t\t\tconst estimate = await navigator.storage.estimate();\n\n\t\t\t\t// Some browsers have a usageDetails property that will tell you\n\t\t\t\t// more detailed information on how the storage is being used\n\t\t\t\tlet indexedDBSize: number | undefined;\n\t\t\t\tif (\"usageDetails\" in estimate) {\n\t\t\t\t\tindexedDBSize = ((estimate as any).usageDetails as StorageQuotaUsageDetails)\n\t\t\t\t\t\t.indexedDB;\n\t\t\t\t}\n\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t\tquota: estimate.quota,\n\t\t\t\t\tusage: estimate.usage,\n\t\t\t\t\tindexedDBSize,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\tscheduleIdleTask(async () => {\n\t\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\n\t\t\t// Delete entries that have not been accessed recently to clean up space\n\t\t\ttry {\n\t\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\t\tconst index = transaction.store.index(\"createdTimeMs\");\n\t\t\t\t// Get items which were cached before the maxCacheItemAge.\n\t\t\t\tconst keysToDelete = await index.getAllKeys(\n\t\t\t\t\tIDBKeyRange.upperBound(new Date().getTime() - this.maxCacheItemAge),\n\t\t\t\t);\n\n\t\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\t\tawait transaction.done;\n\t\t\t} catch (error: any) {\n\t\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t\t{\n\t\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t\t},\n\t\t\t\t\terror,\n\t\t\t\t);\n\t\t\t} finally {\n\t\t\t\tdb?.close();\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic async removeEntries(file: IFileEntry): Promise<void> {\n\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\t\ttry {\n\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\tconst index = transaction.store.index(\"fileId\");\n\n\t\t\tconst keysToDelete = await index.getAllKeys(file.docId);\n\n\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\tawait transaction.done;\n\t\t} catch (error: any) {\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{\n\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t},\n\t\t\t\terror,\n\t\t\t);\n\t\t} finally {\n\t\t\tdb?.close();\n\t\t}\n\t}\n\n\tpublic async get(cacheEntry: ICacheEntry): Promise<any> {\n\t\tconst startTime = performance.now();\n\n\t\tconst cachedItem = await this.getItemFromCache(cacheEntry);\n\n\t\tthis.logger.sendPerformanceEvent({\n\t\t\teventName: \"FluidCacheAccess\",\n\t\t\tcacheHit: cachedItem !== undefined,\n\t\t\ttype: cacheEntry.type,\n\t\t\tduration: performance.now() - startTime,\n\t\t\tdbOpenPerf: cachedItem?.dbOpenPerf,\n\t\t\tdbClosePerf: cachedItem?.dbClosePerf,\n\t\t});\n\n\t\t// Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\treturn cachedItem?.cachedObject;\n\t}\n\n\tprivate async getItemFromCache(cacheEntry: ICacheEntry) {\n\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\t\ttry {\n\t\t\tconst key = getKeyForCacheEntry(cacheEntry);\n\n\t\t\tconst dbOpenStartTime = performance.now();\n\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\t\t\tconst dbOpenPerf = performance.now() - dbOpenStartTime;\n\t\t\tconst value = await db.get(FluidDriverObjectStoreName, key);\n\n\t\t\tif (!value) {\n\t\t\t\tdb.close();\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\t// If the data does not come from the same partition, don't return it\n\t\t\tif (value.partitionKey !== this.partitionKey) {\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t});\n\n\t\t\t\tdb.close();\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\t// If too much time has passed since this cache entry was used, we will also return undefined\n\t\t\tif (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n\t\t\t\tdb.close();\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst dbCloseStartTime = performance.now();\n\t\t\tdb.close();\n\t\t\tconst dbClosePerf = performance.now() - dbCloseStartTime;\n\t\t\treturn { ...value, dbOpenPerf, dbClosePerf };\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us. Return undefined in this case\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCacheGetError },\n\t\t\t\terror,\n\t\t\t);\n\t\t\tdb?.close();\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tpublic async put(entry: ICacheEntry, value: any): Promise<void> {\n\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\t\ttry {\n\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\tawait db.put(\n\t\t\t\tFluidDriverObjectStoreName,\n\t\t\t\t{\n\t\t\t\t\tcachedObject: value,\n\t\t\t\t\tfileId: entry.file.docId,\n\t\t\t\t\ttype: entry.type,\n\t\t\t\t\tcacheItemId: entry.key,\n\t\t\t\t\tpartitionKey: this.partitionKey,\n\t\t\t\t\tcreatedTimeMs: currentTime,\n\t\t\t\t\tlastAccessTimeMs: currentTime,\n\t\t\t\t},\n\t\t\t\tgetKeyForCacheEntry(entry),\n\t\t\t);\n\n\t\t\tdb.close();\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCachePutError },\n\t\t\t\terror,\n\t\t\t);\n\t\t} finally {\n\t\t\tdb?.close();\n\t\t}\n\t}\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import { DBSchema, IDBPDatabase } from "idb";
|
|
5
|
+
import { DBSchema, DeleteDBCallbacks, IDBPDatabase } from "idb";
|
|
6
6
|
import { ICacheEntry } from "@fluidframework/odsp-driver-definitions";
|
|
7
7
|
import { ITelemetryBaseLogger } from "@fluidframework/common-definitions";
|
|
8
8
|
export declare const FluidDriverCacheDBName = "fluidDriverCache";
|
|
@@ -13,7 +13,7 @@ export declare const oldVersionNameMapping: Partial<{
|
|
|
13
13
|
}>;
|
|
14
14
|
export declare function getKeyForCacheEntry(entry: ICacheEntry): string;
|
|
15
15
|
export declare function getFluidCacheIndexedDbInstance(logger?: ITelemetryBaseLogger): Promise<IDBPDatabase<FluidCacheDBSchema>>;
|
|
16
|
-
export declare function deleteFluidCacheIndexDbInstance(): Promise<void>;
|
|
16
|
+
export declare function deleteFluidCacheIndexDbInstance(deleteDBCallbacks?: DeleteDBCallbacks): Promise<void>;
|
|
17
17
|
/**
|
|
18
18
|
* Schema for the object store used to cache driver information
|
|
19
19
|
*/
|
|
@@ -51,6 +51,8 @@ export interface FluidCacheDBSchema extends DBSchema {
|
|
|
51
51
|
/**
|
|
52
52
|
* The last time the cache entry was used.
|
|
53
53
|
* This is initially set to the time the cache entry was created Measured as ms since unix epoch.
|
|
54
|
+
* With the recent change, this won't be updated on read as it will not be used anywhere. Only keeping
|
|
55
|
+
* so as to not upgrade the schema version.
|
|
54
56
|
*/
|
|
55
57
|
lastAccessTimeMs: number;
|
|
56
58
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCacheIndexedDb.d.ts","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAU,QAAQ,EAAE,YAAY,EAAY,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"FluidCacheIndexedDb.d.ts","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAU,QAAQ,EAAE,iBAAiB,EAAE,YAAY,EAAY,MAAM,KAAK,CAAC;AAClF,OAAO,EAAE,WAAW,EAAE,MAAM,yCAAyC,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAK1E,eAAO,MAAM,sBAAsB,qBAAqB,CAAC;AAGzD,eAAO,MAAM,0BAA0B,qBAAqB,CAAC;AAE7D,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAGrC,eAAO,MAAM,qBAAqB,EAAE,OAAO,CAAC;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAGpE,CAAC;AAEF,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,WAAW,UAErD;AAED,wBAAgB,8BAA8B,CAC7C,MAAM,CAAC,EAAE,oBAAoB,GAC3B,OAAO,CAAC,YAAY,CAAC,kBAAkB,CAAC,CAAC,CAwC3C;AAID,wBAAgB,+BAA+B,CAC9C,iBAAiB,CAAC,EAAE,iBAAiB,GACnC,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;GAEG;AACH,MAAM,WAAW,kBAAmB,SAAQ,QAAQ;IACnD,CAAC,0BAA0B,CAAC,EAAE;QAC7B;;WAEG;QACH,GAAG,EAAE,MAAM,CAAC;QAEZ,KAAK,EAAE;YACN;;eAEG;YACH,MAAM,EAAE,MAAM,CAAC;YAEf;;eAEG;YACH,IAAI,EAAE,MAAM,CAAC;YAEb;;;;eAIG;YACH,WAAW,EAAE,MAAM,CAAC;YAKpB,YAAY,EAAE,GAAG,CAAC;YAElB;;;eAGG;YAEH,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;YAE5B;;eAEG;YACH,aAAa,EAAE,MAAM,CAAC;YAEtB;;;;;eAKG;YACH,gBAAgB,EAAE,MAAM,CAAC;SACzB,CAAC;QAEF,OAAO,EAAE;YACR,aAAa,EAAE,MAAM,CAAC;YACtB,YAAY,EAAE,MAAM,CAAC;YACrB,gBAAgB,EAAE,MAAM,CAAC;YACzB,MAAM,EAAE,MAAM,CAAC;SACf,CAAC;KACF,CAAC;CACF"}
|
|
@@ -58,8 +58,8 @@ function getFluidCacheIndexedDbInstance(logger) {
|
|
|
58
58
|
exports.getFluidCacheIndexedDbInstance = getFluidCacheIndexedDbInstance;
|
|
59
59
|
// Deletes the indexed DB instance.
|
|
60
60
|
// Warning this can throw an error in Firefox incognito, where accessing storage is prohibited.
|
|
61
|
-
function deleteFluidCacheIndexDbInstance() {
|
|
62
|
-
return (0, idb_1.deleteDB)(exports.FluidDriverCacheDBName);
|
|
61
|
+
function deleteFluidCacheIndexDbInstance(deleteDBCallbacks) {
|
|
62
|
+
return (0, idb_1.deleteDB)(exports.FluidDriverCacheDBName, deleteDBCallbacks);
|
|
63
63
|
}
|
|
64
64
|
exports.deleteFluidCacheIndexDbInstance = deleteFluidCacheIndexDbInstance;
|
|
65
65
|
//# sourceMappingURL=FluidCacheIndexedDb.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCacheIndexedDb.js","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,
|
|
1
|
+
{"version":3,"file":"FluidCacheIndexedDb.js","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,6BAAkF;AAGlF,qEAA8D;AAG9D,+DAA+D;AAClD,QAAA,sBAAsB,GAAG,kBAAkB,CAAC;AAEzD,+GAA+G;AAClG,QAAA,0BAA0B,GAAG,kBAAkB,CAAC;AAEhD,QAAA,mBAAmB,GAAG,CAAC,CAAC;AAErC,kGAAkG;AACrF,QAAA,qBAAqB,GAAuC;IACxE,CAAC,EAAE,cAAc;IACjB,CAAC,EAAE,iBAAiB;CACpB,CAAC;AAEF,SAAgB,mBAAmB,CAAC,KAAkB;IACrD,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,GAAG,EAAE,CAAC;AACzD,CAAC;AAFD,kDAEC;AAED,SAAgB,8BAA8B,CAC7C,MAA6B;IAE7B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,IAAA,YAAM,EAAqB,8BAAsB,EAAE,2BAAmB,EAAE;YACvE,OAAO,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE;gBAC3B,IAAI;oBACH,wDAAwD;oBACxD,kDAAkD;oBAClD,MAAM,aAAa,GAAG,6BAAqB,CAAC,UAAU,CAAC,CAAC;oBACxD,IAAI,aAAa,EAAE;wBAClB,sEAAsE;wBACtE,sDAAsD;wBACtD,EAAE,CAAC,iBAAiB,CAAC,aAAoB,CAAC,CAAC;qBAC3C;iBACD;gBAAC,OAAO,KAAU,EAAE;oBACpB,oEAAoE;oBACpE,8CAA8C;oBAC9C,wEAAwE;oBACxE,6BAAW,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CACxC;wBACC,SAAS,+DAAiD;qBAC1D,EACD,KAAK,CACL,CAAC;iBACF;gBAED,MAAM,gBAAgB,GAAG,EAAE,CAAC,iBAAiB,CAAC,kCAA0B,CAAC,CAAC;gBAC1E,gBAAgB,CAAC,WAAW,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC;gBAC/D,gBAAgB,CAAC,WAAW,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC;gBACrE,gBAAgB,CAAC,WAAW,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;gBAC7D,gBAAgB,CAAC,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;YAClD,CAAC;YACD,OAAO,EAAE,GAAG,EAAE;gBACb,MAAM,CACL,IAAI,KAAK,CACR,+EAA+E,CAC/E,CACD,CAAC;YACH,CAAC;SACD,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;AACJ,CAAC;AA1CD,wEA0CC;AAED,mCAAmC;AACnC,+FAA+F;AAC/F,SAAgB,+BAA+B,CAC9C,iBAAqC;IAErC,OAAO,IAAA,cAAQ,EAAC,8BAAsB,EAAE,iBAAiB,CAAC,CAAC;AAC5D,CAAC;AAJD,0EAIC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { openDB, DBSchema, DeleteDBCallbacks, IDBPDatabase, deleteDB } from \"idb\";\nimport { ICacheEntry } from \"@fluidframework/odsp-driver-definitions\";\nimport { ITelemetryBaseLogger } from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { FluidCacheErrorEvent } from \"./fluidCacheTelemetry\";\n\n// The name of the database that we use for caching Fluid info.\nexport const FluidDriverCacheDBName = \"fluidDriverCache\";\n\n// The name of the object store within the indexed db instance that the driver will use to cache Fluid content.\nexport const FluidDriverObjectStoreName = \"driverStorage.V3\";\n\nexport const CurrentCacheVersion = 3;\n\n// Note that V1 and V2 were misspelled as \"diver\", and we need to keep using the misspelling here.\nexport const oldVersionNameMapping: Partial<{ [key: number]: string }> = {\n\t1: \"diverStorage\",\n\t2: \"diverStorage.V2\",\n};\n\nexport function getKeyForCacheEntry(entry: ICacheEntry) {\n\treturn `${entry.file.docId}_${entry.type}_${entry.key}`;\n}\n\nexport function getFluidCacheIndexedDbInstance(\n\tlogger?: ITelemetryBaseLogger,\n): Promise<IDBPDatabase<FluidCacheDBSchema>> {\n\treturn new Promise((resolve, reject) => {\n\t\topenDB<FluidCacheDBSchema>(FluidDriverCacheDBName, CurrentCacheVersion, {\n\t\t\tupgrade: (db, oldVersion) => {\n\t\t\t\ttry {\n\t\t\t\t\t// We changed the format of the object store, so we must\n\t\t\t\t\t// delete the old stores to create a new one in V3\n\t\t\t\t\tconst cacheToDelete = oldVersionNameMapping[oldVersion];\n\t\t\t\t\tif (cacheToDelete) {\n\t\t\t\t\t\t// We don't include the old object stores in the schema, so we need to\n\t\t\t\t\t\t// use a typecast here to prevent IDB from complaining\n\t\t\t\t\t\tdb.deleteObjectStore(cacheToDelete as any);\n\t\t\t\t\t}\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// Catch any error done when attempting to delete the older version.\n\t\t\t\t\t// If the object does not exist db will throw.\n\t\t\t\t\t// We can now assume that the old version is no longer there regardless.\n\t\t\t\t\tChildLogger.create(logger).sendErrorEvent(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldDbError,\n\t\t\t\t\t\t},\n\t\t\t\t\t\terror,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tconst cacheObjectStore = db.createObjectStore(FluidDriverObjectStoreName);\n\t\t\t\tcacheObjectStore.createIndex(\"createdTimeMs\", \"createdTimeMs\");\n\t\t\t\tcacheObjectStore.createIndex(\"lastAccessTimeMs\", \"lastAccessTimeMs\");\n\t\t\t\tcacheObjectStore.createIndex(\"partitionKey\", \"partitionKey\");\n\t\t\t\tcacheObjectStore.createIndex(\"fileId\", \"fileId\");\n\t\t\t},\n\t\t\tblocked: () => {\n\t\t\t\treject(\n\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\"Could not open DB since it is blocked by an older client that has the DB open\",\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t},\n\t\t}).then(resolve, reject);\n\t});\n}\n\n// Deletes the indexed DB instance.\n// Warning this can throw an error in Firefox incognito, where accessing storage is prohibited.\nexport function deleteFluidCacheIndexDbInstance(\n\tdeleteDBCallbacks?: DeleteDBCallbacks,\n): Promise<void> {\n\treturn deleteDB(FluidDriverCacheDBName, deleteDBCallbacks);\n}\n\n/**\n * Schema for the object store used to cache driver information\n */\nexport interface FluidCacheDBSchema extends DBSchema {\n\t[FluidDriverObjectStoreName]: {\n\t\t/**\n\t\t * A unique identifier for an item in the cache. It is a combination of file, type, and cacheItemId\n\t\t */\n\t\tkey: string;\n\n\t\tvalue: {\n\t\t\t/**\n\t\t\t * The identifier of the file associated with the cache entry\n\t\t\t */\n\t\t\tfileId: string;\n\n\t\t\t/**\n\t\t\t * Describes the type of content being cached, such as snapshot\n\t\t\t */\n\t\t\ttype: string;\n\n\t\t\t/**\n\t\t\t * Files may have multiple cached items associated with them,\n\t\t\t * this property uniquely identifies a specific cache entry for a file.\n\t\t\t * This is not globally unique, but rather a unique id for this file\n\t\t\t */\n\t\t\tcacheItemId: string;\n\n\t\t\t/*\n\t\t\t * Opaque object that the driver asks us to store in a cache for performance reasons\n\t\t\t */\n\t\t\tcachedObject: any;\n\n\t\t\t/**\n\t\t\t * A string to specify what partition of the cache you wish to use (e.g. a user id).\n\t\t\t * Null can be used to explicity indicate no partitioning.\n\t\t\t */\n\t\t\t// eslint-disable-next-line @rushstack/no-new-null\n\t\t\tpartitionKey: string | null;\n\n\t\t\t/**\n\t\t\t * The time when the cache entry was put into the cache\n\t\t\t */\n\t\t\tcreatedTimeMs: number;\n\n\t\t\t/**\n\t\t\t * The last time the cache entry was used.\n\t\t\t * This is initially set to the time the cache entry was created Measured as ms since unix epoch.\n\t\t\t * With the recent change, this won't be updated on read as it will not be used anywhere. Only keeping\n\t\t\t * so as to not upgrade the schema version.\n\t\t\t */\n\t\t\tlastAccessTimeMs: number;\n\t\t};\n\n\t\tindexes: {\n\t\t\tcreatedTimeMs: number;\n\t\t\tpartitionKey: string;\n\t\t\tlastAccessTimeMs: number;\n\t\t\tfileId: string;\n\t\t};\n\t};\n}\n"]}
|
package/dist/packageVersion.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export declare const pkgName = "@fluidframework/driver-web-cache";
|
|
8
|
-
export declare const pkgVersion = "2.0.0-dev.
|
|
8
|
+
export declare const pkgVersion = "2.0.0-dev.4.1.0.148229";
|
|
9
9
|
//# sourceMappingURL=packageVersion.d.ts.map
|
package/dist/packageVersion.js
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.pkgVersion = exports.pkgName = void 0;
|
|
10
10
|
exports.pkgName = "@fluidframework/driver-web-cache";
|
|
11
|
-
exports.pkgVersion = "2.0.0-dev.
|
|
11
|
+
exports.pkgVersion = "2.0.0-dev.4.1.0.148229";
|
|
12
12
|
//# sourceMappingURL=packageVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,kCAAkC,CAAC;AAC7C,QAAA,UAAU,GAAG,wBAAwB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"2.0.0-dev.
|
|
1
|
+
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,kCAAkC,CAAC;AAC7C,QAAA,UAAU,GAAG,wBAAwB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"2.0.0-dev.4.1.0.148229\";\n"]}
|
package/lib/FluidCache.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
1
|
import { IPersistedCache, ICacheEntry, IFileEntry } from "@fluidframework/odsp-driver-definitions";
|
|
6
2
|
import { ITelemetryBaseLogger } from "@fluidframework/common-definitions";
|
|
7
3
|
export interface FluidCacheConfig {
|
package/lib/FluidCache.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCache.d.ts","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FluidCache.d.ts","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,yCAAyC,CAAC;AACnG,OAAO,EAAE,oBAAoB,EAAoB,MAAM,oCAAoC,CAAC;AAqB5F,MAAM,WAAW,gBAAgB;IAChC;;;;;;OAMG;IAEH,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAE5B;;OAEG;IACH,MAAM,CAAC,EAAE,oBAAoB,CAAC;IAE9B;;;OAGG;IACH,eAAe,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,qBAAa,UAAW,YAAW,eAAe;IACjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmB;IAE1C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAgB;IAE7C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;gBAE7B,MAAM,EAAE,gBAAgB;IA2DvB,aAAa,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAwB9C,GAAG,CAAC,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;YAmBzC,gBAAgB;IAkDjB,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;CAiC/D"}
|
package/lib/FluidCache.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
1
|
import { ChildLogger } from "@fluidframework/telemetry-utils";
|
|
6
2
|
import { scheduleIdleTask } from "./scheduleIdleTask";
|
|
7
3
|
import { getFluidCacheIndexedDbInstance, FluidDriverObjectStoreName, getKeyForCacheEntry, } from "./FluidCacheIndexedDb";
|
|
@@ -37,13 +33,14 @@ export class FluidCache {
|
|
|
37
33
|
}
|
|
38
34
|
});
|
|
39
35
|
scheduleIdleTask(async () => {
|
|
36
|
+
let db;
|
|
40
37
|
// Delete entries that have not been accessed recently to clean up space
|
|
41
38
|
try {
|
|
42
|
-
|
|
39
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
43
40
|
const transaction = db.transaction(FluidDriverObjectStoreName, "readwrite");
|
|
44
|
-
const index = transaction.store.index("
|
|
45
|
-
// Get items
|
|
46
|
-
const keysToDelete = await index.getAllKeys(IDBKeyRange.upperBound(new Date().getTime() -
|
|
41
|
+
const index = transaction.store.index("createdTimeMs");
|
|
42
|
+
// Get items which were cached before the maxCacheItemAge.
|
|
43
|
+
const keysToDelete = await index.getAllKeys(IDBKeyRange.upperBound(new Date().getTime() - this.maxCacheItemAge));
|
|
47
44
|
await Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));
|
|
48
45
|
await transaction.done;
|
|
49
46
|
}
|
|
@@ -52,11 +49,15 @@ export class FluidCache {
|
|
|
52
49
|
eventName: "FluidCacheDeleteOldEntriesError" /* FluidCacheDeleteOldEntriesError */,
|
|
53
50
|
}, error);
|
|
54
51
|
}
|
|
52
|
+
finally {
|
|
53
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
54
|
+
}
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
async removeEntries(file) {
|
|
58
|
+
let db;
|
|
58
59
|
try {
|
|
59
|
-
|
|
60
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
60
61
|
const transaction = db.transaction(FluidDriverObjectStoreName, "readwrite");
|
|
61
62
|
const index = transaction.store.index("fileId");
|
|
62
63
|
const keysToDelete = await index.getAllKeys(file.docId);
|
|
@@ -68,6 +69,9 @@ export class FluidCache {
|
|
|
68
69
|
eventName: "FluidCacheDeleteOldEntriesError" /* FluidCacheDeleteOldEntriesError */,
|
|
69
70
|
}, error);
|
|
70
71
|
}
|
|
72
|
+
finally {
|
|
73
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
74
|
+
}
|
|
71
75
|
}
|
|
72
76
|
async get(cacheEntry) {
|
|
73
77
|
const startTime = performance.now();
|
|
@@ -77,17 +81,23 @@ export class FluidCache {
|
|
|
77
81
|
cacheHit: cachedItem !== undefined,
|
|
78
82
|
type: cacheEntry.type,
|
|
79
83
|
duration: performance.now() - startTime,
|
|
84
|
+
dbOpenPerf: cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.dbOpenPerf,
|
|
85
|
+
dbClosePerf: cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.dbClosePerf,
|
|
80
86
|
});
|
|
81
87
|
// Value will contain metadata like the expiry time, we just want to return the object we were asked to cache
|
|
82
88
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
83
89
|
return cachedItem === null || cachedItem === void 0 ? void 0 : cachedItem.cachedObject;
|
|
84
90
|
}
|
|
85
91
|
async getItemFromCache(cacheEntry) {
|
|
92
|
+
let db;
|
|
86
93
|
try {
|
|
87
94
|
const key = getKeyForCacheEntry(cacheEntry);
|
|
88
|
-
const
|
|
95
|
+
const dbOpenStartTime = performance.now();
|
|
96
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
97
|
+
const dbOpenPerf = performance.now() - dbOpenStartTime;
|
|
89
98
|
const value = await db.get(FluidDriverObjectStoreName, key);
|
|
90
99
|
if (!value) {
|
|
100
|
+
db.close();
|
|
91
101
|
return undefined;
|
|
92
102
|
}
|
|
93
103
|
// If the data does not come from the same partition, don't return it
|
|
@@ -96,44 +106,32 @@ export class FluidCache {
|
|
|
96
106
|
eventName: "FluidCachePartitionKeyMismatch" /* FluidCachePartitionKeyMismatch */,
|
|
97
107
|
subCategory: "FluidCache" /* FluidCache */,
|
|
98
108
|
});
|
|
109
|
+
db.close();
|
|
99
110
|
return undefined;
|
|
100
111
|
}
|
|
101
112
|
const currentTime = new Date().getTime();
|
|
102
113
|
// If too much time has passed since this cache entry was used, we will also return undefined
|
|
103
114
|
if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {
|
|
115
|
+
db.close();
|
|
104
116
|
return undefined;
|
|
105
117
|
}
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
.get(key)
|
|
111
|
-
.then(async (valueToUpdate) => {
|
|
112
|
-
// This value in the database could have been updated concurrently by other tabs/iframes
|
|
113
|
-
// since we first read it. Only update the last accessed time if the current value in the
|
|
114
|
-
// DB was the same one we returned.
|
|
115
|
-
if (valueToUpdate !== undefined &&
|
|
116
|
-
valueToUpdate.createdTimeMs === value.createdTimeMs &&
|
|
117
|
-
(valueToUpdate.lastAccessTimeMs === undefined ||
|
|
118
|
-
valueToUpdate.lastAccessTimeMs < currentTime)) {
|
|
119
|
-
await transaction.store.put(Object.assign(Object.assign({}, valueToUpdate), { lastAccessTimeMs: currentTime }), key);
|
|
120
|
-
}
|
|
121
|
-
await transaction.done;
|
|
122
|
-
db.close();
|
|
123
|
-
})
|
|
124
|
-
.catch(() => { });
|
|
125
|
-
return value;
|
|
118
|
+
const dbCloseStartTime = performance.now();
|
|
119
|
+
db.close();
|
|
120
|
+
const dbClosePerf = performance.now() - dbCloseStartTime;
|
|
121
|
+
return Object.assign(Object.assign({}, value), { dbOpenPerf, dbClosePerf });
|
|
126
122
|
}
|
|
127
123
|
catch (error) {
|
|
128
124
|
// We can fail to open the db for a variety of reasons,
|
|
129
125
|
// such as the database version having upgraded underneath us. Return undefined in this case
|
|
130
126
|
this.logger.sendErrorEvent({ eventName: "FluidCacheGetError" /* FluidCacheGetError */ }, error);
|
|
127
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
131
128
|
return undefined;
|
|
132
129
|
}
|
|
133
130
|
}
|
|
134
131
|
async put(entry, value) {
|
|
132
|
+
let db;
|
|
135
133
|
try {
|
|
136
|
-
|
|
134
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
137
135
|
const currentTime = new Date().getTime();
|
|
138
136
|
await db.put(FluidDriverObjectStoreName, {
|
|
139
137
|
cachedObject: value,
|
|
@@ -151,6 +149,9 @@ export class FluidCache {
|
|
|
151
149
|
// such as the database version having upgraded underneath us
|
|
152
150
|
this.logger.sendErrorEvent({ eventName: "FluidCachePutError" /* FluidCachePutError */ }, error);
|
|
153
151
|
}
|
|
152
|
+
finally {
|
|
153
|
+
db === null || db === void 0 ? void 0 : db.close();
|
|
154
|
+
}
|
|
154
155
|
}
|
|
155
156
|
}
|
|
156
157
|
//# sourceMappingURL=FluidCache.js.map
|
package/lib/FluidCache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACN,8BAA8B,EAC9B,0BAA0B,EAC1B,mBAAmB,GACnB,MAAM,uBAAuB,CAAC;AAoC/B;;GAEG;AACH,MAAM,OAAO,UAAU;IAOtB,YAAY,MAAwB;QACnC,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAE9C,gBAAgB,CAAC,KAAK,IAAI,EAAE;;YAC3B,oEAAoE;YACpE,wGAAwG;YACxG,gGAAgG;YAChG,IAAI,MAAA,SAAS,CAAC,OAAO,0CAAE,QAAQ,EAAE;gBAChC,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;gBAEpD,gEAAgE;gBAChE,6DAA6D;gBAC7D,IAAI,aAAiC,CAAC;gBACtC,IAAI,cAAc,IAAI,QAAQ,EAAE;oBAC/B,aAAa,GAAK,QA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AAC,KAAK,EAAE,CAAC;YACZ,CAAC,CAAC;iBACD,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;YAClB,OAAO,KAAK,CAAC;SACb;QAAC,OAAO,KAAU,EAAE;YACpB,uDAAuD;YACvD,4FAA4F;YAC5F,IAAI,CAAC,MAAM,CAAC,cAAc,CACzB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACL,CAAC;YACF,OAAO,SAAS,CAAC;SACjB;IACF,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,KAAkB,EAAE,KAAU;QAC9C,IAAI;YACH,MAAM,EAAE,GAAG,MAAM,8BAA8B,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAE7D,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;YAEzC,MAAM,EAAE,CAAC,GAAG,CACX,0BAA0B,EAC1B;gBACC,YAAY,EAAE,KAAK;gBACnB,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK;gBACxB,IAAI,EAAE,KAAK,CAAC,IAAI;gBAChB,WAAW,EAAE,KAAK,CAAC,GAAG;gBACtB,YAAY,EAAE,IAAI,CAAC,YAAY;gBAC/B,aAAa,EAAE,WAAW;gBAC1B,gBAAgB,EAAE,WAAW;aAC7B,EACD,mBAAmB,CAAC,KAAK,CAAC,CAC1B,CAAC;YAEF,EAAE,CAAC,KAAK,EAAE,CAAC;SACX;QAAC,OAAO,KAAU,EAAE;YACpB,uDAAuD;YACvD,6DAA6D;YAC7D,IAAI,CAAC,MAAM,CAAC,cAAc,CACzB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACL,CAAC;SACF;IACF,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IPersistedCache, ICacheEntry, IFileEntry } from \"@fluidframework/odsp-driver-definitions\";\nimport { ITelemetryBaseLogger, ITelemetryLogger } from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n\tgetFluidCacheIndexedDbInstance,\n\tFluidDriverObjectStoreName,\n\tgetKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n\tFluidCacheErrorEvent,\n\tFluidCacheEventSubCategories,\n\tFluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n\tindexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n\t/**\n\t * A string to specify what partition of the cache you wish to use (e.g. a user id).\n\t * Null can be used to explicity indicate no partitioning, and has been chosen\n\t * vs undefined so that it is clear this is an intentional choice by the caller.\n\t * A null value should only be used when the host can ensure that the cache is not able\n\t * to be shared with multiple users.\n\t */\n\t// eslint-disable-next-line @rushstack/no-new-null\n\tpartitionKey: string | null;\n\n\t/**\n\t * A logger that can be used to get insight into cache performance and errors\n\t */\n\tlogger?: ITelemetryBaseLogger;\n\n\t/**\n\t * A value in milliseconds that determines the maximum age of a cache entry to return.\n\t * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n\t */\n\tmaxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n\tprivate readonly logger: ITelemetryLogger;\n\n\tprivate readonly partitionKey: string | null;\n\n\tprivate readonly maxCacheItemAge: number;\n\n\tconstructor(config: FluidCacheConfig) {\n\t\tthis.logger = ChildLogger.create(config.logger);\n\t\tthis.partitionKey = config.partitionKey;\n\t\tthis.maxCacheItemAge = config.maxCacheItemAge;\n\n\t\tscheduleIdleTask(async () => {\n\t\t\t// Log how much storage space is currently being used by indexed db.\n\t\t\t// NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n\t\t\t// Exception added when eslint rule was added, this should be revisited when modifying this code\n\t\t\tif (navigator.storage?.estimate) {\n\t\t\t\tconst estimate = await navigator.storage.estimate();\n\n\t\t\t\t// Some browsers have a usageDetails property that will tell you\n\t\t\t\t// more detailed information on how the storage is being used\n\t\t\t\tlet indexedDBSize: number | undefined;\n\t\t\t\tif (\"usageDetails\" in estimate) {\n\t\t\t\t\tindexedDBSize = ((estimate as any).usageDetails as StorageQuotaUsageDetails)\n\t\t\t\t\t\t.indexedDB;\n\t\t\t\t}\n\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t\tquota: estimate.quota,\n\t\t\t\t\tusage: estimate.usage,\n\t\t\t\t\tindexedDBSize,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\tscheduleIdleTask(async () => {\n\t\t\t// Delete entries that have not been accessed recently to clean up space\n\t\t\ttry {\n\t\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\t\tconst index = transaction.store.index(\"lastAccessTimeMs\");\n\t\t\t\t// Get items that have not been accessed in 4 weeks\n\t\t\t\tconst keysToDelete = await index.getAllKeys(\n\t\t\t\t\tIDBKeyRange.upperBound(new Date().getTime() - 4 * 7 * 24 * 60 * 60 * 1000),\n\t\t\t\t);\n\n\t\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\t\tawait transaction.done;\n\t\t\t} catch (error: any) {\n\t\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t\t{\n\t\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t\t},\n\t\t\t\t\terror,\n\t\t\t\t);\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic async removeEntries(file: IFileEntry): Promise<void> {\n\t\ttry {\n\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\tconst index = transaction.store.index(\"fileId\");\n\n\t\t\tconst keysToDelete = await index.getAllKeys(file.docId);\n\n\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\tawait transaction.done;\n\t\t} catch (error: any) {\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{\n\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t},\n\t\t\t\terror,\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic async get(cacheEntry: ICacheEntry): Promise<any> {\n\t\tconst startTime = performance.now();\n\n\t\tconst cachedItem = await this.getItemFromCache(cacheEntry);\n\n\t\tthis.logger.sendPerformanceEvent({\n\t\t\teventName: \"FluidCacheAccess\",\n\t\t\tcacheHit: cachedItem !== undefined,\n\t\t\ttype: cacheEntry.type,\n\t\t\tduration: performance.now() - startTime,\n\t\t});\n\n\t\t// Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\treturn cachedItem?.cachedObject;\n\t}\n\n\tprivate async getItemFromCache(cacheEntry: ICacheEntry) {\n\t\ttry {\n\t\t\tconst key = getKeyForCacheEntry(cacheEntry);\n\n\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst value = await db.get(FluidDriverObjectStoreName, key);\n\n\t\t\tif (!value) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\t// If the data does not come from the same partition, don't return it\n\t\t\tif (value.partitionKey !== this.partitionKey) {\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t});\n\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\t// If too much time has passed since this cache entry was used, we will also return undefined\n\t\t\tif (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\t// We don't want to block the get return of this function on updating the last accessed time\n\t\t\t// We catch this promise because there is no user bad if this is rejected.\n\t\t\ttransaction.store\n\t\t\t\t.get(key)\n\t\t\t\t.then(async (valueToUpdate) => {\n\t\t\t\t\t// This value in the database could have been updated concurrently by other tabs/iframes\n\t\t\t\t\t// since we first read it. Only update the last accessed time if the current value in the\n\t\t\t\t\t// DB was the same one we returned.\n\t\t\t\t\tif (\n\t\t\t\t\t\tvalueToUpdate !== undefined &&\n\t\t\t\t\t\tvalueToUpdate.createdTimeMs === value.createdTimeMs &&\n\t\t\t\t\t\t(valueToUpdate.lastAccessTimeMs === undefined ||\n\t\t\t\t\t\t\tvalueToUpdate.lastAccessTimeMs < currentTime)\n\t\t\t\t\t) {\n\t\t\t\t\t\tawait transaction.store.put(\n\t\t\t\t\t\t\t{ ...valueToUpdate, lastAccessTimeMs: currentTime },\n\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tawait transaction.done;\n\n\t\t\t\t\tdb.close();\n\t\t\t\t})\n\t\t\t\t.catch(() => {});\n\t\t\treturn value;\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us. Return undefined in this case\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCacheGetError },\n\t\t\t\terror,\n\t\t\t);\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tpublic async put(entry: ICacheEntry, value: any): Promise<void> {\n\t\ttry {\n\t\t\tconst db = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\tawait db.put(\n\t\t\t\tFluidDriverObjectStoreName,\n\t\t\t\t{\n\t\t\t\t\tcachedObject: value,\n\t\t\t\t\tfileId: entry.file.docId,\n\t\t\t\t\ttype: entry.type,\n\t\t\t\t\tcacheItemId: entry.key,\n\t\t\t\t\tpartitionKey: this.partitionKey,\n\t\t\t\t\tcreatedTimeMs: currentTime,\n\t\t\t\t\tlastAccessTimeMs: currentTime,\n\t\t\t\t},\n\t\t\t\tgetKeyForCacheEntry(entry),\n\t\t\t);\n\n\t\t\tdb.close();\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCachePutError },\n\t\t\t\terror,\n\t\t\t);\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"FluidCache.js","sourceRoot":"","sources":["../src/FluidCache.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACN,8BAA8B,EAE9B,0BAA0B,EAC1B,mBAAmB,GACnB,MAAM,uBAAuB,CAAC;AAoC/B;;GAEG;AACH,MAAM,OAAO,UAAU;IAOtB,YAAY,MAAwB;QACnC,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAE9C,gBAAgB,CAAC,KAAK,IAAI,EAAE;;YAC3B,oEAAoE;YACpE,wGAAwG;YACxG,gGAAgG;YAChG,IAAI,MAAA,SAAS,CAAC,OAAO,0CAAE,QAAQ,EAAE;gBAChC,MAAM,QAAQ,GAAG,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CACzB,EAAE,SAAS,+CAAyC,EAAE,EACtD,KAAK,CACL,CAAC;SACF;gBAAS;YACT,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,KAAK,EAAE,CAAC;SACZ;IACF,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { IDBPDatabase } from \"idb\";\nimport { IPersistedCache, ICacheEntry, IFileEntry } from \"@fluidframework/odsp-driver-definitions\";\nimport { ITelemetryBaseLogger, ITelemetryLogger } from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { scheduleIdleTask } from \"./scheduleIdleTask\";\nimport {\n\tgetFluidCacheIndexedDbInstance,\n\tFluidCacheDBSchema,\n\tFluidDriverObjectStoreName,\n\tgetKeyForCacheEntry,\n} from \"./FluidCacheIndexedDb\";\nimport {\n\tFluidCacheErrorEvent,\n\tFluidCacheEventSubCategories,\n\tFluidCacheGenericEvent,\n} from \"./fluidCacheTelemetry\";\n\n// Some browsers have a usageDetails property that will tell you more detailed information\n// on how the storage is being used\ninterface StorageQuotaUsageDetails {\n\tindexedDB: number | undefined;\n}\n\nexport interface FluidCacheConfig {\n\t/**\n\t * A string to specify what partition of the cache you wish to use (e.g. a user id).\n\t * Null can be used to explicity indicate no partitioning, and has been chosen\n\t * vs undefined so that it is clear this is an intentional choice by the caller.\n\t * A null value should only be used when the host can ensure that the cache is not able\n\t * to be shared with multiple users.\n\t */\n\t// eslint-disable-next-line @rushstack/no-new-null\n\tpartitionKey: string | null;\n\n\t/**\n\t * A logger that can be used to get insight into cache performance and errors\n\t */\n\tlogger?: ITelemetryBaseLogger;\n\n\t/**\n\t * A value in milliseconds that determines the maximum age of a cache entry to return.\n\t * If an entry exists in the cache, but is older than this value, the cached value will not be returned.\n\t */\n\tmaxCacheItemAge: number;\n}\n\n/**\n * A cache that can be used by the Fluid ODSP driver to cache data for faster performance\n */\nexport class FluidCache implements IPersistedCache {\n\tprivate readonly logger: ITelemetryLogger;\n\n\tprivate readonly partitionKey: string | null;\n\n\tprivate readonly maxCacheItemAge: number;\n\n\tconstructor(config: FluidCacheConfig) {\n\t\tthis.logger = ChildLogger.create(config.logger);\n\t\tthis.partitionKey = config.partitionKey;\n\t\tthis.maxCacheItemAge = config.maxCacheItemAge;\n\n\t\tscheduleIdleTask(async () => {\n\t\t\t// Log how much storage space is currently being used by indexed db.\n\t\t\t// NOTE: This API is not supported in all browsers and it doesn't let you see the size of a specific DB.\n\t\t\t// Exception added when eslint rule was added, this should be revisited when modifying this code\n\t\t\tif (navigator.storage?.estimate) {\n\t\t\t\tconst estimate = await navigator.storage.estimate();\n\n\t\t\t\t// Some browsers have a usageDetails property that will tell you\n\t\t\t\t// more detailed information on how the storage is being used\n\t\t\t\tlet indexedDBSize: number | undefined;\n\t\t\t\tif (\"usageDetails\" in estimate) {\n\t\t\t\t\tindexedDBSize = ((estimate as any).usageDetails as StorageQuotaUsageDetails)\n\t\t\t\t\t\t.indexedDB;\n\t\t\t\t}\n\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCacheStorageInfo,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t\tquota: estimate.quota,\n\t\t\t\t\tusage: estimate.usage,\n\t\t\t\t\tindexedDBSize,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\tscheduleIdleTask(async () => {\n\t\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\n\t\t\t// Delete entries that have not been accessed recently to clean up space\n\t\t\ttry {\n\t\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\t\tconst index = transaction.store.index(\"createdTimeMs\");\n\t\t\t\t// Get items which were cached before the maxCacheItemAge.\n\t\t\t\tconst keysToDelete = await index.getAllKeys(\n\t\t\t\t\tIDBKeyRange.upperBound(new Date().getTime() - this.maxCacheItemAge),\n\t\t\t\t);\n\n\t\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\t\tawait transaction.done;\n\t\t\t} catch (error: any) {\n\t\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t\t{\n\t\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t\t},\n\t\t\t\t\terror,\n\t\t\t\t);\n\t\t\t} finally {\n\t\t\t\tdb?.close();\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic async removeEntries(file: IFileEntry): Promise<void> {\n\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\t\ttry {\n\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst transaction = db.transaction(FluidDriverObjectStoreName, \"readwrite\");\n\t\t\tconst index = transaction.store.index(\"fileId\");\n\n\t\t\tconst keysToDelete = await index.getAllKeys(file.docId);\n\n\t\t\tawait Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));\n\t\t\tawait transaction.done;\n\t\t} catch (error: any) {\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{\n\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldEntriesError,\n\t\t\t\t},\n\t\t\t\terror,\n\t\t\t);\n\t\t} finally {\n\t\t\tdb?.close();\n\t\t}\n\t}\n\n\tpublic async get(cacheEntry: ICacheEntry): Promise<any> {\n\t\tconst startTime = performance.now();\n\n\t\tconst cachedItem = await this.getItemFromCache(cacheEntry);\n\n\t\tthis.logger.sendPerformanceEvent({\n\t\t\teventName: \"FluidCacheAccess\",\n\t\t\tcacheHit: cachedItem !== undefined,\n\t\t\ttype: cacheEntry.type,\n\t\t\tduration: performance.now() - startTime,\n\t\t\tdbOpenPerf: cachedItem?.dbOpenPerf,\n\t\t\tdbClosePerf: cachedItem?.dbClosePerf,\n\t\t});\n\n\t\t// Value will contain metadata like the expiry time, we just want to return the object we were asked to cache\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\treturn cachedItem?.cachedObject;\n\t}\n\n\tprivate async getItemFromCache(cacheEntry: ICacheEntry) {\n\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\t\ttry {\n\t\t\tconst key = getKeyForCacheEntry(cacheEntry);\n\n\t\t\tconst dbOpenStartTime = performance.now();\n\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\t\t\tconst dbOpenPerf = performance.now() - dbOpenStartTime;\n\t\t\tconst value = await db.get(FluidDriverObjectStoreName, key);\n\n\t\t\tif (!value) {\n\t\t\t\tdb.close();\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\t// If the data does not come from the same partition, don't return it\n\t\t\tif (value.partitionKey !== this.partitionKey) {\n\t\t\t\tthis.logger.sendTelemetryEvent({\n\t\t\t\t\teventName: FluidCacheGenericEvent.FluidCachePartitionKeyMismatch,\n\t\t\t\t\tsubCategory: FluidCacheEventSubCategories.FluidCache,\n\t\t\t\t});\n\n\t\t\t\tdb.close();\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\t// If too much time has passed since this cache entry was used, we will also return undefined\n\t\t\tif (currentTime - value.createdTimeMs > this.maxCacheItemAge) {\n\t\t\t\tdb.close();\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst dbCloseStartTime = performance.now();\n\t\t\tdb.close();\n\t\t\tconst dbClosePerf = performance.now() - dbCloseStartTime;\n\t\t\treturn { ...value, dbOpenPerf, dbClosePerf };\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us. Return undefined in this case\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCacheGetError },\n\t\t\t\terror,\n\t\t\t);\n\t\t\tdb?.close();\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tpublic async put(entry: ICacheEntry, value: any): Promise<void> {\n\t\tlet db: IDBPDatabase<FluidCacheDBSchema> | undefined;\n\t\ttry {\n\t\t\tdb = await getFluidCacheIndexedDbInstance(this.logger);\n\n\t\t\tconst currentTime = new Date().getTime();\n\n\t\t\tawait db.put(\n\t\t\t\tFluidDriverObjectStoreName,\n\t\t\t\t{\n\t\t\t\t\tcachedObject: value,\n\t\t\t\t\tfileId: entry.file.docId,\n\t\t\t\t\ttype: entry.type,\n\t\t\t\t\tcacheItemId: entry.key,\n\t\t\t\t\tpartitionKey: this.partitionKey,\n\t\t\t\t\tcreatedTimeMs: currentTime,\n\t\t\t\t\tlastAccessTimeMs: currentTime,\n\t\t\t\t},\n\t\t\t\tgetKeyForCacheEntry(entry),\n\t\t\t);\n\n\t\t\tdb.close();\n\t\t} catch (error: any) {\n\t\t\t// We can fail to open the db for a variety of reasons,\n\t\t\t// such as the database version having upgraded underneath us\n\t\t\tthis.logger.sendErrorEvent(\n\t\t\t\t{ eventName: FluidCacheErrorEvent.FluidCachePutError },\n\t\t\t\terror,\n\t\t\t);\n\t\t} finally {\n\t\t\tdb?.close();\n\t\t}\n\t}\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
import { DBSchema, IDBPDatabase } from "idb";
|
|
5
|
+
import { DBSchema, DeleteDBCallbacks, IDBPDatabase } from "idb";
|
|
6
6
|
import { ICacheEntry } from "@fluidframework/odsp-driver-definitions";
|
|
7
7
|
import { ITelemetryBaseLogger } from "@fluidframework/common-definitions";
|
|
8
8
|
export declare const FluidDriverCacheDBName = "fluidDriverCache";
|
|
@@ -13,7 +13,7 @@ export declare const oldVersionNameMapping: Partial<{
|
|
|
13
13
|
}>;
|
|
14
14
|
export declare function getKeyForCacheEntry(entry: ICacheEntry): string;
|
|
15
15
|
export declare function getFluidCacheIndexedDbInstance(logger?: ITelemetryBaseLogger): Promise<IDBPDatabase<FluidCacheDBSchema>>;
|
|
16
|
-
export declare function deleteFluidCacheIndexDbInstance(): Promise<void>;
|
|
16
|
+
export declare function deleteFluidCacheIndexDbInstance(deleteDBCallbacks?: DeleteDBCallbacks): Promise<void>;
|
|
17
17
|
/**
|
|
18
18
|
* Schema for the object store used to cache driver information
|
|
19
19
|
*/
|
|
@@ -51,6 +51,8 @@ export interface FluidCacheDBSchema extends DBSchema {
|
|
|
51
51
|
/**
|
|
52
52
|
* The last time the cache entry was used.
|
|
53
53
|
* This is initially set to the time the cache entry was created Measured as ms since unix epoch.
|
|
54
|
+
* With the recent change, this won't be updated on read as it will not be used anywhere. Only keeping
|
|
55
|
+
* so as to not upgrade the schema version.
|
|
54
56
|
*/
|
|
55
57
|
lastAccessTimeMs: number;
|
|
56
58
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCacheIndexedDb.d.ts","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAU,QAAQ,EAAE,YAAY,EAAY,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"FluidCacheIndexedDb.d.ts","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAU,QAAQ,EAAE,iBAAiB,EAAE,YAAY,EAAY,MAAM,KAAK,CAAC;AAClF,OAAO,EAAE,WAAW,EAAE,MAAM,yCAAyC,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAK1E,eAAO,MAAM,sBAAsB,qBAAqB,CAAC;AAGzD,eAAO,MAAM,0BAA0B,qBAAqB,CAAC;AAE7D,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAGrC,eAAO,MAAM,qBAAqB,EAAE,OAAO,CAAC;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAGpE,CAAC;AAEF,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,WAAW,UAErD;AAED,wBAAgB,8BAA8B,CAC7C,MAAM,CAAC,EAAE,oBAAoB,GAC3B,OAAO,CAAC,YAAY,CAAC,kBAAkB,CAAC,CAAC,CAwC3C;AAID,wBAAgB,+BAA+B,CAC9C,iBAAiB,CAAC,EAAE,iBAAiB,GACnC,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;GAEG;AACH,MAAM,WAAW,kBAAmB,SAAQ,QAAQ;IACnD,CAAC,0BAA0B,CAAC,EAAE;QAC7B;;WAEG;QACH,GAAG,EAAE,MAAM,CAAC;QAEZ,KAAK,EAAE;YACN;;eAEG;YACH,MAAM,EAAE,MAAM,CAAC;YAEf;;eAEG;YACH,IAAI,EAAE,MAAM,CAAC;YAEb;;;;eAIG;YACH,WAAW,EAAE,MAAM,CAAC;YAKpB,YAAY,EAAE,GAAG,CAAC;YAElB;;;eAGG;YAEH,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;YAE5B;;eAEG;YACH,aAAa,EAAE,MAAM,CAAC;YAEtB;;;;;eAKG;YACH,gBAAgB,EAAE,MAAM,CAAC;SACzB,CAAC;QAEF,OAAO,EAAE;YACR,aAAa,EAAE,MAAM,CAAC;YACtB,YAAY,EAAE,MAAM,CAAC;YACrB,gBAAgB,EAAE,MAAM,CAAC;YACzB,MAAM,EAAE,MAAM,CAAC;SACf,CAAC;KACF,CAAC;CACF"}
|
|
@@ -53,7 +53,7 @@ export function getFluidCacheIndexedDbInstance(logger) {
|
|
|
53
53
|
}
|
|
54
54
|
// Deletes the indexed DB instance.
|
|
55
55
|
// Warning this can throw an error in Firefox incognito, where accessing storage is prohibited.
|
|
56
|
-
export function deleteFluidCacheIndexDbInstance() {
|
|
57
|
-
return deleteDB(FluidDriverCacheDBName);
|
|
56
|
+
export function deleteFluidCacheIndexDbInstance(deleteDBCallbacks) {
|
|
57
|
+
return deleteDB(FluidDriverCacheDBName, deleteDBCallbacks);
|
|
58
58
|
}
|
|
59
59
|
//# sourceMappingURL=FluidCacheIndexedDb.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FluidCacheIndexedDb.js","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"FluidCacheIndexedDb.js","sourceRoot":"","sources":["../src/FluidCacheIndexedDb.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAA6C,QAAQ,EAAE,MAAM,KAAK,CAAC;AAGlF,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAG9D,+DAA+D;AAC/D,MAAM,CAAC,MAAM,sBAAsB,GAAG,kBAAkB,CAAC;AAEzD,+GAA+G;AAC/G,MAAM,CAAC,MAAM,0BAA0B,GAAG,kBAAkB,CAAC;AAE7D,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAErC,kGAAkG;AAClG,MAAM,CAAC,MAAM,qBAAqB,GAAuC;IACxE,CAAC,EAAE,cAAc;IACjB,CAAC,EAAE,iBAAiB;CACpB,CAAC;AAEF,MAAM,UAAU,mBAAmB,CAAC,KAAkB;IACrD,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,GAAG,EAAE,CAAC;AACzD,CAAC;AAED,MAAM,UAAU,8BAA8B,CAC7C,MAA6B;IAE7B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,MAAM,CAAqB,sBAAsB,EAAE,mBAAmB,EAAE;YACvE,OAAO,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE;gBAC3B,IAAI;oBACH,wDAAwD;oBACxD,kDAAkD;oBAClD,MAAM,aAAa,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAC;oBACxD,IAAI,aAAa,EAAE;wBAClB,sEAAsE;wBACtE,sDAAsD;wBACtD,EAAE,CAAC,iBAAiB,CAAC,aAAoB,CAAC,CAAC;qBAC3C;iBACD;gBAAC,OAAO,KAAU,EAAE;oBACpB,oEAAoE;oBACpE,8CAA8C;oBAC9C,wEAAwE;oBACxE,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CACxC;wBACC,SAAS,+DAAiD;qBAC1D,EACD,KAAK,CACL,CAAC;iBACF;gBAED,MAAM,gBAAgB,GAAG,EAAE,CAAC,iBAAiB,CAAC,0BAA0B,CAAC,CAAC;gBAC1E,gBAAgB,CAAC,WAAW,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC;gBAC/D,gBAAgB,CAAC,WAAW,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC;gBACrE,gBAAgB,CAAC,WAAW,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;gBAC7D,gBAAgB,CAAC,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;YAClD,CAAC;YACD,OAAO,EAAE,GAAG,EAAE;gBACb,MAAM,CACL,IAAI,KAAK,CACR,+EAA+E,CAC/E,CACD,CAAC;YACH,CAAC;SACD,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,mCAAmC;AACnC,+FAA+F;AAC/F,MAAM,UAAU,+BAA+B,CAC9C,iBAAqC;IAErC,OAAO,QAAQ,CAAC,sBAAsB,EAAE,iBAAiB,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { openDB, DBSchema, DeleteDBCallbacks, IDBPDatabase, deleteDB } from \"idb\";\nimport { ICacheEntry } from \"@fluidframework/odsp-driver-definitions\";\nimport { ITelemetryBaseLogger } from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { FluidCacheErrorEvent } from \"./fluidCacheTelemetry\";\n\n// The name of the database that we use for caching Fluid info.\nexport const FluidDriverCacheDBName = \"fluidDriverCache\";\n\n// The name of the object store within the indexed db instance that the driver will use to cache Fluid content.\nexport const FluidDriverObjectStoreName = \"driverStorage.V3\";\n\nexport const CurrentCacheVersion = 3;\n\n// Note that V1 and V2 were misspelled as \"diver\", and we need to keep using the misspelling here.\nexport const oldVersionNameMapping: Partial<{ [key: number]: string }> = {\n\t1: \"diverStorage\",\n\t2: \"diverStorage.V2\",\n};\n\nexport function getKeyForCacheEntry(entry: ICacheEntry) {\n\treturn `${entry.file.docId}_${entry.type}_${entry.key}`;\n}\n\nexport function getFluidCacheIndexedDbInstance(\n\tlogger?: ITelemetryBaseLogger,\n): Promise<IDBPDatabase<FluidCacheDBSchema>> {\n\treturn new Promise((resolve, reject) => {\n\t\topenDB<FluidCacheDBSchema>(FluidDriverCacheDBName, CurrentCacheVersion, {\n\t\t\tupgrade: (db, oldVersion) => {\n\t\t\t\ttry {\n\t\t\t\t\t// We changed the format of the object store, so we must\n\t\t\t\t\t// delete the old stores to create a new one in V3\n\t\t\t\t\tconst cacheToDelete = oldVersionNameMapping[oldVersion];\n\t\t\t\t\tif (cacheToDelete) {\n\t\t\t\t\t\t// We don't include the old object stores in the schema, so we need to\n\t\t\t\t\t\t// use a typecast here to prevent IDB from complaining\n\t\t\t\t\t\tdb.deleteObjectStore(cacheToDelete as any);\n\t\t\t\t\t}\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// Catch any error done when attempting to delete the older version.\n\t\t\t\t\t// If the object does not exist db will throw.\n\t\t\t\t\t// We can now assume that the old version is no longer there regardless.\n\t\t\t\t\tChildLogger.create(logger).sendErrorEvent(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\teventName: FluidCacheErrorEvent.FluidCacheDeleteOldDbError,\n\t\t\t\t\t\t},\n\t\t\t\t\t\terror,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tconst cacheObjectStore = db.createObjectStore(FluidDriverObjectStoreName);\n\t\t\t\tcacheObjectStore.createIndex(\"createdTimeMs\", \"createdTimeMs\");\n\t\t\t\tcacheObjectStore.createIndex(\"lastAccessTimeMs\", \"lastAccessTimeMs\");\n\t\t\t\tcacheObjectStore.createIndex(\"partitionKey\", \"partitionKey\");\n\t\t\t\tcacheObjectStore.createIndex(\"fileId\", \"fileId\");\n\t\t\t},\n\t\t\tblocked: () => {\n\t\t\t\treject(\n\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\"Could not open DB since it is blocked by an older client that has the DB open\",\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t},\n\t\t}).then(resolve, reject);\n\t});\n}\n\n// Deletes the indexed DB instance.\n// Warning this can throw an error in Firefox incognito, where accessing storage is prohibited.\nexport function deleteFluidCacheIndexDbInstance(\n\tdeleteDBCallbacks?: DeleteDBCallbacks,\n): Promise<void> {\n\treturn deleteDB(FluidDriverCacheDBName, deleteDBCallbacks);\n}\n\n/**\n * Schema for the object store used to cache driver information\n */\nexport interface FluidCacheDBSchema extends DBSchema {\n\t[FluidDriverObjectStoreName]: {\n\t\t/**\n\t\t * A unique identifier for an item in the cache. It is a combination of file, type, and cacheItemId\n\t\t */\n\t\tkey: string;\n\n\t\tvalue: {\n\t\t\t/**\n\t\t\t * The identifier of the file associated with the cache entry\n\t\t\t */\n\t\t\tfileId: string;\n\n\t\t\t/**\n\t\t\t * Describes the type of content being cached, such as snapshot\n\t\t\t */\n\t\t\ttype: string;\n\n\t\t\t/**\n\t\t\t * Files may have multiple cached items associated with them,\n\t\t\t * this property uniquely identifies a specific cache entry for a file.\n\t\t\t * This is not globally unique, but rather a unique id for this file\n\t\t\t */\n\t\t\tcacheItemId: string;\n\n\t\t\t/*\n\t\t\t * Opaque object that the driver asks us to store in a cache for performance reasons\n\t\t\t */\n\t\t\tcachedObject: any;\n\n\t\t\t/**\n\t\t\t * A string to specify what partition of the cache you wish to use (e.g. a user id).\n\t\t\t * Null can be used to explicity indicate no partitioning.\n\t\t\t */\n\t\t\t// eslint-disable-next-line @rushstack/no-new-null\n\t\t\tpartitionKey: string | null;\n\n\t\t\t/**\n\t\t\t * The time when the cache entry was put into the cache\n\t\t\t */\n\t\t\tcreatedTimeMs: number;\n\n\t\t\t/**\n\t\t\t * The last time the cache entry was used.\n\t\t\t * This is initially set to the time the cache entry was created Measured as ms since unix epoch.\n\t\t\t * With the recent change, this won't be updated on read as it will not be used anywhere. Only keeping\n\t\t\t * so as to not upgrade the schema version.\n\t\t\t */\n\t\t\tlastAccessTimeMs: number;\n\t\t};\n\n\t\tindexes: {\n\t\t\tcreatedTimeMs: number;\n\t\t\tpartitionKey: string;\n\t\t\tlastAccessTimeMs: number;\n\t\t\tfileId: string;\n\t\t};\n\t};\n}\n"]}
|
package/lib/packageVersion.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export declare const pkgName = "@fluidframework/driver-web-cache";
|
|
8
|
-
export declare const pkgVersion = "2.0.0-dev.
|
|
8
|
+
export declare const pkgVersion = "2.0.0-dev.4.1.0.148229";
|
|
9
9
|
//# sourceMappingURL=packageVersion.d.ts.map
|
package/lib/packageVersion.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export const pkgName = "@fluidframework/driver-web-cache";
|
|
8
|
-
export const pkgVersion = "2.0.0-dev.
|
|
8
|
+
export const pkgVersion = "2.0.0-dev.4.1.0.148229";
|
|
9
9
|
//# sourceMappingURL=packageVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,kCAAkC,CAAC;AAC1D,MAAM,CAAC,MAAM,UAAU,GAAG,wBAAwB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"2.0.0-dev.
|
|
1
|
+
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,kCAAkC,CAAC;AAC1D,MAAM,CAAC,MAAM,UAAU,GAAG,wBAAwB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/driver-web-cache\";\nexport const pkgVersion = \"2.0.0-dev.4.1.0.148229\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/driver-web-cache",
|
|
3
|
-
"version": "2.0.0-dev.
|
|
3
|
+
"version": "2.0.0-dev.4.1.0.148229",
|
|
4
4
|
"description": "Implementation of the driver caching API for a web browser",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": {
|
|
@@ -14,6 +14,33 @@
|
|
|
14
14
|
"main": "dist/index.js",
|
|
15
15
|
"module": "lib/index.js",
|
|
16
16
|
"types": "dist/index.d.ts",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@fluidframework/common-definitions": "^0.20.1",
|
|
19
|
+
"@fluidframework/odsp-driver-definitions": "2.0.0-dev.4.1.0.148229",
|
|
20
|
+
"@fluidframework/telemetry-utils": "2.0.0-dev.4.1.0.148229",
|
|
21
|
+
"idb": "^6.1.2"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@fluid-tools/build-cli": "^0.13.1",
|
|
25
|
+
"@fluidframework/build-common": "^1.1.0",
|
|
26
|
+
"@fluidframework/build-tools": "^0.13.1",
|
|
27
|
+
"@fluidframework/driver-web-cache-previous": "npm:@fluidframework/driver-web-cache@2.0.0-internal.4.0.0",
|
|
28
|
+
"@fluidframework/eslint-config-fluid": "^2.0.0",
|
|
29
|
+
"@microsoft/api-extractor": "^7.34.4",
|
|
30
|
+
"@types/jest": "22.2.3",
|
|
31
|
+
"@types/node": "^14.18.38",
|
|
32
|
+
"concurrently": "^7.6.0",
|
|
33
|
+
"copyfiles": "^2.4.1",
|
|
34
|
+
"eslint": "~8.6.0",
|
|
35
|
+
"fake-indexeddb": "3.1.4",
|
|
36
|
+
"jest": "^26.6.3",
|
|
37
|
+
"prettier": "~2.6.2",
|
|
38
|
+
"rimraf": "^4.4.0",
|
|
39
|
+
"typescript": "~4.5.5"
|
|
40
|
+
},
|
|
41
|
+
"typeValidation": {
|
|
42
|
+
"broken": {}
|
|
43
|
+
},
|
|
17
44
|
"scripts": {
|
|
18
45
|
"build": "npm run build:genver && concurrently npm:build:compile npm:lint && npm run build:docs",
|
|
19
46
|
"build:commonjs": "npm run tsc && npm run typetests:gen && npm run build:test",
|
|
@@ -35,39 +62,7 @@
|
|
|
35
62
|
"prettier:fix": "prettier --write . --ignore-path ../../../.prettierignore",
|
|
36
63
|
"test": "jest",
|
|
37
64
|
"tsc": "tsc",
|
|
38
|
-
"typetests:gen": "
|
|
65
|
+
"typetests:gen": "fluid-type-test-generator",
|
|
39
66
|
"typetests:prepare": "flub generate typetests --prepare --dir . --pin"
|
|
40
|
-
},
|
|
41
|
-
"dependencies": {
|
|
42
|
-
"@fluidframework/common-definitions": "^0.20.1",
|
|
43
|
-
"@fluidframework/odsp-driver-definitions": ">=2.0.0-dev.3.1.0.125672 <2.0.0-dev.4.0.0",
|
|
44
|
-
"@fluidframework/telemetry-utils": ">=2.0.0-dev.3.1.0.125672 <2.0.0-dev.4.0.0",
|
|
45
|
-
"idb": "^6.1.2"
|
|
46
|
-
},
|
|
47
|
-
"devDependencies": {
|
|
48
|
-
"@fluid-tools/build-cli": "^0.8.0",
|
|
49
|
-
"@fluidframework/build-common": "^1.1.0",
|
|
50
|
-
"@fluidframework/build-tools": "^0.8.0",
|
|
51
|
-
"@fluidframework/driver-web-cache-previous": "npm:@fluidframework/driver-web-cache@2.0.0-internal.3.0.0",
|
|
52
|
-
"@fluidframework/eslint-config-fluid": "^2.0.0",
|
|
53
|
-
"@microsoft/api-extractor": "^7.22.2",
|
|
54
|
-
"@rushstack/eslint-config": "^2.5.1",
|
|
55
|
-
"@types/jest": "22.2.3",
|
|
56
|
-
"@types/node": "^14.18.36",
|
|
57
|
-
"concurrently": "^6.2.0",
|
|
58
|
-
"copyfiles": "^2.4.1",
|
|
59
|
-
"eslint": "~8.6.0",
|
|
60
|
-
"fake-indexeddb": "3.1.4",
|
|
61
|
-
"jest": "^26.6.3",
|
|
62
|
-
"prettier": "~2.6.2",
|
|
63
|
-
"rimraf": "^2.6.2",
|
|
64
|
-
"typescript": "~4.5.5"
|
|
65
|
-
},
|
|
66
|
-
"typeValidation": {
|
|
67
|
-
"version": "2.0.0-internal.3.1.0",
|
|
68
|
-
"previousVersionStyle": "~previousMinor",
|
|
69
|
-
"baselineRange": ">=2.0.0-internal.3.0.0 <2.0.0-internal.3.1.0",
|
|
70
|
-
"baselineVersion": "2.0.0-internal.3.0.0",
|
|
71
|
-
"broken": {}
|
|
72
67
|
}
|
|
73
|
-
}
|
|
68
|
+
}
|
package/src/FluidCache.ts
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
import { IDBPDatabase } from "idb";
|
|
6
6
|
import { IPersistedCache, ICacheEntry, IFileEntry } from "@fluidframework/odsp-driver-definitions";
|
|
7
7
|
import { ITelemetryBaseLogger, ITelemetryLogger } from "@fluidframework/common-definitions";
|
|
8
8
|
import { ChildLogger } from "@fluidframework/telemetry-utils";
|
|
9
9
|
import { scheduleIdleTask } from "./scheduleIdleTask";
|
|
10
10
|
import {
|
|
11
11
|
getFluidCacheIndexedDbInstance,
|
|
12
|
+
FluidCacheDBSchema,
|
|
12
13
|
FluidDriverObjectStoreName,
|
|
13
14
|
getKeyForCacheEntry,
|
|
14
15
|
} from "./FluidCacheIndexedDb";
|
|
@@ -88,15 +89,17 @@ export class FluidCache implements IPersistedCache {
|
|
|
88
89
|
});
|
|
89
90
|
|
|
90
91
|
scheduleIdleTask(async () => {
|
|
92
|
+
let db: IDBPDatabase<FluidCacheDBSchema> | undefined;
|
|
93
|
+
|
|
91
94
|
// Delete entries that have not been accessed recently to clean up space
|
|
92
95
|
try {
|
|
93
|
-
|
|
96
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
94
97
|
|
|
95
98
|
const transaction = db.transaction(FluidDriverObjectStoreName, "readwrite");
|
|
96
|
-
const index = transaction.store.index("
|
|
97
|
-
// Get items
|
|
99
|
+
const index = transaction.store.index("createdTimeMs");
|
|
100
|
+
// Get items which were cached before the maxCacheItemAge.
|
|
98
101
|
const keysToDelete = await index.getAllKeys(
|
|
99
|
-
IDBKeyRange.upperBound(new Date().getTime() -
|
|
102
|
+
IDBKeyRange.upperBound(new Date().getTime() - this.maxCacheItemAge),
|
|
100
103
|
);
|
|
101
104
|
|
|
102
105
|
await Promise.all(keysToDelete.map((key) => transaction.store.delete(key)));
|
|
@@ -108,13 +111,16 @@ export class FluidCache implements IPersistedCache {
|
|
|
108
111
|
},
|
|
109
112
|
error,
|
|
110
113
|
);
|
|
114
|
+
} finally {
|
|
115
|
+
db?.close();
|
|
111
116
|
}
|
|
112
117
|
});
|
|
113
118
|
}
|
|
114
119
|
|
|
115
120
|
public async removeEntries(file: IFileEntry): Promise<void> {
|
|
121
|
+
let db: IDBPDatabase<FluidCacheDBSchema> | undefined;
|
|
116
122
|
try {
|
|
117
|
-
|
|
123
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
118
124
|
|
|
119
125
|
const transaction = db.transaction(FluidDriverObjectStoreName, "readwrite");
|
|
120
126
|
const index = transaction.store.index("fileId");
|
|
@@ -130,6 +136,8 @@ export class FluidCache implements IPersistedCache {
|
|
|
130
136
|
},
|
|
131
137
|
error,
|
|
132
138
|
);
|
|
139
|
+
} finally {
|
|
140
|
+
db?.close();
|
|
133
141
|
}
|
|
134
142
|
}
|
|
135
143
|
|
|
@@ -143,6 +151,8 @@ export class FluidCache implements IPersistedCache {
|
|
|
143
151
|
cacheHit: cachedItem !== undefined,
|
|
144
152
|
type: cacheEntry.type,
|
|
145
153
|
duration: performance.now() - startTime,
|
|
154
|
+
dbOpenPerf: cachedItem?.dbOpenPerf,
|
|
155
|
+
dbClosePerf: cachedItem?.dbClosePerf,
|
|
146
156
|
});
|
|
147
157
|
|
|
148
158
|
// Value will contain metadata like the expiry time, we just want to return the object we were asked to cache
|
|
@@ -151,14 +161,17 @@ export class FluidCache implements IPersistedCache {
|
|
|
151
161
|
}
|
|
152
162
|
|
|
153
163
|
private async getItemFromCache(cacheEntry: ICacheEntry) {
|
|
164
|
+
let db: IDBPDatabase<FluidCacheDBSchema> | undefined;
|
|
154
165
|
try {
|
|
155
166
|
const key = getKeyForCacheEntry(cacheEntry);
|
|
156
167
|
|
|
157
|
-
const
|
|
158
|
-
|
|
168
|
+
const dbOpenStartTime = performance.now();
|
|
169
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
170
|
+
const dbOpenPerf = performance.now() - dbOpenStartTime;
|
|
159
171
|
const value = await db.get(FluidDriverObjectStoreName, key);
|
|
160
172
|
|
|
161
173
|
if (!value) {
|
|
174
|
+
db.close();
|
|
162
175
|
return undefined;
|
|
163
176
|
}
|
|
164
177
|
|
|
@@ -169,6 +182,7 @@ export class FluidCache implements IPersistedCache {
|
|
|
169
182
|
subCategory: FluidCacheEventSubCategories.FluidCache,
|
|
170
183
|
});
|
|
171
184
|
|
|
185
|
+
db.close();
|
|
172
186
|
return undefined;
|
|
173
187
|
}
|
|
174
188
|
|
|
@@ -176,35 +190,14 @@ export class FluidCache implements IPersistedCache {
|
|
|
176
190
|
|
|
177
191
|
// If too much time has passed since this cache entry was used, we will also return undefined
|
|
178
192
|
if (currentTime - value.createdTimeMs > this.maxCacheItemAge) {
|
|
193
|
+
db.close();
|
|
179
194
|
return undefined;
|
|
180
195
|
}
|
|
181
196
|
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
.get(key)
|
|
187
|
-
.then(async (valueToUpdate) => {
|
|
188
|
-
// This value in the database could have been updated concurrently by other tabs/iframes
|
|
189
|
-
// since we first read it. Only update the last accessed time if the current value in the
|
|
190
|
-
// DB was the same one we returned.
|
|
191
|
-
if (
|
|
192
|
-
valueToUpdate !== undefined &&
|
|
193
|
-
valueToUpdate.createdTimeMs === value.createdTimeMs &&
|
|
194
|
-
(valueToUpdate.lastAccessTimeMs === undefined ||
|
|
195
|
-
valueToUpdate.lastAccessTimeMs < currentTime)
|
|
196
|
-
) {
|
|
197
|
-
await transaction.store.put(
|
|
198
|
-
{ ...valueToUpdate, lastAccessTimeMs: currentTime },
|
|
199
|
-
key,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
await transaction.done;
|
|
203
|
-
|
|
204
|
-
db.close();
|
|
205
|
-
})
|
|
206
|
-
.catch(() => {});
|
|
207
|
-
return value;
|
|
197
|
+
const dbCloseStartTime = performance.now();
|
|
198
|
+
db.close();
|
|
199
|
+
const dbClosePerf = performance.now() - dbCloseStartTime;
|
|
200
|
+
return { ...value, dbOpenPerf, dbClosePerf };
|
|
208
201
|
} catch (error: any) {
|
|
209
202
|
// We can fail to open the db for a variety of reasons,
|
|
210
203
|
// such as the database version having upgraded underneath us. Return undefined in this case
|
|
@@ -212,13 +205,15 @@ export class FluidCache implements IPersistedCache {
|
|
|
212
205
|
{ eventName: FluidCacheErrorEvent.FluidCacheGetError },
|
|
213
206
|
error,
|
|
214
207
|
);
|
|
208
|
+
db?.close();
|
|
215
209
|
return undefined;
|
|
216
210
|
}
|
|
217
211
|
}
|
|
218
212
|
|
|
219
213
|
public async put(entry: ICacheEntry, value: any): Promise<void> {
|
|
214
|
+
let db: IDBPDatabase<FluidCacheDBSchema> | undefined;
|
|
220
215
|
try {
|
|
221
|
-
|
|
216
|
+
db = await getFluidCacheIndexedDbInstance(this.logger);
|
|
222
217
|
|
|
223
218
|
const currentTime = new Date().getTime();
|
|
224
219
|
|
|
@@ -244,6 +239,8 @@ export class FluidCache implements IPersistedCache {
|
|
|
244
239
|
{ eventName: FluidCacheErrorEvent.FluidCachePutError },
|
|
245
240
|
error,
|
|
246
241
|
);
|
|
242
|
+
} finally {
|
|
243
|
+
db?.close();
|
|
247
244
|
}
|
|
248
245
|
}
|
|
249
246
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { openDB, DBSchema, IDBPDatabase, deleteDB } from "idb";
|
|
6
|
+
import { openDB, DBSchema, DeleteDBCallbacks, IDBPDatabase, deleteDB } from "idb";
|
|
7
7
|
import { ICacheEntry } from "@fluidframework/odsp-driver-definitions";
|
|
8
8
|
import { ITelemetryBaseLogger } from "@fluidframework/common-definitions";
|
|
9
9
|
import { ChildLogger } from "@fluidframework/telemetry-utils";
|
|
@@ -73,8 +73,10 @@ export function getFluidCacheIndexedDbInstance(
|
|
|
73
73
|
|
|
74
74
|
// Deletes the indexed DB instance.
|
|
75
75
|
// Warning this can throw an error in Firefox incognito, where accessing storage is prohibited.
|
|
76
|
-
export function deleteFluidCacheIndexDbInstance(
|
|
77
|
-
|
|
76
|
+
export function deleteFluidCacheIndexDbInstance(
|
|
77
|
+
deleteDBCallbacks?: DeleteDBCallbacks,
|
|
78
|
+
): Promise<void> {
|
|
79
|
+
return deleteDB(FluidDriverCacheDBName, deleteDBCallbacks);
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
/**
|
|
@@ -125,6 +127,8 @@ export interface FluidCacheDBSchema extends DBSchema {
|
|
|
125
127
|
/**
|
|
126
128
|
* The last time the cache entry was used.
|
|
127
129
|
* This is initially set to the time the cache entry was created Measured as ms since unix epoch.
|
|
130
|
+
* With the recent change, this won't be updated on read as it will not be used anywhere. Only keeping
|
|
131
|
+
* so as to not upgrade the schema version.
|
|
128
132
|
*/
|
|
129
133
|
lastAccessTimeMs: number;
|
|
130
134
|
};
|
package/src/packageVersion.ts
CHANGED