forked from sushi-labs/sushiswap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from rainlanguage/2024-05-04-memoizer
memoizer
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import fs from 'fs' | ||
import { describe, expect, it } from 'vitest' | ||
import { memoizer } from './memoizer.js' | ||
|
||
describe('Memoizer', async () => { | ||
it('should serialize, memoize, read from cache, deserialize', async () => { | ||
let didHitCacheOnce = false | ||
const testFn = (value1: any) => { | ||
didHitCacheOnce = !didHitCacheOnce | ||
return { | ||
...value1, | ||
someOtherValue: 'some data', | ||
} | ||
} | ||
const testMemoizer = await memoizer.fn(testFn) | ||
|
||
const testValue = { | ||
bigint: 12345n, | ||
string: 'some text', | ||
number: 123, | ||
bool: true, | ||
obj: { | ||
prop: 'some prop', | ||
}, | ||
} | ||
const noCacheHitReturnedValue = await testMemoizer(testValue) | ||
const cacheHitReturnedValue = await testMemoizer(testValue) | ||
const expectedReturnedValue = { | ||
bigint: 12345n, | ||
string: 'some text', | ||
number: 123, | ||
bool: true, | ||
obj: { | ||
prop: 'some prop', | ||
}, | ||
someOtherValue: 'some data', | ||
} | ||
|
||
expect(didHitCacheOnce).toEqual(true) | ||
expect(noCacheHitReturnedValue).toStrictEqual(expectedReturnedValue) | ||
expect(cacheHitReturnedValue).toStrictEqual(expectedReturnedValue) | ||
|
||
// read cached file content | ||
const cacheFileContent = fs.readFileSync( | ||
`./mem-cache/${fs.readdirSync('./mem-cache')[0]}`, | ||
{ encoding: 'utf-8' }, | ||
) | ||
const expectedCachedContent = | ||
'{"data":{"bigint":"12345n","string":"some text","number":123,"bool":true,"obj":{"prop":"some prop"},"someOtherValue":"some data"}}' | ||
|
||
expect(cacheFileContent).toEqual(expectedCachedContent) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import memoize from 'memoize-fs' | ||
|
||
const serialize = (val: any) => { | ||
const circRefColl: any[] = [] | ||
return JSON.stringify(val, (_name, value) => { | ||
if (typeof value === 'function') { | ||
return // ignore arguments and attributes of type function silently | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (circRefColl.indexOf(value) !== -1) { | ||
// circular reference has been found, discard key | ||
return | ||
} | ||
// store value in collection | ||
circRefColl.push(value) | ||
} | ||
if (typeof value === 'bigint') return `${value.toString()}n` | ||
return value | ||
}) | ||
} | ||
|
||
const deserialize = (val: string) => { | ||
return JSON.parse(val, (_key, value) => { | ||
if (typeof value === 'string' && /^\d+n$/.test(value)) { | ||
return BigInt(value.slice(0, -1)) | ||
} else return value | ||
}).data | ||
} | ||
|
||
export const memoizer = memoize({ | ||
cachePath: './mem-cache', | ||
serialize, | ||
deserialize, | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.