Skip to content

Commit

Permalink
Use string prefix to identify buffer values
Browse files Browse the repository at this point in the history
  • Loading branch information
christianb-unito committed Aug 19, 2024
1 parent 1d8687e commit 0f52aab
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
45 changes: 45 additions & 0 deletions scripts/benchmark.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Benchmarkify from "benchmarkify";
import { randomUUID } from 'node:crypto';
import { RedisCache } from '../dist/src/lib/RedisCache.js';

const benchmark = new Benchmarkify("MessagePack", { chartImage: true }).printHeader();

const cache = new RedisCache('redis://localhost:6379');
await cache.isReady();

const setStructure = new Set();
setStructure.add('key1');
setStructure.add('key2');
const value = {
level1: {
level2: {
level3: {
l3Set: setStructure,
l3Bool: false,
},
l2Set: setStructure,
},
l1Set: setStructure,
},
};

benchmark.createSuite("serialize", { time: 1000, description: "FIGHT!" })
.add("msgpack", () => {
const key = randomUUID();
cache.setValue(key, value, 1, false);

for (let i = 0; i < 5; i++) {
cache.getValue(key);
}
})

.ref("json", () => {
const key = randomUUID();
cache.setValue(key, value, 1, true);

for (let i = 0; i < 5; i++) {
cache.getValue(key);
}
});

benchmark.run();
8 changes: 4 additions & 4 deletions src/lib/RedisCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class RedisCache extends CacheInstance {
public static TRUE_VALUE = 'f405eed4-507c-4aa5-a6d2-c1813d584b8f-TRUE';
public static FALSE_VALUE = 'f405eed4-507c-4aa5-a6d2-c1813d584b8f-FALSE';
public static JSON_PREFIX = 'f405eed4-507c-4aa5-a6d2-c1813d584b8f-JSON';
public static MSGP_PREFIX = 'f405eed4-507c-4aa5-a6d2-c1813d584b8f-MSGP';
public static ERROR_PREFIX = 'f405eed4-507c-4aa5-a6d2-c1813d584b8f-ERROR';
public static NUMBER_PREFIX = 'f405eed4-507c-4aa5-a6d2-c1813d584b8f-NUMBER';

Expand Down Expand Up @@ -168,11 +169,10 @@ export class RedisCache extends CacheInstance {
}

if (value instanceof Object) {
return MPACK.pack(value);
return `${RedisCache.MSGP_PREFIX}${MPACK.pack(value).toString('binary')}`;
}

return value;

}

/**
Expand Down Expand Up @@ -206,8 +206,8 @@ export class RedisCache extends CacheInstance {
return false;
}

if (value instanceof Buffer) {
return MPACK.unpack(value);
if (value.startsWith(RedisCache.MSGP_PREFIX)) {
return MPACK.unpack(Buffer.from(value.substring(RedisCache.MSGP_PREFIX.length), 'binary'));
}

if (value.startsWith(RedisCache.ERROR_PREFIX)) {
Expand Down
47 changes: 44 additions & 3 deletions test/RedisCache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('RedisCache', () => {
},
};
let value = RedisCache.serializeValue(obj);
expect(value).to.be.instanceOf(Buffer);
expect(value.startsWith(RedisCache.MSGP_PREFIX)).to.be.true;
value = RedisCache.deserializeValue(value);
expect(value).to.deep.equal(obj);
});
Expand All @@ -78,7 +78,7 @@ describe('RedisCache', () => {
},
};
let value = RedisCache.serializeValue(obj);
expect(value).to.be.instanceOf(Buffer);
expect(value.startsWith(RedisCache.MSGP_PREFIX)).to.be.true;
value = RedisCache.deserializeValue(value);
expect(value).to.deep.equal(obj);
});
Expand All @@ -100,7 +100,7 @@ describe('RedisCache', () => {
},
};
let value = RedisCache.serializeValue(obj);
expect(value).to.be.instanceOf(Buffer);
expect(value.startsWith(RedisCache.MSGP_PREFIX)).to.be.true;
value = RedisCache.deserializeValue(value);
expect(value).to.deep.equal(obj);
});
Expand Down Expand Up @@ -222,6 +222,47 @@ describe('RedisCache', () => {

expect(await cache.itemCount()).to.equal(1);
});

it('can set a buffer value', async function (): Promise<void> {
if (!process.env.TEST_REDIS_URL) {
this.skip();
}

const cache = new RedisCache(process.env.TEST_REDIS_URL as string);
await cache.isReady();

// Just to be sure that the cache is really empty...
await cache.clear();

const wasSet = await cache.setValue('key', { '🍌': '🥔' });
expect(wasSet).to.be.true;

const value = await cache.getValue('key');
expect(value).to.deep.equal({ '🍌': '🥔' });

expect(await cache.itemCount()).to.equal(1);
});

it('can get a JSON value', async function (): Promise<void> {
if (!process.env.TEST_REDIS_URL) {
this.skip();
}

const cache = new RedisCache(process.env.TEST_REDIS_URL as string);
await cache.isReady();

// Just to be sure that the cache is really empty...
await cache.clear();

// Manual serialization here to avoid the automatic serialization of the setValue method.
const wasSet = await cache.setValue('key', `${RedisCache.JSON_PREFIX}${JSON.stringify({ '🍌': '🥔' })}`);
expect(wasSet).to.be.true;

const value = await cache.getValue('key');
expect(value).to.deep.equal({ '🍌': '🥔' });

expect(await cache.itemCount()).to.equal(1);
});
});

describe('itemCount', async () => {
Expand Down

0 comments on commit 0f52aab

Please sign in to comment.