This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Redis-Memory Storage): Caching module with redis and memory stor…
…age and supporting decorators
- Loading branch information
Showing
57 changed files
with
5,009 additions
and
5,473 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Cache, CacheClear, RedisStorage, ExpirationStrategy, MemoryStorage, | ||
LbServicesCacheComponent, CacheBindings, CacheRequest, CacheStrategyResolverProvider} from './lib/cache/src'; |
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,6 +1 @@ | ||
module.exports= | ||
{ | ||
Cache : require('./lib/cache'), | ||
Middleware: require('./lib/middleware'), | ||
Class: require('./lib/class') | ||
} | ||
module.exports = require('./dist'); |
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 @@ | ||
export * from './src'; |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Himmet Avsar | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,101 @@ | ||
[![Travis CI](https://img.shields.io/travis/havsar/node-ts-cache.svg)](https://travis-ci.org/havsar/node-ts-cache) | ||
[![David](https://img.shields.io/david/havsar/node-ts-cache.svg)](https://david-dm.org/havsar/node-ts-cache) | ||
[![npm](https://img.shields.io/npm/v/node-ts-cache.svg)](https://www.npmjs.org/package/node-ts-cache) | ||
[![The MIT License](https://img.shields.io/npm/l/node-ts-cache.svg)](http://opensource.org/licenses/MIT) | ||
|
||
[![NPM](https://nodei.co/npm/node-ts-cache.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/node-ts-cache/) | ||
|
||
# labshare/services-cache | ||
Simple and extensible caching module with redis and memory storage and supporting decorators. | ||
|
||
<!-- TOC depthTo:2 --> | ||
|
||
- [node-ts-cache](#labshare/services-cache ) | ||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [With decorator](#with-decorator) | ||
- [Directly](#directly) | ||
- [Strategies](#strategies) | ||
- [ExpirationStrategy](#expirationstrategy) | ||
- [Storages](#storages) | ||
- [Test](#test) | ||
|
||
<!-- /TOC --> | ||
|
||
# Install | ||
```bash | ||
npm install --save @labshare/services-cache | ||
``` | ||
|
||
# Usage | ||
## With decorator | ||
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 strategy for this particular method | ||
|
||
*Note: @Cache always converts the method response to a promise because caching might be async.* | ||
|
||
```ts | ||
import { Cache, ExpirationStrategy, MemoryStorage } from "@labshare/services-cache"; | ||
|
||
class MyService { | ||
|
||
@Cache(myStrategy, { ttl: 60 }) | ||
public async getUsers(): Promise<string[]> { | ||
return ["John", "Doe"]; | ||
} | ||
} | ||
``` | ||
|
||
## Directly | ||
|
||
```ts | ||
import { ExpirationStrategy, MemoryStorage } from "@labshare/services-cache"; | ||
|
||
const memoryCache = new ExpirationStrategy(new MemoryStorage()); | ||
|
||
class MyService { | ||
|
||
public async getUsers(): Promise<string[]> { | ||
const cachedUsers = await memoryCache.getItem<string[]>("users"); | ||
if (cachedUsers) { | ||
return cachedUsers; | ||
} | ||
|
||
const newUsers = ["John", "Doe"]; | ||
await memoryCache.setItem("users", newUsers, { ttl: 60 }); | ||
|
||
return newUsers; | ||
} | ||
} | ||
``` | ||
|
||
# Strategies | ||
## ExpirationStrategy | ||
Cached items expire after a given amount of time. | ||
|
||
- `ttl`: *(Default: 60)* Number of seconds to expire the cachte item | ||
- `isLazy`: *(Default: true)* If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given *ttl*. | ||
- `isCachedForver`: *(Default: false)* If true, cache entry has no expiration. | ||
- `refreshCache,`: boolean. | ||
- `noop?`: boolean; // Allows for consuming libraries to conditionally disable caching. Set this to true to disable caching for some reason. | ||
|
||
|
||
# Storages | ||
|
||
*Note: For specific storages, client libraries must be installed:* | ||
|
||
| Storage | Needed client library | | ||
|--------------|:---------------------:| | ||
| RedisStorage | `npm install redis` | | ||
|
||
#### MemoryStorage() | ||
#### FsJsonStorage(`fileName: string`) | ||
#### RedisStorage(`clientOpts:` [RedisClientOptions](https://github.com/NodeRedis/node_redis#options-object-properties)) | ||
|
||
|
||
# Test | ||
```bash | ||
npm test | ||
``` |
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,2 @@ | ||
// export { Cache, CacheClear, RedisStorage, ExpirationStrategy, MemoryStorage, | ||
// LbServicesCacheComponent, CacheBindings, CacheRequest, CacheStrategyResolverProvider} from './src'; |
Oops, something went wrong.