@devvit/cache 0.12.1-next-2025-10-02-22-58-58-07942952e.0
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/LICENSE +26 -0
- package/PromiseCache.d.ts +87 -0
- package/PromiseCache.d.ts.map +1 -0
- package/PromiseCache.js +263 -0
- package/README.md +5 -0
- package/cache.d.ts +10 -0
- package/cache.d.ts.map +1 -0
- package/cache.js +10 -0
- package/cache.test.d.ts.map +1 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -0
- package/index.js +1 -0
- package/package.json +45 -0
- package/serverImportInClientCodePanic.d.ts +17 -0
- package/serverImportInClientCodePanic.d.ts.map +1 -0
- package/serverImportInClientCodePanic.js +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Copyright (c) 2023 Reddit Inc.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions
|
|
5
|
+
are met:
|
|
6
|
+
|
|
7
|
+
1. Redistributions of source code must retain the above copyright
|
|
8
|
+
notice, this list of conditions and the following disclaimer.
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
|
11
|
+
documentation and/or other materials provided with the distribution.
|
|
12
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
13
|
+
contributors may be used to endorse or promote products derived from
|
|
14
|
+
this software without specific prior written permission.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
17
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
19
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
22
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
23
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
24
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
25
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
26
|
+
SUCH DAMAGE.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { RedisClient } from '@devvit/redis';
|
|
2
|
+
import type { JsonValue } from '@devvit/shared-types/json.js';
|
|
3
|
+
export type CacheEntry = {
|
|
4
|
+
value: JsonValue | null;
|
|
5
|
+
expires: number;
|
|
6
|
+
error: string | null;
|
|
7
|
+
errorTime: number | null;
|
|
8
|
+
checkedAt: number;
|
|
9
|
+
errorCount: number;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A class that provides a random number between 0 and 1. It is helpful to extract this for testing purposes.
|
|
13
|
+
*/
|
|
14
|
+
export type Randomizer = {
|
|
15
|
+
random(): number;
|
|
16
|
+
};
|
|
17
|
+
export declare const MathRandomizer: Randomizer;
|
|
18
|
+
/**
|
|
19
|
+
* A class that provides the current time. It is helpful to extract this for testing purposes.
|
|
20
|
+
*/
|
|
21
|
+
export type Clock = {
|
|
22
|
+
now(): Date;
|
|
23
|
+
};
|
|
24
|
+
export declare const SystemClock: Clock;
|
|
25
|
+
export type CacheOptions = {
|
|
26
|
+
/**
|
|
27
|
+
* Time to live in milliseconds.
|
|
28
|
+
*/
|
|
29
|
+
ttl: number;
|
|
30
|
+
/**
|
|
31
|
+
* Key to use for caching.
|
|
32
|
+
*/
|
|
33
|
+
key: string;
|
|
34
|
+
};
|
|
35
|
+
export type LocalCache = {
|
|
36
|
+
[key: string]: CacheEntry;
|
|
37
|
+
};
|
|
38
|
+
export declare function _namespaced(key: string): string;
|
|
39
|
+
export declare function _lock(key: string): string;
|
|
40
|
+
export declare const retryLimit: number;
|
|
41
|
+
export declare const clientRetryDelay: number;
|
|
42
|
+
export declare const allowStaleFor: number;
|
|
43
|
+
/**
|
|
44
|
+
* This class is responsible for managing the caching of promises. It is a layered cache, meaning it will first check
|
|
45
|
+
* the local in-memory cache, then the redis cache, and finally the source of truth. It will also handle refreshing the cache according
|
|
46
|
+
* to the TTL and error handling.
|
|
47
|
+
*
|
|
48
|
+
* The local cache is shared across all requests to a given pod, while the redis cache is shared across all requests to all pods.
|
|
49
|
+
*
|
|
50
|
+
* Please note that in order to prevent a stampede of requests to the source of truth, we use a lock in redis to ensure only one
|
|
51
|
+
* request is made to the source of truth at a time. If the lock is obtained, the cache will be updated and the lock will be released.
|
|
52
|
+
*
|
|
53
|
+
* Additionally, we use a polling mechanism to fetch the cache if the lock is not obtained. This is to prevent unnecessary errors.
|
|
54
|
+
*
|
|
55
|
+
* Finally, we also want to prevent stampedes against redis for the lock election and the retries. We use a ramping probability to ease in the
|
|
56
|
+
* attempts to get the lock, and not every error will trigger a retry.
|
|
57
|
+
*
|
|
58
|
+
* This means that the cache will be eventually consistent, but will not be immediately consistent. This is a tradeoff we are willing to make.
|
|
59
|
+
* Additionally, this means that the TTL is not precise. The cache may be updated a bit more often than the TTL, but it will not be updated less often.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* import { cache, reddit } from "@devvit/web/server";
|
|
64
|
+
* export function fetchSnoovatarUrl(username: string): Promise<string> {
|
|
65
|
+
* return await cache(
|
|
66
|
+
* async () => {
|
|
67
|
+
* const url = await reddit.getSnoovatarUrl(username)
|
|
68
|
+
* console.log(`snoovatar URL cache busted for ${username}`)
|
|
69
|
+
* if (!url) {
|
|
70
|
+
* throw new Error(`Failed to fetch snoovatar URL for user ${username}`);
|
|
71
|
+
* }
|
|
72
|
+
* return url;
|
|
73
|
+
* },
|
|
74
|
+
* {
|
|
75
|
+
* key: `snoovatar_by_${username}`,
|
|
76
|
+
* ttl: 24 * 60 * 60 * 1000 // expire after one day.
|
|
77
|
+
* }
|
|
78
|
+
* );
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare class PromiseCache {
|
|
83
|
+
#private;
|
|
84
|
+
constructor(redis: RedisClient, localCache: LocalCache, clock: Clock, randomizer: Randomizer);
|
|
85
|
+
cache<T extends JsonValue>(closure: () => Promise<T>, options: CacheOptions): Promise<T>;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=PromiseCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PromiseCache.d.ts","sourceRoot":"","sources":["../src/PromiseCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAE9D,MAAM,MAAM,UAAU,GAAG;IAEvB,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IAExB,OAAO,EAAE,MAAM,CAAC;IAEhB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAErB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAEzB,SAAS,EAAE,MAAM,CAAC;IAElB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,IAAI,MAAM,CAAC;CAClB,CAAC;AAGF,eAAO,MAAM,cAAc,EAAE,UAI5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,KAAK,GAAG;IAClB,GAAG,IAAI,IAAI,CAAC;CACb,CAAC;AAEF,eAAO,MAAM,WAAW,EAAE,KAIzB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAA;CAAE,CAAC;AAEvD,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AACD,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAKD,eAAO,MAAM,UAAU,EAAE,MAAU,CAAC;AAEpC,eAAO,MAAM,gBAAgB,EAAE,MAAc,CAAC;AAC9C,eAAO,MAAM,aAAa,EAAE,MAAe,CAAC;AAS5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,qBAAa,YAAY;;gBAMX,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU;IAgBtF,KAAK,CAAC,CAAC,SAAS,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC;CA2M/F"}
|
package/PromiseCache.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _PromiseCache_instances, _PromiseCache_redis, _PromiseCache_localCache, _PromiseCache_clock, _PromiseCache_randomizer, _PromiseCache_localCachedAnswer, _PromiseCache_maybeRefreshCache, _PromiseCache_refreshCache, _PromiseCache_pollForCache, _PromiseCache_updateCache, _PromiseCache_calculateRamp, _PromiseCache_redisEntry, _PromiseCache_enforceTTL;
|
|
13
|
+
// Isolate the random() method to avoid passing the Math package around
|
|
14
|
+
export const MathRandomizer = {
|
|
15
|
+
random() {
|
|
16
|
+
return Math.random();
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
export const SystemClock = {
|
|
20
|
+
now() {
|
|
21
|
+
return new Date();
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
export function _namespaced(key) {
|
|
25
|
+
return `__devvit_cache_val__${key}`;
|
|
26
|
+
}
|
|
27
|
+
export function _lock(key) {
|
|
28
|
+
return `__devvit_cache_lock__${key}`;
|
|
29
|
+
}
|
|
30
|
+
const pollEvery = 300; // milliseconds
|
|
31
|
+
const maxPollingTimeout = 1000; // milliseconds
|
|
32
|
+
const minTtlValue = 5000; // milliseconds
|
|
33
|
+
export const retryLimit = 3;
|
|
34
|
+
const errorRetryProbability = 0.1;
|
|
35
|
+
export const clientRetryDelay = 1000; // milliseconds
|
|
36
|
+
export const allowStaleFor = 30000; // milliseconds
|
|
37
|
+
function _unwrap(entry) {
|
|
38
|
+
if (entry.error) {
|
|
39
|
+
throw new Error(entry.error);
|
|
40
|
+
}
|
|
41
|
+
return entry.value;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* This class is responsible for managing the caching of promises. It is a layered cache, meaning it will first check
|
|
45
|
+
* the local in-memory cache, then the redis cache, and finally the source of truth. It will also handle refreshing the cache according
|
|
46
|
+
* to the TTL and error handling.
|
|
47
|
+
*
|
|
48
|
+
* The local cache is shared across all requests to a given pod, while the redis cache is shared across all requests to all pods.
|
|
49
|
+
*
|
|
50
|
+
* Please note that in order to prevent a stampede of requests to the source of truth, we use a lock in redis to ensure only one
|
|
51
|
+
* request is made to the source of truth at a time. If the lock is obtained, the cache will be updated and the lock will be released.
|
|
52
|
+
*
|
|
53
|
+
* Additionally, we use a polling mechanism to fetch the cache if the lock is not obtained. This is to prevent unnecessary errors.
|
|
54
|
+
*
|
|
55
|
+
* Finally, we also want to prevent stampedes against redis for the lock election and the retries. We use a ramping probability to ease in the
|
|
56
|
+
* attempts to get the lock, and not every error will trigger a retry.
|
|
57
|
+
*
|
|
58
|
+
* This means that the cache will be eventually consistent, but will not be immediately consistent. This is a tradeoff we are willing to make.
|
|
59
|
+
* Additionally, this means that the TTL is not precise. The cache may be updated a bit more often than the TTL, but it will not be updated less often.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* import { cache, reddit } from "@devvit/web/server";
|
|
64
|
+
* export function fetchSnoovatarUrl(username: string): Promise<string> {
|
|
65
|
+
* return await cache(
|
|
66
|
+
* async () => {
|
|
67
|
+
* const url = await reddit.getSnoovatarUrl(username)
|
|
68
|
+
* console.log(`snoovatar URL cache busted for ${username}`)
|
|
69
|
+
* if (!url) {
|
|
70
|
+
* throw new Error(`Failed to fetch snoovatar URL for user ${username}`);
|
|
71
|
+
* }
|
|
72
|
+
* return url;
|
|
73
|
+
* },
|
|
74
|
+
* {
|
|
75
|
+
* key: `snoovatar_by_${username}`,
|
|
76
|
+
* ttl: 24 * 60 * 60 * 1000 // expire after one day.
|
|
77
|
+
* }
|
|
78
|
+
* );
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export class PromiseCache {
|
|
83
|
+
constructor(redis, localCache, clock, randomizer) {
|
|
84
|
+
_PromiseCache_instances.add(this);
|
|
85
|
+
_PromiseCache_redis.set(this, void 0);
|
|
86
|
+
_PromiseCache_localCache.set(this, void 0);
|
|
87
|
+
_PromiseCache_clock.set(this, void 0);
|
|
88
|
+
_PromiseCache_randomizer.set(this, void 0);
|
|
89
|
+
__classPrivateFieldSet(this, _PromiseCache_redis, redis, "f");
|
|
90
|
+
__classPrivateFieldSet(this, _PromiseCache_localCache, localCache, "f");
|
|
91
|
+
__classPrivateFieldSet(this, _PromiseCache_clock, clock, "f");
|
|
92
|
+
__classPrivateFieldSet(this, _PromiseCache_randomizer, randomizer, "f");
|
|
93
|
+
}
|
|
94
|
+
/*
|
|
95
|
+
* Main entry point for caching a promise. This will first check the local cache, then the redis cache, and finally
|
|
96
|
+
* call the provided closure to get the value. It will also handle refreshing the cache according to the TTL and error handling.
|
|
97
|
+
*
|
|
98
|
+
* When interacting with redis, this method may run:
|
|
99
|
+
* - GET __autocache__${key} to get the cached value for a given key
|
|
100
|
+
* - SET __autocache__${key} ${value} to set the cached value for a given key
|
|
101
|
+
* - SET __lock__${key} '1' and DEL __lock__${key} to acquire and release a lock for a given key
|
|
102
|
+
*/
|
|
103
|
+
async cache(closure, options) {
|
|
104
|
+
__classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_enforceTTL).call(this, options);
|
|
105
|
+
const localCachedAnswer = __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_localCachedAnswer).call(this, options.key);
|
|
106
|
+
if (localCachedAnswer !== undefined) {
|
|
107
|
+
return localCachedAnswer;
|
|
108
|
+
}
|
|
109
|
+
const existing = await __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_redisEntry).call(this, options.key);
|
|
110
|
+
const entry = await __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_maybeRefreshCache).call(this, options, existing, closure);
|
|
111
|
+
return _unwrap(entry);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
_PromiseCache_redis = new WeakMap(), _PromiseCache_localCache = new WeakMap(), _PromiseCache_clock = new WeakMap(), _PromiseCache_randomizer = new WeakMap(), _PromiseCache_instances = new WeakSet(), _PromiseCache_localCachedAnswer = function _PromiseCache_localCachedAnswer(key) {
|
|
115
|
+
const val = __classPrivateFieldGet(this, _PromiseCache_localCache, "f")[key];
|
|
116
|
+
if (val) {
|
|
117
|
+
const now = __classPrivateFieldGet(this, _PromiseCache_clock, "f").now().getTime();
|
|
118
|
+
const hasRetryableError = val?.error &&
|
|
119
|
+
val?.errorTime &&
|
|
120
|
+
val.errorCount < retryLimit &&
|
|
121
|
+
__classPrivateFieldGet(this, _PromiseCache_randomizer, "f").random() < errorRetryProbability &&
|
|
122
|
+
val.errorTime + clientRetryDelay < now;
|
|
123
|
+
const expired = val?.expires && val.expires < now && val.checkedAt + clientRetryDelay < now;
|
|
124
|
+
if (expired || hasRetryableError) {
|
|
125
|
+
delete __classPrivateFieldGet(this, _PromiseCache_localCache, "f")[key];
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return _unwrap(val);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}, _PromiseCache_maybeRefreshCache =
|
|
134
|
+
/**
|
|
135
|
+
* If we've bothered to check redis, we're already on the backend. Let's see if the cache either (1) contains an error, (2)
|
|
136
|
+
* is expired, (3) is missing, or (4) is about to expire. If any of these are true, we'll refresh the cache based on heuristics.
|
|
137
|
+
*
|
|
138
|
+
* We'll always refresh if missing or expired, but its probabilistic if we'll refresh if about to expire or if we have an error.
|
|
139
|
+
*/
|
|
140
|
+
async function _PromiseCache_maybeRefreshCache(options, entry, closure) {
|
|
141
|
+
const expires = entry?.expires;
|
|
142
|
+
const rampProbability = expires ? __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_calculateRamp).call(this, expires) : 1;
|
|
143
|
+
if (!entry ||
|
|
144
|
+
(entry?.error &&
|
|
145
|
+
entry.errorCount < retryLimit &&
|
|
146
|
+
errorRetryProbability > __classPrivateFieldGet(this, _PromiseCache_randomizer, "f").random()) ||
|
|
147
|
+
rampProbability > __classPrivateFieldGet(this, _PromiseCache_randomizer, "f").random()) {
|
|
148
|
+
return __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_refreshCache).call(this, options, entry, closure);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
return entry;
|
|
152
|
+
}
|
|
153
|
+
}, _PromiseCache_refreshCache =
|
|
154
|
+
/**
|
|
155
|
+
* The conditions for refreshing the cache are handled in the calling method, which should be
|
|
156
|
+
* #maybeRefreshCache.
|
|
157
|
+
*
|
|
158
|
+
* If you don't win the lock, you'll poll for the cache. If you don't get the cache within maxPollingTimeout, you'll throw an error.
|
|
159
|
+
*/
|
|
160
|
+
async function _PromiseCache_refreshCache(options, entry, closure) {
|
|
161
|
+
const lockKey = _lock(options.key);
|
|
162
|
+
const now = __classPrivateFieldGet(this, _PromiseCache_clock, "f").now().getTime();
|
|
163
|
+
/**
|
|
164
|
+
* The write lock should last for a while, but not the full TTL. Hopefully write attempts settle down after a while.
|
|
165
|
+
*/
|
|
166
|
+
const lockExpiration = new Date(now + options.ttl / 2);
|
|
167
|
+
const lockObtained = await __classPrivateFieldGet(this, _PromiseCache_redis, "f").set(lockKey, '1', {
|
|
168
|
+
expiration: lockExpiration,
|
|
169
|
+
nx: true,
|
|
170
|
+
});
|
|
171
|
+
if (lockObtained) {
|
|
172
|
+
return __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_updateCache).call(this, options.key, entry, closure, options.ttl);
|
|
173
|
+
}
|
|
174
|
+
else if (entry) {
|
|
175
|
+
// This entry is still valid, return it
|
|
176
|
+
return entry;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const start = __classPrivateFieldGet(this, _PromiseCache_clock, "f").now();
|
|
180
|
+
return __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_pollForCache).call(this, start, options.key, options.ttl);
|
|
181
|
+
}
|
|
182
|
+
}, _PromiseCache_pollForCache = async function _PromiseCache_pollForCache(start, key, ttl) {
|
|
183
|
+
const pollingTimeout = Math.min(ttl, maxPollingTimeout);
|
|
184
|
+
const existing = await __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_redisEntry).call(this, key);
|
|
185
|
+
if (existing) {
|
|
186
|
+
return existing;
|
|
187
|
+
}
|
|
188
|
+
if (__classPrivateFieldGet(this, _PromiseCache_clock, "f").now().getTime() - start.getTime() >= pollingTimeout) {
|
|
189
|
+
throw new Error(`Cache request timed out trying to get data at key: ${key}`);
|
|
190
|
+
}
|
|
191
|
+
await new Promise((resolve) => setTimeout(resolve, pollEvery));
|
|
192
|
+
return __classPrivateFieldGet(this, _PromiseCache_instances, "m", _PromiseCache_pollForCache).call(this, start, key, ttl);
|
|
193
|
+
}, _PromiseCache_updateCache =
|
|
194
|
+
/**
|
|
195
|
+
* Actually update the cache. This is the method that will be called if we have the lock.
|
|
196
|
+
*/
|
|
197
|
+
async function _PromiseCache_updateCache(key, entry, closure, ttl) {
|
|
198
|
+
const expires = __classPrivateFieldGet(this, _PromiseCache_clock, "f").now().getTime() + ttl;
|
|
199
|
+
entry = entry ?? {
|
|
200
|
+
value: null,
|
|
201
|
+
expires,
|
|
202
|
+
errorCount: 0,
|
|
203
|
+
error: null,
|
|
204
|
+
errorTime: null,
|
|
205
|
+
checkedAt: 0,
|
|
206
|
+
};
|
|
207
|
+
try {
|
|
208
|
+
entry.value = await closure();
|
|
209
|
+
entry.error = null;
|
|
210
|
+
entry.errorCount = 0;
|
|
211
|
+
entry.errorTime = null;
|
|
212
|
+
}
|
|
213
|
+
catch (err) {
|
|
214
|
+
entry.value = null;
|
|
215
|
+
entry.error = err instanceof Error ? err.message : 'unknown error';
|
|
216
|
+
entry.errorTime = __classPrivateFieldGet(this, _PromiseCache_clock, "f").now().getTime();
|
|
217
|
+
entry.errorCount++;
|
|
218
|
+
}
|
|
219
|
+
__classPrivateFieldGet(this, _PromiseCache_localCache, "f")[key] = entry;
|
|
220
|
+
await __classPrivateFieldGet(this, _PromiseCache_redis, "f").set(_namespaced(key), JSON.stringify(entry), {
|
|
221
|
+
expiration: new Date(expires + allowStaleFor),
|
|
222
|
+
});
|
|
223
|
+
/**
|
|
224
|
+
* Unlocking will allow retries to happen if there was an error. Otherwise we don't unlock, because the lock
|
|
225
|
+
* will expire on its own.
|
|
226
|
+
*/
|
|
227
|
+
if (entry.error && entry.errorCount < retryLimit) {
|
|
228
|
+
await __classPrivateFieldGet(this, _PromiseCache_redis, "f").del(_lock(key));
|
|
229
|
+
}
|
|
230
|
+
return entry;
|
|
231
|
+
}, _PromiseCache_calculateRamp = function _PromiseCache_calculateRamp(expiry) {
|
|
232
|
+
const now = __classPrivateFieldGet(this, _PromiseCache_clock, "f").now().getTime();
|
|
233
|
+
const remaining = expiry - now;
|
|
234
|
+
if (remaining < 0) {
|
|
235
|
+
return 1;
|
|
236
|
+
}
|
|
237
|
+
else if (remaining < 1000) {
|
|
238
|
+
return 0.1;
|
|
239
|
+
}
|
|
240
|
+
else if (remaining < 2000) {
|
|
241
|
+
return 0.01;
|
|
242
|
+
}
|
|
243
|
+
else if (remaining < 3000) {
|
|
244
|
+
return 0.001;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
return 0;
|
|
248
|
+
}
|
|
249
|
+
}, _PromiseCache_redisEntry = async function _PromiseCache_redisEntry(key) {
|
|
250
|
+
const val = await __classPrivateFieldGet(this, _PromiseCache_redis, "f").get(_namespaced(key));
|
|
251
|
+
if (val) {
|
|
252
|
+
const entry = JSON.parse(val);
|
|
253
|
+
entry.checkedAt = __classPrivateFieldGet(this, _PromiseCache_clock, "f").now().getTime();
|
|
254
|
+
__classPrivateFieldGet(this, _PromiseCache_localCache, "f")[key] = entry;
|
|
255
|
+
return entry;
|
|
256
|
+
}
|
|
257
|
+
return undefined;
|
|
258
|
+
}, _PromiseCache_enforceTTL = function _PromiseCache_enforceTTL(options) {
|
|
259
|
+
if (options.ttl < minTtlValue) {
|
|
260
|
+
console.warn(`Cache TTL cannot be less than ${minTtlValue} milliseconds! Updating ttl value of ${options.ttl} to ${minTtlValue}.`);
|
|
261
|
+
options.ttl = minTtlValue;
|
|
262
|
+
}
|
|
263
|
+
};
|
package/README.md
ADDED
package/cache.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type RedisClient } from '@devvit/redis';
|
|
2
|
+
import type { JsonValue } from '@devvit/shared-types/json.js';
|
|
3
|
+
import type { CacheOptions, Clock, LocalCache, Randomizer } from './PromiseCache.js';
|
|
4
|
+
export type CacheHelper = <T extends JsonValue>(fn: () => Promise<T>, options: CacheOptions) => Promise<T>;
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare function makeCache(redis: RedisClient, localCache: LocalCache, clock: Clock, randomizer: Randomizer): CacheHelper;
|
|
9
|
+
export declare const cache: CacheHelper;
|
|
10
|
+
//# sourceMappingURL=cache.d.ts.map
|
package/cache.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAE9D,OAAO,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGrF,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,SAAS,SAAS,EAC5C,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,YAAY,KAClB,OAAO,CAAC,CAAC,CAAC,CAAC;AAEhB;;GAEG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,WAAW,EAClB,UAAU,EAAE,UAAU,EACtB,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,UAAU,GACrB,WAAW,CAGb;AAED,eAAO,MAAM,KAAK,EAAE,WAA+D,CAAC"}
|
package/cache.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { redis } from '@devvit/redis';
|
|
2
|
+
import { MathRandomizer, PromiseCache, SystemClock } from './PromiseCache.js';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export function makeCache(redis, localCache, clock, randomizer) {
|
|
7
|
+
const pc = new PromiseCache(redis, localCache, clock, randomizer);
|
|
8
|
+
return pc.cache.bind(pc);
|
|
9
|
+
}
|
|
10
|
+
export const cache = makeCache(redis, {}, SystemClock, MathRandomizer);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.test.d.ts","sourceRoot":"","sources":["../src/cache.test.ts"],"names":[],"mappings":""}
|
package/index.d.ts
ADDED
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cache.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@devvit/cache",
|
|
3
|
+
"version": "0.12.1-next-2025-10-02-22-58-58-07942952e.0",
|
|
4
|
+
"license": "BSD-3-Clause",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://developers.reddit.com/"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"browser": "./serverImportInClientCodePanic.js",
|
|
13
|
+
"default": "./index.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"**"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"clean": "rm -rf .turbo coverage dist",
|
|
23
|
+
"clobber": "yarn clean && rm -rf node_modules",
|
|
24
|
+
"dev": "tsc -w",
|
|
25
|
+
"lint": "redlint .",
|
|
26
|
+
"lint:fix": "yarn lint --fix",
|
|
27
|
+
"prepublishOnly": "publish-package-json",
|
|
28
|
+
"test": "yarn test:unit && yarn test:types && yarn lint",
|
|
29
|
+
"test:types": "tsc --noEmit",
|
|
30
|
+
"test:unit": "vitest run",
|
|
31
|
+
"test:unit-with-coverage": "vitest run --coverage"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@devvit/redis": "0.12.1-next-2025-10-02-22-58-58-07942952e.0",
|
|
35
|
+
"@devvit/shared-types": "0.12.1-next-2025-10-02-22-58-58-07942952e.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@devvit/repo-tools": "0.12.1-next-2025-10-02-22-58-58-07942952e.0",
|
|
39
|
+
"@devvit/tsconfig": "0.12.1-next-2025-10-02-22-58-58-07942952e.0",
|
|
40
|
+
"eslint": "9.11.1",
|
|
41
|
+
"typescript": "5.8.3",
|
|
42
|
+
"vitest": "1.6.1"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "f4532ed7df33f9251f1f1418bd7a41c9b92fbb16"
|
|
45
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hey! If you're seeing this, you're probably trying to run server code in the
|
|
3
|
+
* client context, or you have your server build environment set up incorrectly.
|
|
4
|
+
* To fix this:
|
|
5
|
+
* 1) Check that you're not importing server code in your client files. If you
|
|
6
|
+
* are, you should import from `@devvit/web/client` instead of
|
|
7
|
+
* `@devvit/web/server`. `@devvit/cache` is server only and has no client
|
|
8
|
+
* support.
|
|
9
|
+
* 2) If this is coming from server code, ensure that your client's tsconfig &
|
|
10
|
+
* build environment is set up correctly - specifically, make sure you do NOT
|
|
11
|
+
* have `compilerOptions.customConditions` set to include `["browser"]` in
|
|
12
|
+
* your `tsconfig.json` file. Also, verify that whatever bundler you're using
|
|
13
|
+
* (esbuild, vite, etc.) is bundling the client code for a Node environment,
|
|
14
|
+
* and not a web browser!
|
|
15
|
+
*/
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=serverImportInClientCodePanic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverImportInClientCodePanic.d.ts","sourceRoot":"","sources":["../src/serverImportInClientCodePanic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hey! If you're seeing this, you're probably trying to run server code in the
|
|
3
|
+
* client context, or you have your server build environment set up incorrectly.
|
|
4
|
+
* To fix this:
|
|
5
|
+
* 1) Check that you're not importing server code in your client files. If you
|
|
6
|
+
* are, you should import from `@devvit/web/client` instead of
|
|
7
|
+
* `@devvit/web/server`. `@devvit/cache` is server only and has no client
|
|
8
|
+
* support.
|
|
9
|
+
* 2) If this is coming from server code, ensure that your client's tsconfig &
|
|
10
|
+
* build environment is set up correctly - specifically, make sure you do NOT
|
|
11
|
+
* have `compilerOptions.customConditions` set to include `["browser"]` in
|
|
12
|
+
* your `tsconfig.json` file. Also, verify that whatever bundler you're using
|
|
13
|
+
* (esbuild, vite, etc.) is bundling the client code for a Node environment,
|
|
14
|
+
* and not a web browser!
|
|
15
|
+
*/
|
|
16
|
+
console.error("Can't import server code in the client!");
|
|
17
|
+
export {};
|