Skip to content

Commit

Permalink
feat: persistent max age
Browse files Browse the repository at this point in the history
  • Loading branch information
borisdiakur committed Jul 3, 2024
1 parent 5b4f3e5 commit 1414391
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 4.0.0 (2024-06-20)
* __BREAKING CHANGE__: The `maxAge` option now ensures that the cache becomes invalid after the specified cache lifetime is reached based on `stats.mtimeMs` (last modification time of the cache file) instead of relying on an in memory timeout that invalidates the cache. This ensures that cache life times are evaluated correctly between multiple processes.

#### How to upgrade
If you are using the `maxAge` option, ensure that you add the option to every `memoizer.fn` call (if you have multiple) to reliably check cache validity. Most probably this is already the case in your application, and you don't need to change anything.

### 3.0.4 (2024-03-06)
* style: improve typing by using Awaited instead of our custom EnsurePromise type

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ memoizer.fn(fnToMemoize, { salt: 'foobar' })

### maxAge

With `maxAge` option you can ensure that cache for given call is cleared after a predefined period of time (in milliseconds).
You can ensure that cache becomes invalid after a cache lifetime defined by the `maxAge` option is reached. memoize-fs uses [stats.mtimeMs](https://nodejs.org/api/fs.html#statsmtimems) (last modification time) when checking the age of the cache.

```js
memoizer.fn(fnToMemoize, { maxAge: 10000 })
Expand Down
10 changes: 5 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ describe('memoize-fs', () => {
)
})

it('invalidates cache after timeout with maxAge option set', async () => {
it('checks cache age if maxAge option is set', async () => {
const cachePath = FIXTURE_CACHE
const memoize = memoizeFs({ cachePath })
let c = 3
Expand All @@ -1262,7 +1262,7 @@ describe('memoize-fs', () => {
function (a: number, b: number) {
return a + b + c
},
{ cacheId: 'foobar' }
{ cacheId: 'foobar', maxAge: 10 }
)
result = await memFn(1, 2)
assert.strictEqual(result, 6, 'expected result to strictly equal 7')
Expand All @@ -1274,7 +1274,7 @@ describe('memoize-fs', () => {
function (a: number, b: number) {
return a + b + c
},
{ cacheId: 'foobar' }
{ cacheId: 'foobar', maxAge: 10 }
)
result = await memFn(1, 2)
assert.strictEqual(result, 7, 'expected result to strictly equal 7')
Expand Down Expand Up @@ -1307,7 +1307,7 @@ describe('memoize-fs', () => {
function (a: number, b: number) {
return a + b + c
},
{ cacheId: 'foobar' }
{ cacheId: 'foobar', maxAge: 10 }
)
result = await memFn(1, 2)
assert.strictEqual(result, 6, 'expected result to strictly equal 7')
Expand All @@ -1319,7 +1319,7 @@ describe('memoize-fs', () => {
function (a: number, b: number) {
return a + b + c
},
{ cacheId: 'foobar' }
{ cacheId: 'foobar', maxAge: 10 }
)
result = await memFn(1, 2)
assert.strictEqual(result, 7, 'expected result to strictly equal 7')
Expand Down
44 changes: 33 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface MemoizerOptions {

function serialize(val: unknown) {
const circRefColl: unknown[] = []
return JSON.stringify(val, function (name, value) {
return JSON.stringify(val, function (_name, value) {
if (typeof value === 'function') {
return // ignore arguments and attributes of type function silently
}
Expand Down Expand Up @@ -131,15 +131,6 @@ async function writeResult(
} else {
resultString = '{"data":' + r + '}'
}
if (optExt.maxAge) {
setTimeout(function () {
fs.rm(filePath, { recursive: true }).catch(function (err) {
if (err && err.code !== 'ENOENT') {
throw err
}
})
}, optExt.maxAge)
}
try {
await fs.writeFile(filePath, resultString)
cb()
Expand Down Expand Up @@ -245,6 +236,33 @@ async function processFnAsync<FN>(
;(fn as (...args: unknown[]) => unknown).apply(null, args)
}

async function checkFileAgeAndRead(filePath: string, maxAge?: number): Promise<string | null> {
let fileHandle;
try {
fileHandle = await fs.open(filePath, 'r');

if (maxAge !== undefined) {
const stats = await fileHandle.stat();

const now = new Date().getTime();
const fileAge = now - stats.mtimeMs;

if (fileAge > maxAge) {
return null;
}
}

const content = await fs.readFile(filePath, { encoding: 'utf8' });
return content;
} catch (err) {
return null;
} finally {
if (fileHandle) {
await fileHandle.close();
}
}
}

export default function buildMemoizer(
memoizerOptions: Partial<MemoizerOptions>
) {
Expand Down Expand Up @@ -321,8 +339,12 @@ export default function buildMemoizer(
await processFn(fn, args, allOptions, filePath, resolve, reject)
}

fs.readFile(filePath, { encoding: 'utf8' })
checkFileAgeAndRead(filePath, allOptions.maxAge)
.then(function (data) {
if (data === null) {
return cacheAndReturn()
}

const parsedData = parseResult(data, allOptions.deserialize)
if (allOptions.retryOnInvalidCache && parsedData === undefined) {
return cacheAndReturn()
Expand Down

0 comments on commit 1414391

Please sign in to comment.