DefinitelyTyped/types/cache-manager/cache-manager-tests.ts
Nathan Shively-Sanders e7e259f3fe
Make cache-manager tests strictNullChecks safe (#42686)
The new code in cache-manager wasn't strictNullChecks safe, which caused
problems because a community member enabled strictNullChecks in most
packages during the review period for that code.

This change just fixes the tests, although the optional methods on Store
are a bit suspicious. From skimming cache-manager's documentation, I
couldn't figure out if having the methods be optional was accurate.
2020-02-27 09:50:39 -08:00

75 lines
1.5 KiB
TypeScript

import * as cacheManager from 'cache-manager'
const memoryCache: cacheManager.Cache = cacheManager.caching({ store: 'memory', max: 100, ttl: 10/*seconds*/ });
const ttl = 5;
memoryCache.set('foo', 'bar', { ttl: ttl }, (err) => {
if (err) {
throw err;
}
memoryCache.get('foo', (err, result) => {
// console.log(result);
memoryCache.del('foo', (err) => {
});
});
});
function getUser(id: number, cb: Function) {
cb(null, { id: id, name: 'Bob' });
}
const userId = 123;
const key = 'user_' + userId;
const key2 = 'user_' + userId + '4';
// Note: ttl is optional in wrap()
memoryCache.wrap<{ id: number, name: string }>(key, (cb: any) => {
getUser(userId, cb);
}, { ttl: ttl }, (err: any, user: { id: number, name: string }) => {
//console.log(user);
// Second time fetches user from memoryCache
memoryCache.wrap<{ id: number, name: string }>(key, key2, (cb: any) => {
getUser(userId, cb);
}, (err: any, user: { id: number, name: string }) => {
//console.log(user);
});
});
if (memoryCache.store.keys) {
memoryCache.store.keys().then((result) => {
//console.log(result);
});
}
const multiCache = cacheManager.multiCaching([memoryCache]);
multiCache.set('foo', 'bar', { ttl: ttl }, (err) => {
if (err) {
throw err;
}
multiCache.get('foo', (err, result) => {
// console.log(result);
multiCache.del('foo', (err) => {
});
});
});