@joker.server/redis 1.0.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/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Joker Server Redis Cache
2
+
3
+ Joker Server Redis Cache Solution Plugin
4
+
5
+ ## How to Use
6
+
7
+ ```
8
+ pnpm add @joker.server/redis
9
+ ```
10
+
11
+ Specify the cache strategy during Server initialization
12
+
13
+ ```js
14
+ let redisCache = new RedisCache({
15
+ url: "your redis service address"
16
+ });
17
+
18
+ //Wait for the connection to be established
19
+ await redisCache.client.connect();
20
+
21
+ //Configure cache strategy, please refer to the Server help document for initCacheOption
22
+ initCacheOption(redisCache);
23
+ ```
package/dist/bundle.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var t=require("redis");function e(t,e){return`${t}:${e}`}exports.RedisCache=class{client;constructor(e){this.client=t.createClient(e)}async expire(t,e,a){await(this.client?.expire(t,e))}async incrBy(t,e){return await(this.client?.incrBy(t,e))}async incrByFloat(t,e){let a=await(this.client?.incrByFloat(t,e));return Number(a)}async setHashValue(t,e,a,i){let s=JSON.stringify(a);i?await this.client.hSetNX(t,e,s):await this.client.hSet(t,e,s)}async getHashValue(t,e){let a=await this.client.hGet(t,e);if(a)return JSON.parse(a)}async deleteHashValue(t,e){await this.client.hDel(t,e)}getHashKeys(t){return this.client.hKeys(t)}async hasHashKey(t,e){return 1===await this.client.hExists(t,e)}incrHashBy(t,e,a){return this.client.hIncrBy(t,e,a)}async incrHashByFloat(t,e,a){let i=await(this.client?.hIncrByFloat(t,e,a));return Number(i)}clearHash(t){return this.delete(t)}async clearAll(){await this.client.flushDb()}async get(t){let e=await this.client.get(t);if(e)return JSON.parse(e)}async set(t,e,a,i){let s=JSON.stringify(e);i?await this.client.setNX(t,s):await this.client.set(t,s),a&&this.client.expire(t,a)}async delete(t){await this.client.del(t)}async exists(t){return await this.client.exists(t)>0}async arrayPush(t,...e){let a=e.map((t=>JSON.stringify(t)));await this.client.rPush(t,a)}async arraySlice(t,e,a){return(await this.client.lRange(t,e,a)).map((t=>JSON.parse(t)))}async arrayItem(t,e){let a=await this.client.lIndex(t,e);if(null!==a)return JSON.parse(a)}async arrayLength(t){return await this.client.lLen(t)}setMapValue(t,a,i,s,n){let r=e(t,a);return this.set(r,i,s,n)}async setMapValues(t,a,i,s){let n={};for(let i in a){n[e(t,i)]=JSON.stringify(a[i])}if(s?await this.client.mSetNX(n):await this.client.mSet(n),i)for(let t in n)this.client.expire(t,i)}getMapValue(t,a){let i=e(t,a);return this.get(i)}async getMapValues(t,a){return(await this.client.mGet(a.map((a=>e(t,a))))).map((t=>{if(t)return JSON.parse(t)}))}deleteMapValue(t,a){let i=e(t,a);return this.delete(i)}async getMapKeys(t){return(await this.client.keys(`${t}:*`)).map((e=>e.substring(t.length+1)))}hasMapKey(t,a){let i=e(t,a);return this.exists(i)}async clearMap(t){let e=await this.getMapKeys(t);for(let t of e)this.delete(t)}};
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@joker.server/redis",
3
+ "version": "1.0.1",
4
+ "description": "Joker 服务端 Redis 缓存方案插件",
5
+ "main": "dist/bundle.js",
6
+ "types": "./types/index.d.ts",
7
+ "scripts": {
8
+ "build": "rm -rf types && joker_build_library --format=cjs --input=src/index.ts",
9
+ "build:prod": "joker_build_library --sourcemap=false --terser",
10
+ "release": "npm run build && joker_release_library",
11
+ "release:prod": "npm run build:prod && npm publish --access public --registry https://registry.npmjs.org/"
12
+ },
13
+ "author": "Zohar",
14
+ "license": "MIT",
15
+ "files": [
16
+ "dist/bundle.js",
17
+ "types/*",
18
+ "README.md"
19
+ ],
20
+ "devDependencies": {
21
+ "@joker.front/library-cli": "^1.2.15",
22
+ "@joker.server/core": "^1.0.158",
23
+ "@joker.front/shared": "^1.5.30",
24
+ "typescript": "^5.0.4"
25
+ },
26
+ "dependencies": {
27
+ "redis": "^5.5.6"
28
+ }
29
+ }
@@ -0,0 +1,34 @@
1
+ import type { ICache } from "@joker.server/core";
2
+ import { RedisClientOptions, createClient } from "redis";
3
+ export declare class RedisCache implements ICache {
4
+ client: ReturnType<typeof createClient>;
5
+ constructor(redisOption?: RedisClientOptions);
6
+ expire(key: string, time: number, callBack?: Function): Promise<void>;
7
+ incrBy(key: string, number: number): Promise<number>;
8
+ incrByFloat(key: string, number: number): Promise<number>;
9
+ setHashValue(key: string, id: string, value: any, setnx?: boolean): Promise<void>;
10
+ getHashValue(key: string, id: string): Promise<any>;
11
+ deleteHashValue(key: string, id: string): Promise<void>;
12
+ getHashKeys(key: string): Promise<string[]>;
13
+ hasHashKey(key: string, id: string): Promise<boolean>;
14
+ incrHashBy(key: string, id: string, number: number): Promise<number>;
15
+ incrHashByFloat(key: string, id: string, number: number): Promise<number>;
16
+ clearHash(key: string): Promise<any>;
17
+ clearAll(): Promise<void>;
18
+ get(key: string): Promise<any>;
19
+ set(key: string, value: any, expire: number | undefined, setnx?: boolean | undefined): Promise<void>;
20
+ delete(key: string): Promise<void>;
21
+ exists(key: string): Promise<boolean>;
22
+ arrayPush(key: string, ...values: any[]): Promise<void>;
23
+ arraySlice(key: string, start: number, end: number): Promise<any[]>;
24
+ arrayItem(key: string, index: number): Promise<any>;
25
+ arrayLength(key: string): Promise<number>;
26
+ setMapValue(key: string, id: string, value: any, expire: number | undefined, setnx?: boolean | undefined): Promise<void>;
27
+ setMapValues(key: string, values: Record<string, any>, expire: number | undefined, setnx?: boolean | undefined): Promise<void>;
28
+ getMapValue(key: string, id: string): Promise<any>;
29
+ getMapValues(key: string, ids: string[]): Promise<any[]>;
30
+ deleteMapValue(key: string, id: string): Promise<void>;
31
+ getMapKeys(key: string): Promise<string[]>;
32
+ hasMapKey(key: string, id: string): Promise<boolean>;
33
+ clearMap(key: string): Promise<any>;
34
+ }