Skip to content

Commit

Permalink
feat!: make method interfaces consistent with decorators
Browse files Browse the repository at this point in the history
BREAKING CHANGE: make method interfaces consistent with decorators
  • Loading branch information
SaurusXI committed Feb 4, 2023
1 parent 2ed4203 commit f844e75
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
21 changes: 12 additions & 9 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ export class SugarCache {

/**
* Reads an element stored at a key
* @param key Cache key for the element you're trying to fetch
* @param keys Cache keys for the element you're trying to fetch
* @returns The object stored at the given key; `null` if no such object is found
*/
public get = async (key: string) => {
public get = async (keys: string[]) => {
const key = keys.join(':');
const cacheKey = this.transformIntoCacheKey(key);

const result = await this.redisTransaction()
Expand Down Expand Up @@ -109,10 +110,11 @@ export class SugarCache {

/**
* Upserts a value in the cache at the specified key
* @param key Cache key at which the value has to be stored
* @param keys Cache keys at which the value has to be stored
* @param value The value to be stored at the key
*/
public set = async (key: string, value: any, ttl: TTL) => {
public set = async (keys: string[], value: any, ttl: TTL) => {
const key = keys.join(':');
const cacheKey = this.transformIntoCacheKey(key);

const result = await this.redisTransaction()
Expand All @@ -130,9 +132,10 @@ export class SugarCache {

/**
* Deletes a value from the cache
* @param key Key of value to be removed
* @param keys Key of value to be removed
*/
public del = async (key: string) => {
public del = async (keys: string[]) => {
const key = keys.join(':');
const cacheKey = this.transformIntoCacheKey(key);

const result = await this.redisTransaction()
Expand Down Expand Up @@ -198,14 +201,14 @@ export class SugarCache {
const cacheKey = cacheKeyArgs.join(':');

cacheInstance.logger.debug(`[SugarCache:${cacheInstance.namespace}] Checking key ${cacheKey} in cache`);
const cachedResult = await cacheInstance.get(cacheKey);
const cachedResult = await cacheInstance.get([cacheKey]);
if (cachedResult !== null) {
cacheInstance.logger.debug(`[SugarCache:${cacheInstance.namespace}] result for key ${cacheKey} found in cache. Returning...`);
return cachedResult
};

const result = await currentFn.apply(this, arguments);
await cacheInstance.set(cacheKey, result, ttl)
await cacheInstance.set([cacheKey], result, ttl)
.catch((err) => { throw new Error(`[SugarCache:${cacheInstance.namespace}] Unable to set value to cache - ${err}`) });

cacheInstance.logger.debug(`[SugarCache:${cacheInstance.namespace}] result for key ${cacheKey} set in cache`)
Expand Down Expand Up @@ -236,7 +239,7 @@ export class SugarCache {
const cacheKeyArgs = keys.map(k => namedArguments[k]);
const cacheKey = cacheKeyArgs.join(':');

await cacheInstance.del(cacheKey)
await cacheInstance.del([cacheKey])
.catch((err) => { throw new Error(`[SugarCache:${cacheInstance.namespace}] Unable to delete value from cache - ${err}`)});

cacheInstance.logger.debug(`[SugarCache:${cacheInstance.namespace}] removed key ${cacheKey} from cache`)
Expand Down
30 changes: 15 additions & 15 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ describe('Functional tests', () => {
const mockCacheVals = [...Array(totTestKeys).keys()].map(x => ({ key: `foo-${x}`, val: `bar-${x}` }))

it('write', async () => {
await cacheBasic.set(mockKey, mockVal, ttl);
await cacheBasic.set([mockKey], mockVal, ttl);
})

it('read', async () => {
const cachedVal = await cacheBasic.get(mockKey);
const cachedVal = await cacheBasic.get([mockKey]);
expect(cachedVal).toStrictEqual(mockVal);
})

it('delete', async () => {
await cacheBasic.del(mockKey);
const cachedVal = await cacheBasic.get(mockKey);
await cacheBasic.del([mockKey]);
const cachedVal = await cacheBasic.get([mockKey]);
expect(cachedVal).toBeNull();
})

it('clear cache', async () => {
for (const mockObj of mockCacheVals) {
await cacheBasic.set(mockObj.key, mockObj.val, ttl);
await cacheBasic.set([mockObj.key], mockObj.val, ttl);
}
await cacheBasic.clear();
for (const mockObj of mockCacheVals) {
// Since the first element was inserted first, expect that to be omitted
const cachedVal = await cacheBasic.get(mockObj.key);
const cachedVal = await cacheBasic.get([mockObj.key]);
expect(cachedVal).toBeNull();
}
})

it('TTL based eviction', async () => {
await cacheBasic.set(mockKey, mockVal, ttl);
await cacheBasic.set([mockKey], mockVal, ttl);
await new Promise((resolve) => {
setTimeout(resolve, ttl * 1.1)
});
const cachedVal = await cacheBasic.get(mockKey);
const cachedVal = await cacheBasic.get([mockKey]);
expect(cachedVal).toBeNull();
}, 2 * ttl)

Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Functional tests', () => {
it('invalidate', async () => {
const response = await controller.delete(resourceId, resourceCategory);
expect(response).toStrictEqual({ res: resourceCategory + resourceId });
const cachedValue = await cacheBasic.get(`${resourceCategory}:${resourceId}`);
const cachedValue = await cacheBasic.get([`${resourceCategory}:${resourceId}`]);
expect(cachedValue).toBeNull();
})

Expand All @@ -129,29 +129,29 @@ describe('Functional tests', () => {
const mockCacheVals = [...Array(totTestKeys).keys()].map(x => ({ key: `foo-${x}`, val: `bar-${x}` }))

it('write', async () => {
await cacheBasic.set(mockKey, mockVal, ttl);
await cacheBasic.set([mockKey], mockVal, ttl);
})

it('read', async () => {
const cachedVal = await cacheBasic.get(mockKey);
const cachedVal = await cacheBasic.get([mockKey]);
expect(cachedVal).toStrictEqual(mockVal);
})

it('delete', async () => {
await cacheBasic.del(mockKey);
await cacheBasic.del([mockKey]);

const cachedVal = await cacheBasic.get(mockKey);
const cachedVal = await cacheBasic.get([mockKey]);
expect(cachedVal).toBeNull();
})

it('clear cache', async () => {
for (const mockObj of mockCacheVals) {
await cacheBasic.set(mockObj.key, mockObj.val, ttl);
await cacheBasic.set([mockObj.key], mockObj.val, ttl);
}
await cacheBasic.clear();
for (const mockObj of mockCacheVals) {
// Since the first element was inserted first, expect that to be omitted
const cachedVal = await cacheBasic.get(mockObj.key);
const cachedVal = await cacheBasic.get([mockObj.key]);
expect(cachedVal).toBeNull();
}
})
Expand Down

0 comments on commit f844e75

Please sign in to comment.