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

Commit

Permalink
feat(Redis-Memory Storage): Caching module with redis and memory stor…
Browse files Browse the repository at this point in the history
…age and supporting decorators
  • Loading branch information
chandgi committed May 24, 2019
1 parent c4f2a43 commit 46f7cb6
Show file tree
Hide file tree
Showing 57 changed files with 5,009 additions and 5,473 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
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';
7 changes: 1 addition & 6 deletions index.js
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');
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src';
21 changes: 21 additions & 0 deletions lib/cache/LICENSE
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.
101 changes: 101 additions & 0 deletions lib/cache/README.md
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
```
2 changes: 2 additions & 0 deletions lib/cache/index.d.ts
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';
Loading

0 comments on commit 46f7cb6

Please sign in to comment.