2023-07-25 02:56:38 -07:00
|
|
|
import Container from 'typedi';
|
|
|
|
import { CacheService } from '@/services/cache.service';
|
|
|
|
import type { MemoryCache } from 'cache-manager';
|
2023-08-02 03:51:25 -07:00
|
|
|
import type { RedisCache } from 'cache-manager-ioredis-yet';
|
2023-07-25 02:56:38 -07:00
|
|
|
import config from '@/config';
|
|
|
|
|
|
|
|
const cacheService = Container.get(CacheService);
|
|
|
|
|
|
|
|
function setDefaultConfig() {
|
|
|
|
config.set('executions.mode', 'regular');
|
2023-08-02 03:51:25 -07:00
|
|
|
config.set('cache.enabled', true);
|
|
|
|
config.set('cache.backend', 'memory');
|
2023-07-25 02:56:38 -07:00
|
|
|
config.set('cache.memory.maxSize', 1 * 1024 * 1024);
|
|
|
|
}
|
|
|
|
|
2023-08-02 03:51:25 -07:00
|
|
|
interface TestObject {
|
|
|
|
test: string;
|
|
|
|
test2: number;
|
|
|
|
test3?: TestObject & { test4: TestObject };
|
|
|
|
}
|
|
|
|
|
|
|
|
const testObject: TestObject = {
|
|
|
|
test: 'test',
|
|
|
|
test2: 123,
|
|
|
|
test3: {
|
|
|
|
test: 'test3',
|
|
|
|
test2: 123,
|
|
|
|
test4: {
|
|
|
|
test: 'test4',
|
|
|
|
test2: 123,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-07-25 02:56:38 -07:00
|
|
|
describe('cacheService', () => {
|
2023-08-02 03:51:25 -07:00
|
|
|
beforeAll(async () => {
|
|
|
|
jest.mock('ioredis', () => {
|
|
|
|
const Redis = require('ioredis-mock');
|
|
|
|
if (typeof Redis === 'object') {
|
|
|
|
// the first mock is an ioredis shim because ioredis-mock depends on it
|
|
|
|
// https://github.com/stipsan/ioredis-mock/blob/master/src/index.js#L101-L111
|
|
|
|
return {
|
|
|
|
Command: { _transformer: { argument: {}, reply: {} } },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// second mock for our code
|
|
|
|
return function (...args: any) {
|
|
|
|
return new Redis(args);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-25 02:56:38 -07:00
|
|
|
beforeEach(async () => {
|
|
|
|
setDefaultConfig();
|
|
|
|
await Container.get(CacheService).destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create a memory cache by default', async () => {
|
|
|
|
await cacheService.init();
|
|
|
|
await expect(cacheService.getCache()).resolves.toBeDefined();
|
|
|
|
const candidate = (await cacheService.getCache()) as MemoryCache;
|
|
|
|
// type guard to check that a MemoryCache is returned and not a RedisCache (which does not have a size property)
|
|
|
|
expect(candidate.store.size).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should cache and retrieve a value', async () => {
|
|
|
|
await cacheService.init();
|
|
|
|
await expect(cacheService.getCache()).resolves.toBeDefined();
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.set('testString', 'test');
|
|
|
|
await cacheService.set('testNumber1', 123);
|
2023-07-25 02:56:38 -07:00
|
|
|
|
2023-08-02 03:51:25 -07:00
|
|
|
await expect(cacheService.get('testString')).resolves.toBe('test');
|
|
|
|
expect(typeof (await cacheService.get('testString'))).toBe('string');
|
|
|
|
await expect(cacheService.get('testNumber1')).resolves.toBe(123);
|
|
|
|
expect(typeof (await cacheService.get('testNumber1'))).toBe('number');
|
2023-07-25 02:56:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should honour ttl values', async () => {
|
2023-09-27 03:03:15 -07:00
|
|
|
await cacheService.set('testString', 'test', 10);
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.set('testNumber1', 123, 1000);
|
2023-07-25 02:56:38 -07:00
|
|
|
|
|
|
|
const store = (await cacheService.getCache())?.store;
|
|
|
|
|
|
|
|
expect(store).toBeDefined();
|
|
|
|
|
|
|
|
await expect(store!.ttl('testString')).resolves.toBeLessThanOrEqual(100);
|
2023-08-02 03:51:25 -07:00
|
|
|
await expect(store!.ttl('testNumber1')).resolves.toBeLessThanOrEqual(1000);
|
2023-07-25 02:56:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should set and remove values', async () => {
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.set('testString', 'test');
|
|
|
|
await expect(cacheService.get('testString')).resolves.toBe('test');
|
2023-07-25 02:56:38 -07:00
|
|
|
await cacheService.delete('testString');
|
2023-08-02 03:51:25 -07:00
|
|
|
await expect(cacheService.get('testString')).resolves.toBeUndefined();
|
2023-07-25 02:56:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should calculate maxSize', async () => {
|
|
|
|
config.set('cache.memory.maxSize', 16);
|
|
|
|
await cacheService.destroy();
|
|
|
|
|
|
|
|
// 16 bytes because stringify wraps the string in quotes, so 2 bytes for the quotes
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.set('testString', 'withoutUnicode');
|
|
|
|
await expect(cacheService.get('testString')).resolves.toBe('withoutUnicode');
|
2023-07-25 02:56:38 -07:00
|
|
|
|
|
|
|
await cacheService.destroy();
|
|
|
|
|
|
|
|
// should not fit!
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.set('testString', 'withUnicodeԱԲԳ');
|
|
|
|
await expect(cacheService.get('testString')).resolves.toBeUndefined();
|
2023-07-25 02:56:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should set and get complex objects', async () => {
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.set('testObject', testObject);
|
|
|
|
await expect(cacheService.get('testObject')).resolves.toMatchObject(testObject);
|
2023-07-25 02:56:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should set and get multiple values', async () => {
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.destroy();
|
|
|
|
expect(cacheService.isRedisCache()).toBe(false);
|
2023-07-25 02:56:38 -07:00
|
|
|
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.setMany([
|
2023-07-25 02:56:38 -07:00
|
|
|
['testString', 'test'],
|
|
|
|
['testString2', 'test2'],
|
|
|
|
]);
|
2023-08-02 03:51:25 -07:00
|
|
|
await cacheService.setMany([
|
|
|
|
['testNumber1', 123],
|
2023-07-25 02:56:38 -07:00
|
|
|
['testNumber2', 456],
|
|
|
|
]);
|
2023-08-02 03:51:25 -07:00
|
|
|
await expect(cacheService.getMany(['testString', 'testString2'])).resolves.toStrictEqual([
|
|
|
|
'test',
|
|
|
|
'test2',
|
|
|
|
]);
|
|
|
|
await expect(cacheService.getMany(['testNumber1', 'testNumber2'])).resolves.toStrictEqual([
|
|
|
|
123, 456,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create a redis in queue mode', async () => {
|
|
|
|
config.set('cache.backend', 'auto');
|
|
|
|
config.set('executions.mode', 'queue');
|
|
|
|
await cacheService.destroy();
|
|
|
|
await cacheService.init();
|
|
|
|
|
|
|
|
const cache = await cacheService.getCache();
|
|
|
|
await expect(cacheService.getCache()).resolves.toBeDefined();
|
|
|
|
const candidate = (await cacheService.getCache()) as RedisCache;
|
|
|
|
expect(candidate.store.client).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create a redis cache if asked', async () => {
|
|
|
|
config.set('cache.backend', 'redis');
|
|
|
|
config.set('executions.mode', 'queue');
|
|
|
|
await cacheService.destroy();
|
|
|
|
await cacheService.init();
|
|
|
|
|
|
|
|
const cache = await cacheService.getCache();
|
|
|
|
await expect(cacheService.getCache()).resolves.toBeDefined();
|
|
|
|
const candidate = (await cacheService.getCache()) as RedisCache;
|
|
|
|
expect(candidate.store.client).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should get/set/delete redis cache', async () => {
|
|
|
|
config.set('cache.backend', 'redis');
|
|
|
|
config.set('executions.mode', 'queue');
|
|
|
|
await cacheService.destroy();
|
|
|
|
await cacheService.init();
|
|
|
|
|
|
|
|
await cacheService.set('testObject', testObject);
|
|
|
|
await expect(cacheService.get('testObject')).resolves.toMatchObject(testObject);
|
|
|
|
await cacheService.delete('testObject');
|
|
|
|
await expect(cacheService.get('testObject')).resolves.toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE: mset and mget are not supported by ioredis-mock
|
|
|
|
// test('should set and get multiple values with redis', async () => {
|
|
|
|
// });
|
|
|
|
|
|
|
|
test('should return fallback value if key is not set', async () => {
|
|
|
|
await cacheService.reset();
|
|
|
|
await expect(cacheService.get('testString')).resolves.toBeUndefined();
|
|
|
|
await expect(
|
|
|
|
cacheService.get('testString', {
|
|
|
|
fallbackValue: 'fallback',
|
|
|
|
}),
|
|
|
|
).resolves.toBe('fallback');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should call refreshFunction if key is not set', async () => {
|
|
|
|
await cacheService.reset();
|
|
|
|
await expect(cacheService.get('testString')).resolves.toBeUndefined();
|
|
|
|
await expect(
|
|
|
|
cacheService.get('testString', {
|
|
|
|
refreshFunction: async () => 'refreshed',
|
|
|
|
fallbackValue: 'this should not be returned',
|
|
|
|
}),
|
|
|
|
).resolves.toBe('refreshed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should transparently handle disabled cache', async () => {
|
|
|
|
await cacheService.disable();
|
|
|
|
await expect(cacheService.get('testString')).resolves.toBeUndefined();
|
|
|
|
await cacheService.set('testString', 'whatever');
|
|
|
|
await expect(cacheService.get('testString')).resolves.toBeUndefined();
|
2023-07-25 02:56:38 -07:00
|
|
|
await expect(
|
2023-08-02 03:51:25 -07:00
|
|
|
cacheService.get('testString', {
|
|
|
|
fallbackValue: 'fallback',
|
|
|
|
}),
|
|
|
|
).resolves.toBe('fallback');
|
2023-07-25 02:56:38 -07:00
|
|
|
await expect(
|
2023-08-02 03:51:25 -07:00
|
|
|
cacheService.get('testString', {
|
|
|
|
refreshFunction: async () => 'refreshed',
|
|
|
|
fallbackValue: 'this should not be returned',
|
|
|
|
}),
|
|
|
|
).resolves.toBe('refreshed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should set and get partial results', async () => {
|
|
|
|
await cacheService.setMany([
|
|
|
|
['testNumber1', 123],
|
|
|
|
['testNumber2', 456],
|
|
|
|
]);
|
|
|
|
await expect(cacheService.getMany(['testNumber1', 'testNumber2'])).resolves.toStrictEqual([
|
|
|
|
123, 456,
|
|
|
|
]);
|
|
|
|
await expect(cacheService.getMany(['testNumber3', 'testNumber2'])).resolves.toStrictEqual([
|
|
|
|
undefined,
|
|
|
|
456,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should getMany and fix partial results and set single key', async () => {
|
|
|
|
await cacheService.setMany([
|
|
|
|
['testNumber1', 123],
|
|
|
|
['testNumber2', 456],
|
|
|
|
]);
|
|
|
|
await expect(
|
|
|
|
cacheService.getMany(['testNumber1', 'testNumber2', 'testNumber3']),
|
|
|
|
).resolves.toStrictEqual([123, 456, undefined]);
|
|
|
|
await expect(cacheService.get('testNumber3')).resolves.toBeUndefined();
|
|
|
|
await expect(
|
|
|
|
cacheService.getMany(['testNumber1', 'testNumber2', 'testNumber3'], {
|
|
|
|
async refreshFunctionEach(key) {
|
|
|
|
return key === 'testNumber3' ? 789 : undefined;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).resolves.toStrictEqual([123, 456, 789]);
|
|
|
|
await expect(cacheService.get('testNumber3')).resolves.toBe(789);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should getMany and set all keys', async () => {
|
|
|
|
await cacheService.setMany([
|
|
|
|
['testNumber1', 123],
|
|
|
|
['testNumber2', 456],
|
|
|
|
]);
|
|
|
|
await expect(
|
|
|
|
cacheService.getMany(['testNumber1', 'testNumber2', 'testNumber3']),
|
|
|
|
).resolves.toStrictEqual([123, 456, undefined]);
|
|
|
|
await expect(cacheService.get('testNumber3')).resolves.toBeUndefined();
|
|
|
|
await expect(
|
|
|
|
cacheService.getMany(['testNumber1', 'testNumber2', 'testNumber3'], {
|
|
|
|
async refreshFunctionMany(keys) {
|
|
|
|
return [111, 222, 333];
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).resolves.toStrictEqual([111, 222, 333]);
|
|
|
|
await expect(cacheService.get('testNumber1')).resolves.toBe(111);
|
|
|
|
await expect(cacheService.get('testNumber2')).resolves.toBe(222);
|
|
|
|
await expect(cacheService.get('testNumber3')).resolves.toBe(333);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should set and get multiple values with fallbackValue', async () => {
|
|
|
|
await cacheService.disable();
|
|
|
|
await cacheService.setMany([
|
|
|
|
['testNumber1', 123],
|
|
|
|
['testNumber2', 456],
|
|
|
|
]);
|
|
|
|
await expect(cacheService.getMany(['testNumber1', 'testNumber2'])).resolves.toStrictEqual([
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
]);
|
|
|
|
await expect(
|
|
|
|
cacheService.getMany(['testNumber1', 'testNumber2'], {
|
|
|
|
fallbackValues: [123, 456],
|
|
|
|
}),
|
|
|
|
).resolves.toStrictEqual([123, 456]);
|
|
|
|
await expect(
|
|
|
|
cacheService.getMany(['testNumber1', 'testNumber2'], {
|
|
|
|
refreshFunctionMany: async () => [123, 456],
|
|
|
|
fallbackValues: [0, 1],
|
|
|
|
}),
|
2023-07-25 02:56:38 -07:00
|
|
|
).resolves.toStrictEqual([123, 456]);
|
|
|
|
});
|
2023-08-02 03:51:25 -07:00
|
|
|
|
|
|
|
test('should deal with unicode keys', async () => {
|
|
|
|
const key = '? > ":< ! withUnicodeԱԲԳ';
|
|
|
|
await cacheService.set(key, 'test');
|
|
|
|
await expect(cacheService.get(key)).resolves.toBe('test');
|
|
|
|
await cacheService.delete(key);
|
|
|
|
await expect(cacheService.get(key)).resolves.toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should deal with unicode keys in redis', async () => {
|
|
|
|
config.set('cache.backend', 'redis');
|
|
|
|
config.set('executions.mode', 'queue');
|
|
|
|
await cacheService.destroy();
|
|
|
|
await cacheService.init();
|
|
|
|
const key = '? > ":< ! withUnicodeԱԲԳ';
|
|
|
|
|
|
|
|
expect(((await cacheService.getCache()) as RedisCache).store.client).toBeDefined();
|
|
|
|
|
|
|
|
await cacheService.set(key, 'test');
|
|
|
|
await expect(cacheService.get(key)).resolves.toBe('test');
|
|
|
|
await cacheService.delete(key);
|
|
|
|
await expect(cacheService.get(key)).resolves.toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not cache null or undefined values', async () => {
|
|
|
|
await cacheService.set('nullValue', null);
|
|
|
|
await cacheService.set('undefValue', undefined);
|
|
|
|
await cacheService.set('normalValue', 'test');
|
|
|
|
|
|
|
|
await expect(cacheService.get('normalValue')).resolves.toBe('test');
|
|
|
|
await expect(cacheService.get('undefValue')).resolves.toBeUndefined();
|
|
|
|
await expect(cacheService.get('nullValue')).resolves.toBeUndefined();
|
|
|
|
});
|
2023-08-04 10:44:41 -07:00
|
|
|
|
|
|
|
test('should handle setting empty keys', async () => {
|
|
|
|
await cacheService.set('', null);
|
|
|
|
await expect(cacheService.get('')).resolves.toBeUndefined();
|
|
|
|
await cacheService.setMany([
|
|
|
|
['', 'something'],
|
|
|
|
['', 'something'],
|
|
|
|
]);
|
|
|
|
await expect(cacheService.getMany([''])).resolves.toStrictEqual([undefined]);
|
|
|
|
await cacheService.setMany([]);
|
|
|
|
await expect(cacheService.getMany([])).resolves.toStrictEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should handle setting empty keys (redis)', async () => {
|
|
|
|
config.set('cache.backend', 'redis');
|
|
|
|
config.set('executions.mode', 'queue');
|
|
|
|
await cacheService.destroy();
|
|
|
|
await cacheService.init();
|
|
|
|
|
|
|
|
await cacheService.set('', null);
|
|
|
|
await expect(cacheService.get('')).resolves.toBeUndefined();
|
|
|
|
await cacheService.setMany([
|
|
|
|
['', 'something'],
|
|
|
|
['', 'something'],
|
|
|
|
]);
|
|
|
|
await expect(cacheService.getMany([''])).resolves.toStrictEqual([undefined]);
|
|
|
|
await cacheService.setMany([]);
|
|
|
|
await expect(cacheService.getMany([])).resolves.toStrictEqual([]);
|
|
|
|
});
|
2023-07-25 02:56:38 -07:00
|
|
|
});
|