Services-cache is a Redis cache plugin for LabShare Services.
- Node.js v6+
- Redis (if Redis is not installed locally you can use this docker file)
npm i @labshare/services-cache
*You might need to use sudo (UNIX).
Simple and extensible caching module with redis and memory storage and supporting decorators.
npm install --save @labshare/services-cache
Create a Config folder in the root and add config file under config/default.json
```json
"services":
{
"cache": {
"type": "redis", // specifies if memory ( default) or redis storage is being selected.
"redis": {
"host": "ec2-52-90-18-4.compute-2.amazonaws.com", // eg : redis server
"port": 6379
},
"memory":{
"isGlobalCache" : true
}
}
}
```
import {LabShareCache} from "@labshare/services-cache";
const config = require('config');
...
...
// if redis
const myRedisCache = new LabShareCache(config.get('services.cache'));
// if memory
// change type to memory
const memoryCache = new LabShareCache(config.get('services.cache'));
class MyService {
/**
* GET one hero by id
*/
public async getOne(req: Request, res: Response, next: NextFunction) {
let query = parseInt(req.params.id);
const cachekey = "cacheKey:"+query;
const cachedSearchResults = await myRedisCache.getItem<string>(cachekey);
if (cachedSearchResults) {
return cachedSearchResults;
}
let hero = Heroes.find(hero => hero.id === query);
await myRedisCache.setItem(cachekey, hero, { ttl: 120, isCachedForever: false });
if (hero) {
res.status(200)
.send({
message: 'Successfully',
status: res.status,
hero
});
}
else {
res.status(404)
.send({
message: 'No hero found with the given id.',
status: res.status
});
}
}
}
import {ServicesCache} from '@labshare/services-cache';
import {ServicesConfigComponent} from '@labshare/services-config';
// Other imports …
export class LbServicesExampleApplication
{
constructor(options: ApplicationConfig = {}){
// binding
this.bind(CacheBindings.CACHE_CONFIG).to({
"type": "redis",
"redis": {
"host": "redis", // eg : redis server ec2-52-90-18-4.compute-1.amazonaws.com
"port": 6379
}
});
this.component(ServicesCache); // Method implementation ...
}
}
...
...
Caches function response using the given options. Works with different strategies and storages. Uses all arguments to build an unique key.
@Cache(options)
options
: Options passed to the cache provider for this particular method
Note: @Cache always converts the method response to a promise because caching might be async.
import { Cache } from "@labshare/services-cache";
class MyService {
@Cache({ ttl: 60 })
public async getUsers(): Promise<string[]> {
return ["John", "Doe"];
}
}
import { LabShareCache } from "@labshare/services-cache";
class MyService {
constructor(
@inject(CacheBindings.CACHE)
private cache: LabShareCache,
) {}
public async getShipToUsers(): Promise<string[]> {
const cachedUsers = await cache.getItem<string[]>("users");
if (cachedUsers) {
return cachedUsers;
}
const newUsers = ["John", "Doe"];
await cache.setItem("users", newUsers, { ttl: 60 });
return newUsers;
}
}
Cached items expire after a given amount of time.
ttl
: (Default: 60) Number of seconds to expire the cachte itemisLazy
: (Default: true) If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given ttl.noop?
: boolean; // Allows for consuming libraries to conditionally disable caching. Set this to true to disable caching for some reason.cacheKey?
: stringrefreshCache
: boolean.
Storage | Configuration |
---|---|
Redis | RedisStorage({redis}: RedisClientOptions) |
memory | MemoryStorage({memory}: isGlobalCache =true creates a global instance for local memory, if false is a local storage per instance ) |
npm test
The cache's duration is in seconds, also maxTime is used if no duration is given.
MIT