-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrating to a mono repo - redis (#657)
* 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
Showing
20 changed files
with
1,633 additions
and
11 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
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
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,6 @@ | ||
version: '3' | ||
services: | ||
redis: | ||
image: redis | ||
ports: | ||
- '6379:6379' |
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
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 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. |
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,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) |
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,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" | ||
} | ||
} |
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,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); | ||
} |
Oops, something went wrong.