@resolid/cache-redis 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/index.d.mts +34 -0
- package/dist/index.mjs +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present, Resolid Tech
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Redis Cache store for @resolid/cache
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
<b>[Documentation](https://www.resolid.tech/docs/cache)</b> | [Framework Bundle](https://github.com/resolid/framework)
|
|
7
|
+
|
|
8
|
+
Redis store for @resolid/cache
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Built on top of @redis/client.
|
|
13
|
+
- TTL is handled directly by Redis.
|
|
14
|
+
- Supports Redis Clusters.
|
|
15
|
+
- Url connection string support or pass in your Redis Options
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
pnpm add @resolid/cache @resolid/cache-redis
|
|
21
|
+
# or
|
|
22
|
+
npm install @resolid/cache @resolid/cache-redis
|
|
23
|
+
# or
|
|
24
|
+
yarn add @resolid/cache @resolid/cache-redis
|
|
25
|
+
# or
|
|
26
|
+
bun add @resolid/cache @resolid/cache-redis
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { Cacher } from "@resolid/cache";
|
|
33
|
+
import { RedisCache } from "@resolid/cache-redis";
|
|
34
|
+
|
|
35
|
+
const cache = new Cacher({
|
|
36
|
+
store: new RedisCache("redis://user:pass@localhost:6379"),
|
|
37
|
+
defaultTtl: 1000,
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT License (MIT). Please see [LICENSE](./LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { RedisClientOptions, RedisClusterOptions, RedisSentinelOptions } from "@redis/client";
|
|
2
|
+
import { CacheStore } from "@resolid/cache/stores";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
type RedisCacheOptions = {
|
|
6
|
+
namespace?: string;
|
|
7
|
+
connectionTimeout?: number | null;
|
|
8
|
+
clearBatchSize?: number;
|
|
9
|
+
forceClose?: boolean;
|
|
10
|
+
};
|
|
11
|
+
declare class RedisCache implements CacheStore {
|
|
12
|
+
private readonly _client;
|
|
13
|
+
private readonly _options;
|
|
14
|
+
private readonly _isCluster;
|
|
15
|
+
private _connectPromise?;
|
|
16
|
+
constructor(connect?: string | RedisClientOptions | RedisClusterOptions | RedisSentinelOptions, options?: RedisCacheOptions);
|
|
17
|
+
private _resolve;
|
|
18
|
+
private _connect;
|
|
19
|
+
private _getClient;
|
|
20
|
+
private _getMasterNodes;
|
|
21
|
+
private _getSlotMap;
|
|
22
|
+
private _getSlotMaster;
|
|
23
|
+
has(key: string): Promise<boolean>;
|
|
24
|
+
get(key: string): Promise<string | undefined>;
|
|
25
|
+
getMultiple(keys: string[]): Promise<(string | undefined)[]>;
|
|
26
|
+
set(key: string, value: string, ttl?: number): Promise<boolean>;
|
|
27
|
+
setMultiple(values: Record<string, string>, ttl?: number): Promise<boolean>;
|
|
28
|
+
del(key: string): Promise<boolean>;
|
|
29
|
+
delMultiple(keys: string[]): Promise<boolean>;
|
|
30
|
+
clear(): Promise<boolean>;
|
|
31
|
+
dispose(): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { RedisCache, RedisCacheOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createClient as e,createCluster as t,createSentinel as n}from"@redis/client";import r from"cluster-key-slot";var i=class{_client;_options;_isCluster=!1;_connectPromise;constructor(r,i){let a={reconnectStrategy:e=>Math.min(2**e*100,2e3)+(Math.random()-.5)*100};r&&typeof r==`object`?`sentinelRootNodes`in r?this._client=n(r):`rootNodes`in r?(this._client=t({...r,defaults:{...r.defaults,socket:{...r.defaults?.socket,...a}}}),this._isCluster=!0):this._client=e({...r,socket:{...r.socket,...a}}):this._client=e({url:r,socket:a}),this._options={namespace:`rs`,forceClose:!1,connectionTimeout:null,clearBatchSize:1e3,...i}}_resolve(e){return`${this._options.namespace}::${e}`}async _connect(){try{if(this._options.connectionTimeout==null)await this._client.connect();else{let e=this._client.connect(),t;try{await Promise.race([e,new Promise((e,n)=>{t=setTimeout(()=>{n(Error(`Redis timed out after ${this._options.connectionTimeout}ms`))},this._options.connectionTimeout)})])}finally{t&&clearTimeout(t)}}}catch(e){throw await this._client.destroy(),e}}async _getClient(){return this._client.isOpen?this._client:(this._connectPromise??=this._connect().finally(()=>{this._connectPromise=void 0}),await this._connectPromise,this._client)}async _getMasterNodes(){let e=await this._getClient();if(this._isCluster){let t=e,n=t.masters.map(async e=>t.nodeClient(e));return Promise.all(n)}return[e]}_getSlotMap(e){let t=new Map;if(this._isCluster)for(let n of e){let e=r(n),i=t.get(e)??[];i.push(n),t.set(e,i)}else t.set(0,e);return t}async _getSlotMaster(e){let t=await this._getClient();if(this._isCluster){let n=t;return await n.nodeClient(n.slots[e].master)}return t}async has(e){let t=await this._getClient();try{return await t.exists(this._resolve(e))===1}catch{return!1}}async get(e){let t=await this._getClient();try{let n=await t.get(this._resolve(e));return n===null?void 0:n}catch{return}}async getMultiple(e){if(e.length===0)return[];let t=e.map(e=>this._resolve(e));try{if(this._isCluster){let e=new Map;return await Promise.all(Array.from(this._getSlotMap(t),async([t,n])=>{let r=await(await this._getSlotMaster(t)).mGet(n);for(let[t,i]of r.entries())e.set(n[t],i??void 0)})),t.map(t=>e.get(t))}return(await(await this._getClient()).mGet(t)).map(e=>e??void 0)}catch{return Array(e.length).fill(void 0)}}async set(e,t,n){let r=await this._getClient();try{return n?await r.set(this._resolve(e),t,{expiration:{type:`PX`,value:n}}):await r.set(this._resolve(e),t),!0}catch{return!1}}async setMultiple(e,t){try{if(this._isCluster){let n=new Map;for(let[t,i]of Object.entries(e)){let e=this._resolve(t),a=r(e),o=n.get(a)??{};o[e]=i,n.set(a,o)}await Promise.all(Array.from(n.entries(),async([e,n])=>{let r=(await this._getSlotMaster(e)).multi();for(let[e,i]of Object.entries(n))t?r.set(e,i,{expiration:{type:`PX`,value:t}}):r.set(e,i);await r.exec()}))}else{let n=(await this._getClient()).multi();for(let[r,i]of Object.entries(e))t?n.set(this._resolve(r),i,{expiration:{type:`PX`,value:t}}):n.set(this._resolve(r),i);await n.exec()}return!0}catch{return!1}}async del(e){let t=await this._getClient();try{return await t.del(this._resolve(e))>0}catch{return!1}}async delMultiple(e){if(e.length===0)return!0;let t=e.map(e=>this._resolve(e));try{return this._isCluster?await Promise.all(Array.from(this._getSlotMap(t),async([e,t])=>{let n=(await this._getSlotMaster(e)).multi();for(let e of t)n.del(e);await n.exec()})):await(await this._getClient()).del(t),!0}catch{return!1}}async clear(){try{let e=await this._getMasterNodes();return await Promise.all(e.map(async e=>{let t=`0`,n=this._options.clearBatchSize,r=this._resolve(`*`);do{let i=await e.scan(t,{MATCH:r,COUNT:n,TYPE:`string`});t=i.cursor.toString(),i.keys.length>0&&await Promise.all(Array.from(this._getSlotMap(i.keys).entries(),async([e,t])=>{await(await this._getSlotMaster(e)).del(t)}))}while(t!==`0`)})),!0}catch{return!1}}async dispose(){this._client.isOpen&&(this._options.forceClose?await this._client.destroy():await this._client.close())}};export{i as RedisCache};
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@resolid/cache-redis",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Redis Cache store for @resolid/cache",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"cache",
|
|
8
|
+
"cache manager",
|
|
9
|
+
"redis",
|
|
10
|
+
"resolid"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://www.resolid.tech",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "Huijie Wei",
|
|
16
|
+
"email": "hello@resolid.tech"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/resolid/framework.git",
|
|
21
|
+
"directory": "packages/cache-redis"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"import": "./dist/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsdown",
|
|
42
|
+
"lint": "oxlint",
|
|
43
|
+
"test": "vitest run --reporter=hanging-process",
|
|
44
|
+
"typecheck": "tsc --noEmit"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@redis/client": "^5.12.1",
|
|
48
|
+
"cluster-key-slot": "^1.1.2"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@resolid/utils": "^1.3.8",
|
|
52
|
+
"tsdown": "^0.22.0"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@resolid/cache": "workspace:^"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": "^22.13.0 || >=24"
|
|
59
|
+
}
|
|
60
|
+
}
|