Skip to content

Commit

Permalink
fix(memoizer): json deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-pr-p committed Jan 20, 2020
1 parent 7e8a717 commit 1981209
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "memoredis",
"version": "1.0.1",
"version": "1.0.2",
"description": "Redis memoization library with good Typescript generics, locking, and argument-wide bulk invalidation",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
27 changes: 27 additions & 0 deletions src/lib/memoizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,30 @@ describe('invalidation', () => {
expect(mock).toHaveBeenCalledTimes(1);
});
});

describe('prefixes', () => {
test('should keep keyspaces separate', async () => {
const a = createMemoizer({ prefix: 'a' });
const b = createMemoizer({ prefix: 'b' });

const mock = jest.fn();

const memoizableFunction = async () => {
mock();
return 4;
};

const aF = a.memoize(memoizableFunction, {
key: 'prefix-test'
});

const bF = b.memoize(memoizableFunction, {
key: 'prefix-test'
});

await aF({ a: 1 });
await bF({ a: 1 });

expect(mock).toHaveBeenCalledTimes(2);
});
});
2 changes: 1 addition & 1 deletion src/lib/memoizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const createMemoizer = (instanceOpts: MemoizerOpts) => {
client.get(key, (err, reply) =>
err
? reject(err)
: resolve(reply === null ? (JSON.parse(reply) as T) : (reply as null))
: resolve(reply !== null ? (JSON.parse(reply) as T) : (reply as null))
)
);

Expand Down

0 comments on commit 1981209

Please sign in to comment.