Skip to content

Commit

Permalink
feat(@gabliam/cache): Add cache plugin for autoconfiguration of cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eyolas committed May 28, 2018
1 parent d265974 commit 659f97f
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/cache-redis/__tests__/redis-cache.test.ts
@@ -1,4 +1,4 @@
import { RedisCache } from '../src/index';
import RedisCache from '../src/index';

let cache: RedisCache;
let cache2: RedisCache;
Expand Down
2 changes: 1 addition & 1 deletion packages/cache-redis/src/index.ts
@@ -1 +1 @@
export * from './redis-cache';
export { RedisCache as default, RedisCacheOptions } from './redis-cache';
261 changes: 261 additions & 0 deletions packages/cache/__tests__/plugin/__snapshots__/integration.test.ts.snap
@@ -0,0 +1,261 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`config test bad config bad cacheManager 1`] = `[CacheManagerPgkNotInstalledError: CacheManager "BadCacheManager" package has not been found installed. Try to install it: npm install BadCacheManager --save or yarn add BadCacheManager]`;

exports[`config test bad config bad defaultCache 1`] = `[CachePgkNotInstalledError: Cache "BadCache" package has not been found installed. Try to install it: npm install BadCache --save or yarn add BadCache]`;

exports[`config test with config 1`] = `
SimpleCacheManager {
"cacheMap": Map {},
"defaultCache": [Function],
"defaultOptionsCache": undefined,
"dynamic": true,
"startedCache": Map {},
}
`;

exports[`config test without config 1`] = `
SimpleCacheManager {
"cacheMap": Map {},
"defaultCache": [Function],
"defaultOptionsCache": undefined,
"dynamic": true,
"startedCache": Map {},
}
`;

exports[`integrations test cache 1`] = `"hi test test"`;

exports[`integrations test cache 2`] = `"hi test test"`;

exports[`integrations test cache 3`] = `
MemoryCache {
"name": "hi",
"options": undefined,
"store": LRUCache {
Symbol(max): Infinity,
Symbol(lengthCalculator): [Function],
Symbol(allowStale): false,
Symbol(maxAge): 0,
Symbol(dispose): undefined,
Symbol(noDisposeOnSet): false,
Symbol(cache): Map {
"testtest" => Node {
"list": Yallist {
"head": [Circular],
"length": 1,
"tail": [Circular],
},
"next": null,
"prev": null,
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
},
Symbol(lruList): Yallist {
"head": Node {
"list": [Circular],
"next": null,
"prev": null,
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
"length": 1,
"tail": Node {
"list": [Circular],
"next": null,
"prev": null,
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
},
Symbol(length): 1,
},
}
`;

exports[`integrations test cache 4`] = `"hi test2 test2"`;

exports[`integrations test cache 5`] = `
MemoryCache {
"name": "hi",
"options": undefined,
"store": LRUCache {
Symbol(max): Infinity,
Symbol(lengthCalculator): [Function],
Symbol(allowStale): false,
Symbol(maxAge): 0,
Symbol(dispose): undefined,
Symbol(noDisposeOnSet): false,
Symbol(cache): Map {
"testtest" => Node {
"list": Yallist {
"head": Node {
"list": [Circular],
"next": [Circular],
"prev": null,
"value": Entry {
"key": "test2test2",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test2 test2",
},
},
"length": 2,
"tail": [Circular],
},
"next": null,
"prev": Node {
"list": Yallist {
"head": [Circular],
"length": 2,
"tail": [Circular],
},
"next": [Circular],
"prev": null,
"value": Entry {
"key": "test2test2",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test2 test2",
},
},
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
"test2test2" => Node {
"list": Yallist {
"head": [Circular],
"length": 2,
"tail": Node {
"list": [Circular],
"next": null,
"prev": [Circular],
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
},
"next": Node {
"list": Yallist {
"head": [Circular],
"length": 2,
"tail": [Circular],
},
"next": null,
"prev": [Circular],
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
"prev": null,
"value": Entry {
"key": "test2test2",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test2 test2",
},
},
},
Symbol(lruList): Yallist {
"head": Node {
"list": [Circular],
"next": Node {
"list": [Circular],
"next": null,
"prev": [Circular],
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
"prev": null,
"value": Entry {
"key": "test2test2",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test2 test2",
},
},
"length": 2,
"tail": Node {
"list": [Circular],
"next": null,
"prev": Node {
"list": [Circular],
"next": [Circular],
"prev": null,
"value": Entry {
"key": "test2test2",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test2 test2",
},
},
"value": Entry {
"key": "testtest",
"length": 1,
"maxAge": 0,
"now": 0,
"value": "hi test test",
},
},
},
Symbol(length): 2,
},
}
`;

exports[`integrations with mapCache 1`] = `"hi test test"`;

exports[`integrations with mapCache 2`] = `"hi test test"`;

exports[`integrations with mapCache 3`] = `
NoOpCache {
"name": "hi",
}
`;

exports[`integrations with mapCache 4`] = `"hi test2 test2"`;

exports[`integrations with mapCache 5`] = `
NoOpCache {
"name": "hi",
}
`;
10 changes: 10 additions & 0 deletions packages/cache/__tests__/plugin/cache-plugin-test.ts
@@ -0,0 +1,10 @@
import { Gabliam } from '@gabliam/core';
import { GabliamTest } from '@gabliam/core/lib/testing';
import CachePlugin from '../../src/index';

export class CachePluginTest extends GabliamTest {
constructor() {
const gab = new Gabliam().addPlugin(CachePlugin);
super(gab);
}
}
108 changes: 108 additions & 0 deletions packages/cache/__tests__/plugin/integration.test.ts
@@ -0,0 +1,108 @@
import { CachePluginTest } from './cache-plugin-test';
import {
CACHE_MANAGER,
CacheManager,
CachePut,
SimpleCacheManager,
NoOpCache
} from '../../src/index';
import { Service } from '@gabliam/core';

let appTest: CachePluginTest;

beforeEach(async () => {
appTest = new CachePluginTest();
});

afterEach(async () => {
await appTest.destroy();
});

describe('config test', () => {
it('without config', async () => {
await appTest.build();
const cacheManager = appTest.gab.container.get<CacheManager>(CACHE_MANAGER);
expect(cacheManager).toMatchSnapshot();
});

it('with config', async () => {
appTest.addConf('application.cacheConfig', {
defaultCache: 'MemoryCache'
});
await appTest.build();
const cacheManager = appTest.gab.container.get<CacheManager>(CACHE_MANAGER);
expect(cacheManager).toMatchSnapshot();
});

describe('bad config', () => {
it('bad defaultCache', async () => {
appTest.addConf('application.cacheConfig', {
defaultCache: 'BadCache'
});
await expect(appTest.gab.buildAndStart()).rejects.toMatchSnapshot();
});

it('bad cacheManager', async () => {
appTest.addConf('application.cacheConfig', {
cacheManager: 'BadCacheManager'
});
await expect(appTest.gab.buildAndStart()).rejects.toMatchSnapshot();
});
});
});

describe('integrations', () => {
it('test cache', async () => {
@Service()
class TestService {
@CachePut('hi')
async hi(surname: string, name: string) {
return `hi ${surname} ${name}`;
}
}
appTest.addClass(TestService);
appTest.addConf('application.cacheConfig', {
defaultCache: 'MemoryCache'
});
await appTest.build();
const s = appTest.gab.container.get(TestService);
const cache = appTest.gab.container.get<CacheManager>(CACHE_MANAGER);
expect(await s.hi('test', 'test')).toMatchSnapshot();
expect(await s.hi('test', 'test')).toMatchSnapshot();
expect(await cache.getCache('hi')).toMatchSnapshot();
expect(await s.hi('test2', 'test2')).toMatchSnapshot();
expect(await cache.getCache('hi')).toMatchSnapshot();
});

it('with mapCache', async () => {
class CustomCacheManager extends SimpleCacheManager {}

@Service()
class TestService {
@CachePut('hi')
async hi(surname: string, name: string) {
return `hi ${surname} ${name}`;
}
}
appTest.addClass(TestService);
appTest.addConf('application.cacheConfig', {
cacheManager: CustomCacheManager,
cacheMap: {
test: {
cache: 'MemoryCache'
},
test2: {
cache: NoOpCache
}
}
});
await appTest.build();
const s = appTest.gab.container.get(TestService);
const cache = appTest.gab.container.get<CacheManager>(CACHE_MANAGER);
expect(await s.hi('test', 'test')).toMatchSnapshot();
expect(await s.hi('test', 'test')).toMatchSnapshot();
expect(await cache.getCache('hi')).toMatchSnapshot();
expect(await s.hi('test2', 'test2')).toMatchSnapshot();
expect(await cache.getCache('hi')).toMatchSnapshot();
});
});

0 comments on commit 659f97f

Please sign in to comment.