Skip to content

Commit

Permalink
flat-cache - loading the expiration from file correctly (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray authored Dec 11, 2024
1 parent 8c1eee8 commit 403d872
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
26 changes: 22 additions & 4 deletions packages/cacheable/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export type CacheableMemoryOptions = {
checkInterval?: number;
};

export type SetOptions = {
ttl?: number | string;
expire?: number | Date;
};

export class CacheableMemory extends Hookified {
private _lru = new DoublyLinkedList<string>();
private readonly _hashCache = new Map<string, number>();
Expand Down Expand Up @@ -239,14 +244,27 @@ export class CacheableMemory extends Hookified {
* If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live.
* @returns {void}
*/
public set(key: string, value: any, ttl?: number | string): void {
public set(key: string, value: any, ttl?: number | string | SetOptions): void {
const store = this.getStore(key);
let expires;
if (ttl !== undefined || this._ttl !== undefined) {
const finalTtl = shorthandToTime(ttl ?? this._ttl);
if (typeof ttl === 'object') {
if (ttl.expire) {
expires = typeof ttl.expire === 'number' ? ttl.expire : ttl.expire.getTime();
}

if (finalTtl !== undefined) {
expires = finalTtl;
if (ttl.ttl) {
const finalTtl = shorthandToTime(ttl.ttl);
if (finalTtl !== undefined) {
expires = finalTtl;
}
}
} else {
const finalTtl = shorthandToTime(ttl ?? this._ttl);

if (finalTtl !== undefined) {
expires = finalTtl;
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions packages/cacheable/test/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,37 @@ describe('cacheable wrap', async () => {
const result2 = wrapped(1);
expect(result).toBe(result2); // Cached
});

test('should be able to pass in expiration time', async () => {
const cacheable = new CacheableMemory();
const expire = Date.now() + 100;
cacheable.set('key-expire1', 'value1', {expire});
const result = cacheable.get('key-expire1');
expect(result).toBe('value1');
await sleep(150);
const result2 = cacheable.get('key-expire1');
expect(result2).toBeUndefined();
});

test('should be able to pass in ttl as object', async () => {
const cacheable = new CacheableMemory();
const ttl = '100ms';
cacheable.set('key-expire12', 'value1', {ttl});
const result = cacheable.get('key-expire12');
expect(result).toBe('value1');
await sleep(150);
const result2 = cacheable.get('key-expire12');
expect(result2).toBeUndefined();
});

test('should be able to pass in expiration time with date', async () => {
const cacheable = new CacheableMemory();
const expire = new Date(Date.now() + 100);
cacheable.set('key-expire2', 'value2', {expire});
const result = cacheable.get('key-expire2');
expect(result).toBe('value2');
await sleep(150);
const result2 = cacheable.get('key-expire2');
expect(result2).toBeUndefined();
});
});
5 changes: 3 additions & 2 deletions packages/flat-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ export class FlatCache extends Hookified {
const items = this._parse(data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
for (const key of Object.keys(items)) {
this._cache.set(key, items[key]);
console.log('key', items[key]);
this._cache.set(items[key].key as string, items[key].value, {expire: items[key].expires as number});
}

this._changesSinceLastSave = true;
Expand Down Expand Up @@ -327,7 +328,7 @@ export class FlatCache extends Hookified {
try {
if (this._changesSinceLastSave || force) {
const filePath = this.cacheFilePath;
const items = this.all();
const items = Array.from(this._cache.items);
const data = this._stringify(items);

// Ensure the directory exists
Expand Down
23 changes: 23 additions & 0 deletions packages/flat-cache/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ describe('flat-cache load from persisted cache', () => {
expect(secondCache.getKey('baz')).toEqual([1, 2, 3]);
firstCache.destroy(true);
});

test('should load the cache from the file with expiration', async () => {
// eslint-disable-next-line unicorn/prevent-abbreviations
const cacheDir = '.cachefoo3';
const cacheId = 'cache4';
const firstCache = new FlatCache({cacheDir, cacheId});
firstCache.setKey('foo', 'bar', 250);
firstCache.setKey('bar', {foo: 'bar'}, 500);
firstCache.setKey('baz', [1, 2, 3]);
firstCache.save();
const secondCache = new FlatCache({cacheDir});
secondCache.load(cacheId);
expect(secondCache.getKey('foo')).toBe('bar');
expect(secondCache.getKey('bar')).toEqual({foo: 'bar'});
expect(secondCache.getKey('baz')).toEqual([1, 2, 3]);
await sleep(300);
expect(secondCache.getKey('foo')).toBeUndefined();
expect(secondCache.getKey('bar')).toEqual({foo: 'bar'});
expect(secondCache.getKey('baz')).toEqual([1, 2, 3]);
await sleep(300);
expect(secondCache.getKey('bar')).toBeUndefined();
firstCache.destroy(true);
});
});

describe('flat-cache exported functions', () => {
Expand Down

0 comments on commit 403d872

Please sign in to comment.