@resolid/cache-redis 1.0.1 → 1.1.1
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/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +7 -7
package/dist/index.d.mts
CHANGED
|
@@ -13,7 +13,7 @@ declare class RedisCache implements CacheStore {
|
|
|
13
13
|
private readonly _options;
|
|
14
14
|
private readonly _isCluster;
|
|
15
15
|
private _connectPromise?;
|
|
16
|
-
constructor(connect?: string | RedisClientOptions | RedisClusterOptions | RedisSentinelOptions, options?: RedisCacheOptions);
|
|
16
|
+
constructor(connect?: string | Omit<RedisClientOptions, "RESP"> | Omit<RedisClusterOptions, "RESP"> | Omit<RedisSentinelOptions, "RESP">, options?: RedisCacheOptions);
|
|
17
17
|
private _resolve;
|
|
18
18
|
private _connect;
|
|
19
19
|
private _getClient;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +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._dispose(!0),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
|
|
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._dispose(!0),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.from({length:e.length}).fill(void 0)}}async set(e,t,n){let r=await this._getClient();try{return await r.set(this._resolve(e),t,n?{expiration:{type:`PX`,value:n}}:void 0),!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(e){this._client.isOpen&&await(e?this._client.destroy():this._client.close())}async dispose(){await this._dispose(this._options.forceClose)}};export{i as RedisCache};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@resolid/cache-redis",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Redis Cache store for @resolid/cache",
|
|
6
6
|
"keywords": [
|
|
@@ -38,23 +38,23 @@
|
|
|
38
38
|
"provenance": true
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@redis/client": "^
|
|
41
|
+
"@redis/client": "^6.1.0",
|
|
42
42
|
"cluster-key-slot": "^1.1.2"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@resolid/utils": "^1.
|
|
46
|
-
"tsdown": "^0.22.
|
|
45
|
+
"@resolid/utils": "^1.6.3",
|
|
46
|
+
"tsdown": "^0.22.3"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@resolid/cache": "^1.2.
|
|
49
|
+
"@resolid/cache": "^1.2.2"
|
|
50
50
|
},
|
|
51
51
|
"engines": {
|
|
52
|
-
"node": "^22.
|
|
52
|
+
"node": "^22.22.0 || >=24"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"build": "tsdown",
|
|
56
56
|
"lint": "oxlint",
|
|
57
|
-
"test": "vitest run
|
|
57
|
+
"test": "vitest run",
|
|
58
58
|
"typecheck": "tsc --noEmit"
|
|
59
59
|
}
|
|
60
60
|
}
|