Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@gabliam/cache): Add cache plugin for autoconfiguration of cache
- Loading branch information
Showing
13 changed files
with
550 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './redis-cache'; | ||
export { RedisCache as default, RedisCacheOptions } from './redis-cache'; |
261 changes: 261 additions & 0 deletions
261
packages/cache/__tests__/plugin/__snapshots__/integration.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
Oops, something went wrong.