Skip to content

Commit

Permalink
fix: rename swv to swr on CacheMetadata
Browse files Browse the repository at this point in the history
this was a typo, short for stale while revalidate

fix #6

BREAKING CHANGE:
CacheMetadata.swv has been renamed to CacheMetadata.swr
for the most part this will be handled by the library
(existing cache entries with `swv` will continue to work, only new ones will use `swr`)

Migration:
When you worked with `CacheMetadata` (for example in a custom cache adapter)
you need to adapt your code to:

1. write `swr` instead of `swv`
   👍 new: `const metadata: CacheMetadata = { swr: 1000, /* ... */ }`
   ⛔️ old: `const metadata: CacheMetadata = { swv: 1000, /* ... */ }`
2. read using the `staleWhileRevalidate` helper
   👍 new: `const staleMs = staleWhileRevalidate(metadata)`
   ⛔️ old: `const staleMs = metadata.swv`
  • Loading branch information
Xiphe committed Oct 24, 2022
1 parent c3603b1 commit b0558a5
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 75 deletions.
28 changes: 15 additions & 13 deletions src/adapters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache, CacheEntry } from './common';
import { Cache, CacheEntry, totalTtl } from './common';

export interface LRUishCache extends Omit<Cache, 'set'> {
set(
Expand All @@ -12,10 +12,9 @@ export function lruCacheAdapter(lruCache: LRUishCache): Cache {
return {
name: lruCache.name || 'LRU',
set(key, value) {
const ttl = totalTtl(value?.metadata);
return lruCache.set(key, value, {
ttl:
(value?.metadata?.ttl || 0) + (value?.metadata?.swv || 0) ||
undefined,
ttl: ttl === Infinity ? undefined : ttl,
start: value?.metadata?.createdTime,
});
},
Expand Down Expand Up @@ -53,7 +52,7 @@ export function redis3CacheAdapter(redisCache: Redis3LikeCache): Cache {
name: redisCache.name || 'Redis3',
set(key, value) {
return new Promise<void>((res, rej) => {
const ttl = (value?.metadata?.ttl || 0) + (value?.metadata?.swv || 0);
const ttl = totalTtl(value?.metadata);
const createdTime = value?.metadata?.createdTime;
const cb = (err: unknown) => {
if (err) {
Expand All @@ -62,7 +61,7 @@ export function redis3CacheAdapter(redisCache: Redis3LikeCache): Cache {
res();
};

if (ttl > 0 && typeof createdTime === 'number') {
if (ttl > 0 && ttl < Infinity && typeof createdTime === 'number') {
redisCache
.multi()
.set(key, JSON.stringify(value))
Expand Down Expand Up @@ -118,15 +117,18 @@ export function redisCacheAdapter(redisCache: RedisLikeCache): Cache {
return {
name: redisCache.name || 'Redis',
set(key, value) {
const ttl = (value?.metadata?.ttl || 0) + (value?.metadata?.swv || 0);
const ttl = totalTtl(value?.metadata);
const createdTime = value?.metadata?.createdTime;

return redisCache.set(key, JSON.stringify(value), {
EXAT:
ttl > 0 && typeof createdTime === 'number'
? (ttl + createdTime) / 1000
: 0,
});
return redisCache.set(
key,
JSON.stringify(value),
ttl > 0 && ttl < Infinity && typeof createdTime === 'number'
? {
EXAT: (ttl + createdTime) / 1000,
}
: undefined,
);
},
async get(key) {
const value = await redisCache.get(key);
Expand Down
Loading

0 comments on commit b0558a5

Please sign in to comment.