Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 46f7cb6

Browse files
author
Gokul Chandran
committed
feat(Redis-Memory Storage): Caching module with redis and memory storage and supporting decorators
1 parent c4f2a43 commit 46f7cb6

57 files changed

Lines changed: 5009 additions & 5473 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { Cache, CacheClear, RedisStorage, ExpirationStrategy, MemoryStorage,
2+
LbServicesCacheComponent, CacheBindings, CacheRequest, CacheStrategyResolverProvider} from './lib/cache/src';

index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
module.exports=
2-
{
3-
Cache : require('./lib/cache'),
4-
Middleware: require('./lib/middleware'),
5-
Class: require('./lib/class')
6-
}
1+
module.exports = require('./dist');

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src';

lib/cache/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Himmet Avsar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

lib/cache/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[![Travis CI](https://img.shields.io/travis/havsar/node-ts-cache.svg)](https://travis-ci.org/havsar/node-ts-cache)
2+
[![David](https://img.shields.io/david/havsar/node-ts-cache.svg)](https://david-dm.org/havsar/node-ts-cache)
3+
[![npm](https://img.shields.io/npm/v/node-ts-cache.svg)](https://www.npmjs.org/package/node-ts-cache)
4+
[![The MIT License](https://img.shields.io/npm/l/node-ts-cache.svg)](http://opensource.org/licenses/MIT)
5+
6+
[![NPM](https://nodei.co/npm/node-ts-cache.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/node-ts-cache/)
7+
8+
# labshare/services-cache
9+
Simple and extensible caching module with redis and memory storage and supporting decorators.
10+
11+
<!-- TOC depthTo:2 -->
12+
13+
- [node-ts-cache](#labshare/services-cache )
14+
- [Install](#install)
15+
- [Usage](#usage)
16+
- [With decorator](#with-decorator)
17+
- [Directly](#directly)
18+
- [Strategies](#strategies)
19+
- [ExpirationStrategy](#expirationstrategy)
20+
- [Storages](#storages)
21+
- [Test](#test)
22+
23+
<!-- /TOC -->
24+
25+
# Install
26+
```bash
27+
npm install --save @labshare/services-cache
28+
```
29+
30+
# Usage
31+
## With decorator
32+
Caches function response using the given options. Works with different strategies and storages. Uses all arguments to build an unique key.
33+
34+
`@Cache(options)`
35+
- `options`: Options passed to the strategy for this particular method
36+
37+
*Note: @Cache always converts the method response to a promise because caching might be async.*
38+
39+
```ts
40+
import { Cache, ExpirationStrategy, MemoryStorage } from "@labshare/services-cache";
41+
42+
class MyService {
43+
44+
@Cache(myStrategy, { ttl: 60 })
45+
public async getUsers(): Promise<string[]> {
46+
return ["John", "Doe"];
47+
}
48+
}
49+
```
50+
51+
## Directly
52+
53+
```ts
54+
import { ExpirationStrategy, MemoryStorage } from "@labshare/services-cache";
55+
56+
const memoryCache = new ExpirationStrategy(new MemoryStorage());
57+
58+
class MyService {
59+
60+
public async getUsers(): Promise<string[]> {
61+
const cachedUsers = await memoryCache.getItem<string[]>("users");
62+
if (cachedUsers) {
63+
return cachedUsers;
64+
}
65+
66+
const newUsers = ["John", "Doe"];
67+
await memoryCache.setItem("users", newUsers, { ttl: 60 });
68+
69+
return newUsers;
70+
}
71+
}
72+
```
73+
74+
# Strategies
75+
## ExpirationStrategy
76+
Cached items expire after a given amount of time.
77+
78+
- `ttl`: *(Default: 60)* Number of seconds to expire the cachte item
79+
- `isLazy`: *(Default: true)* If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given *ttl*.
80+
- `isCachedForver`: *(Default: false)* If true, cache entry has no expiration.
81+
- `refreshCache,`: boolean.
82+
- `noop?`: boolean; // Allows for consuming libraries to conditionally disable caching. Set this to true to disable caching for some reason.
83+
84+
85+
# Storages
86+
87+
*Note: For specific storages, client libraries must be installed:*
88+
89+
| Storage | Needed client library |
90+
|--------------|:---------------------:|
91+
| RedisStorage | `npm install redis` |
92+
93+
#### MemoryStorage()
94+
#### FsJsonStorage(`fileName: string`)
95+
#### RedisStorage(`clientOpts:` [RedisClientOptions](https://github.com/NodeRedis/node_redis#options-object-properties))
96+
97+
98+
# Test
99+
```bash
100+
npm test
101+
```

lib/cache/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// export { Cache, CacheClear, RedisStorage, ExpirationStrategy, MemoryStorage,
2+
// LbServicesCacheComponent, CacheBindings, CacheRequest, CacheStrategyResolverProvider} from './src';

0 commit comments

Comments
 (0)