Skip to content

Commit

Permalink
migrating to a mono repo - redis (#657)
Browse files Browse the repository at this point in the history
* initial mono repo changes

* Update test.yml

* Create README.md

* adding in redis and ioredis

* update tests to use caching

* removing security link

* serializing testing

* removing bad tests

* version bump and fix github path
  • Loading branch information
jaredwray committed Apr 5, 2024
1 parent 0d97da2 commit 5fca626
Show file tree
Hide file tree
Showing 20 changed files with 1,633 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
- name: Install pnpm
run: npm install -g pnpm

- name: Start Test Services
run: pnpm test:services:start

- name: Install dependencies
run: pnpm install

Expand Down
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,43 @@ To get started you can visit the [cache-manager](/packages/cache-manager/README.

* [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) - Our code of conduct
* [CONTRIBUTING.md](CONTRIBUTING.md) - How to contribute to this project
* [SECURITY.md](SECURITY.md) - Security guidelines and supported versions

## Store Engines

### Official and updated to last version

- [node-cache-manager-redis-yet](https://github.com/jaredwray/cache-manager/packages/cache-manager-redis-yet) (uses [node_redis](https://github.com/NodeRedis/node_redis))

- [node-cache-manager-ioredis-yet](https://github.com/jaredwray/cache-manager/packages/cache-manager-ioredis-yet) (uses [ioredis](https://github.com/luin/ioredis))

### Third party

- [node-cache-manager-redis](https://github.com/dial-once/node-cache-manager-redis) (uses [sol-redis-pool](https://github.com/joshuah/sol-redis-pool))

- [node-cache-manager-redis-store](https://github.com/dabroek/node-cache-manager-redis-store) (uses [node_redis](https://github.com/NodeRedis/node_redis))

- [node-cache-manager-ioredis](https://github.com/Tirke/node-cache-manager-ioredis) (uses [ioredis](https://github.com/luin/ioredis))

- [node-cache-manager-mongodb](https://github.com/v4l3r10/node-cache-manager-mongodb)

- [node-cache-manager-mongoose](https://github.com/disjunction/node-cache-manager-mongoose)

- [node-cache-manager-fs-binary](https://github.com/sheershoff/node-cache-manager-fs-binary)

- [node-cache-manager-fs-hash](https://github.com/rolandstarke/node-cache-manager-fs-hash)

- [node-cache-manager-hazelcast](https://github.com/marudor/node-cache-manager-hazelcast)

- [node-cache-manager-memcached-store](https://github.com/theogravity/node-cache-manager-memcached-store)

- [node-cache-manager-memory-store](https://github.com/theogravity/node-cache-manager-memory-store)

- [node-cache-manager-couchbase](https://github.com/davidepellegatta/node-cache-manager-couchbase)

- [node-cache-manager-sqlite](https://github.com/maxpert/node-cache-manager-sqlite)

- [@resolid/cache-manager-sqlite](https://github.com/huijiewei/cache-manager-sqlite) (uses [better-sqlite3](https://github.com/WiseLibs/better-sqlite3))


## Getting Started with the Mono Repo

Expand Down Expand Up @@ -70,13 +106,5 @@ Please also refer to our [Code of Conduct](CODE_OF_CONDUCT.md) for more informat

To ask a question, create an issue with the label "question." In the issue description, include the related code and any context that can help us answer your question.

## Request the Addition of a Storage Adapter

To request a new storage adapter, create an issue with the label "storage adapter." In the issue description, include any relevant information about the storage adapter that you would like to be added.

Once this request has been submitted in "issues" we will give it 30-60 days for any upvotes to take place. If there is little interest in the request, it will be closed.

If there is already an adapter that you would like to add, please post an issue with the label "storage adapter" and include the name of the adapter you would like to add with the description and any relevant information.

## License
MIT [LISCENCE](LICENSE)
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
redis:
image: redis
ports:
- '6379:6379'
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scripts": {
"test": "pnpm recursive run test",
"test:ci": "c8 --reporter=lcov pnpm recursive run test:ci",
"test:services:start": "docker-compose up -d",
"test:services:stop": "docker-compose down",
"build": "pnpm recursive run build",
"clean": "pnpm recursive run clean"
},
Expand Down
21 changes: 21 additions & 0 deletions packages/cache-manager-ioredis-yet/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Matthijs Dabroek

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.
17 changes: 17 additions & 0 deletions packages/cache-manager-ioredis-yet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Redis store for node cache manager

[![npm version](https://badge.fury.io/js/cache-manager-ioredis-yet.svg)](https://www.npmjs.com/package/cache-manager-ioredis-yet) [![codecov](https://codecov.io/gh/node-cache-manager/node-cache-manager-ioredis-yet/branch/master/graph/badge.svg?token=NX28S97MDF)](https://codecov.io/gh/node-cache-manager/node-cache-manager-ioredis-yet)

Redis cache store for [node-cache-manager](https://github.com/node-cache-manager/node-cache-manager).

## Installation

```sh
pnpm install cache-manager-ioredis-yet
```

## License

Licensed under the [MIT license](./LICENSE).

Fork from [https://github.com/dabroek/node-cache-manager-redis-store](https://github.com/dabroek/node-cache-manager-redis-store)
71 changes: 71 additions & 0 deletions packages/cache-manager-ioredis-yet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "cache-manager-ioredis-yet",
"authors": [
{
"name": "Matthijs Dabroek",
"email": "dabroek@gmail.com"
},
{
"name": "Jared Wray",
"email": "me@jaredwray.com"
},
{
"name": "Juan Aguilar Santillana",
"email": "mhpoin@gmail.com"
}
],
"description": "Redis store for node-cache-manager updated",
"version": "2.0.3",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/jaredwray/cache-manager.git"
},
"files": [
"dist",
"LICENSE",
"README.md"
],
"keywords": [
"cache-manager",
"ioredis",
"redis",
"redis-cluster"
],
"scripts": {
"clean": "rimraf dist coverage node_modules yarn.lock pnpm-lock.yaml package-lock.json",
"build": "rimraf dist && tsc -p tsconfig.build.json",
"test": "vitest run --coverage",
"test:ci": "vitest run",
"prepare": "npm run build"
},
"dependencies": {
"cache-manager": "^5.4.0",
"ioredis": "^5.3.2",
"telejson": "^7.2.0"
},
"devDependencies": {
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "7.1.1",
"@typescript-eslint/parser": "7.1.1",
"@vitest/coverage-v8": "1.3.1",
"dotenv-cli": "7.3.0",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"lint-staged": "15.2.2",
"prettier": "3.2.5",
"rimraf": "^5.0.5",
"typescript": "5.3.3",
"vitest": "1.3.1"
},
"engines": {
"node": ">= 18.0.0"
},
"lint-staged": {
"*.{ts,js}": "eslint --cache --fix",
"*.{json,prisma,md,yml}": "prettier --write"
}
}
133 changes: 133 additions & 0 deletions packages/cache-manager-ioredis-yet/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import Redis, {
Cluster,
ClusterNode,
ClusterOptions,
RedisOptions,
} from 'ioredis';

import * as telejson from 'telejson';

const stringify = (value: unknown) =>
telejson.stringify({ v: value }, { maxDepth: Infinity });

const parse = (value: string) => telejson.parse(value)?.v;

import type { Cache, Store, Config } from 'cache-manager';

export type RedisCache = Cache<RedisStore>;

export interface RedisStore extends Store {
readonly isCacheable: (value: unknown) => boolean;
get client(): Redis | Cluster;
}

const getVal = (value: unknown) => stringify(value);

export class NoCacheableError implements Error {
name = 'NoCacheableError';
constructor(public message: string) {}
}

export const avoidNoCacheable = async <T>(p: Promise<T>) => {
try {
return await p;
} catch (e) {
if (!(e instanceof NoCacheableError)) throw e;
}
};
function builder(
redisCache: Redis | Cluster,
reset: () => Promise<void>,
keys: (pattern: string) => Promise<string[]>,
options?: Config,
) {
const isCacheable =
options?.isCacheable || ((value) => value !== undefined && value !== null);

return {
async get<T>(key: string) {
const val = await redisCache.get(key);
if (val === undefined || val === null) return undefined;
else return parse(val) as T;
},
async set(key, value, ttl) {
if (!isCacheable(value))
throw new NoCacheableError(`"${value}" is not a cacheable value`);
const t = ttl === undefined ? options?.ttl : ttl;
if (t !== undefined && t !== 0)
await redisCache.set(key, getVal(value), 'PX', t);
else await redisCache.set(key, getVal(value));
},
async mset(args, ttl) {
const t = ttl === undefined ? options?.ttl : ttl;
if (t !== undefined && t !== 0) {
const multi = redisCache.multi();
for (const [key, value] of args) {
if (!isCacheable(value))
throw new NoCacheableError(
`"${getVal(value)}" is not a cacheable value`,
);
multi.set(key, getVal(value), 'PX', t);
}
await multi.exec();
} else
await redisCache.mset(
args.flatMap(([key, value]) => {
if (!isCacheable(value))
throw new Error(`"${getVal(value)}" is not a cacheable value`);
return [key, getVal(value)] as [string, string];
}),
);
},
mget: (...args) =>
redisCache
.mget(args)
.then((x) =>
x.map((x) =>
x === null || x === undefined ? undefined : (parse(x) as unknown),
),
),
async mdel(...args) {
await redisCache.del(args);
},
async del(key) {
await redisCache.del(key);
},
ttl: async (key) => redisCache.pttl(key),
keys: (pattern = '*') => keys(pattern),
reset,
isCacheable,
get client() {
return redisCache;
},
} as RedisStore;
}

export interface RedisClusterConfig {
nodes: ClusterNode[];
options?: ClusterOptions;
}

export async function redisStore(
options?: (RedisOptions | { clusterConfig: RedisClusterConfig }) & Config,
) {
options ||= {};
const redisCache =
'clusterConfig' in options
? new Redis.Cluster(
options.clusterConfig.nodes,
options.clusterConfig.options,
)
: new Redis(options);

return redisInsStore(redisCache, options);
}

export function redisInsStore(redisCache: Redis | Cluster, options?: Config) {
const reset = async () => {
await redisCache.flushdb();
};
const keys = (pattern: string) => redisCache.keys(pattern);

return builder(redisCache, reset, keys, options);
}

0 comments on commit 5fca626

Please sign in to comment.