@commandkit/redis 0.1.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 +5 -0
- package/README.md +24 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +84 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Copyright 2024 Avraj Sahota
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# `@commandkit/redis`
|
|
2
|
+
|
|
3
|
+
Redis cache provider for CommandKit.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @commandkit/redis
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { CommandKit } from 'commandkit';
|
|
15
|
+
import { RedisCache } from '@commandkit/redis';
|
|
16
|
+
|
|
17
|
+
new CommandKit({
|
|
18
|
+
client,
|
|
19
|
+
commandsPath,
|
|
20
|
+
eventsPath,
|
|
21
|
+
// uses default redis connection options (ioredis package)
|
|
22
|
+
cacheProvider: new RedisCache(),
|
|
23
|
+
});
|
|
24
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Redis, type RedisOptions } from 'ioredis';
|
|
2
|
+
import { CacheProvider } from 'commandkit';
|
|
3
|
+
export type Awaitable<T> = T | Promise<T>;
|
|
4
|
+
export type SerializeFunction = (value: any) => Awaitable<string>;
|
|
5
|
+
export type DeserializeFunction = (value: string) => Awaitable<any>;
|
|
6
|
+
/**
|
|
7
|
+
* A cache provider that uses Redis as the cache store.
|
|
8
|
+
* @example const redisCache = new RedisCache();
|
|
9
|
+
* new CommandKit({
|
|
10
|
+
* ...
|
|
11
|
+
* cacheProvider: redisCache,
|
|
12
|
+
* });
|
|
13
|
+
*/
|
|
14
|
+
export declare class RedisCache extends CacheProvider {
|
|
15
|
+
protected redis: Redis;
|
|
16
|
+
/**
|
|
17
|
+
* Serialize function used to serialize values before storing them in the cache.
|
|
18
|
+
* By default, it uses `JSON.stringify`.
|
|
19
|
+
*/
|
|
20
|
+
serialize: SerializeFunction;
|
|
21
|
+
/**
|
|
22
|
+
* Deserialize function used to deserialize values before returning them from the cache.
|
|
23
|
+
* By default, it uses `JSON.parse`.
|
|
24
|
+
*/
|
|
25
|
+
deserialize: DeserializeFunction;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new RedisCache instance.
|
|
28
|
+
*/
|
|
29
|
+
constructor();
|
|
30
|
+
/**
|
|
31
|
+
* Create a new RedisCache instance with the provided Redis client.
|
|
32
|
+
* @param redis The Redis client to use.
|
|
33
|
+
*/
|
|
34
|
+
constructor(redis: Redis);
|
|
35
|
+
/**
|
|
36
|
+
* Create a new RedisCache instance with the provided Redis options.
|
|
37
|
+
* @param redis The Redis client to use.
|
|
38
|
+
*/
|
|
39
|
+
constructor(redis: RedisOptions);
|
|
40
|
+
/**
|
|
41
|
+
* Retrieve a value from the cache.
|
|
42
|
+
* @param key The key to retrieve the value for.
|
|
43
|
+
* @returns The value stored in the cache, or `undefined` if it does not exist.
|
|
44
|
+
*/
|
|
45
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
46
|
+
/**
|
|
47
|
+
* Store a value in the cache.
|
|
48
|
+
* @param key The key to store the value under.
|
|
49
|
+
* @param value The value to store in the cache.
|
|
50
|
+
* @param ttl The time-to-live for the cache entry in milliseconds.
|
|
51
|
+
*/
|
|
52
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Clear all entries from the cache.
|
|
55
|
+
*/
|
|
56
|
+
clear(): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Delete a value from the cache.
|
|
59
|
+
* @param key The key to delete the value for.
|
|
60
|
+
*/
|
|
61
|
+
delete(key: string): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Check if a value exists in the cache.
|
|
64
|
+
* @param key The key to check for.
|
|
65
|
+
* @returns True if the key exists in the cache, false otherwise.
|
|
66
|
+
*/
|
|
67
|
+
exists(key: string): Promise<boolean>;
|
|
68
|
+
/**
|
|
69
|
+
* Set the time-to-live for a cache entry.
|
|
70
|
+
* @param key The key to set the time-to-live for.
|
|
71
|
+
* @param ttl The time-to-live value in milliseconds.
|
|
72
|
+
*/
|
|
73
|
+
expire(key: string, ttl: number): Promise<void>;
|
|
74
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RedisCache = void 0;
|
|
4
|
+
const ioredis_1 = require("ioredis");
|
|
5
|
+
const commandkit_1 = require("commandkit");
|
|
6
|
+
/**
|
|
7
|
+
* A cache provider that uses Redis as the cache store.
|
|
8
|
+
* @example const redisCache = new RedisCache();
|
|
9
|
+
* new CommandKit({
|
|
10
|
+
* ...
|
|
11
|
+
* cacheProvider: redisCache,
|
|
12
|
+
* });
|
|
13
|
+
*/
|
|
14
|
+
class RedisCache extends commandkit_1.CacheProvider {
|
|
15
|
+
constructor(redis) {
|
|
16
|
+
super();
|
|
17
|
+
if (redis instanceof ioredis_1.Redis) {
|
|
18
|
+
this.redis = redis;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.redis = new ioredis_1.Redis(redis ?? {});
|
|
22
|
+
}
|
|
23
|
+
this.serialize = JSON.stringify;
|
|
24
|
+
this.deserialize = JSON.parse;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Retrieve a value from the cache.
|
|
28
|
+
* @param key The key to retrieve the value for.
|
|
29
|
+
* @returns The value stored in the cache, or `undefined` if it does not exist.
|
|
30
|
+
*/
|
|
31
|
+
async get(key) {
|
|
32
|
+
const value = await this.redis.get(key);
|
|
33
|
+
if (value === null) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
return JSON.parse(value);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Store a value in the cache.
|
|
40
|
+
* @param key The key to store the value under.
|
|
41
|
+
* @param value The value to store in the cache.
|
|
42
|
+
* @param ttl The time-to-live for the cache entry in milliseconds.
|
|
43
|
+
*/
|
|
44
|
+
async set(key, value, ttl) {
|
|
45
|
+
const serialized = await this.serialize(value);
|
|
46
|
+
if (typeof ttl === 'number') {
|
|
47
|
+
await this.redis.set(key, serialized, 'PX', ttl);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
await this.redis.set(key, serialized);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Clear all entries from the cache.
|
|
55
|
+
*/
|
|
56
|
+
async clear() {
|
|
57
|
+
await this.redis.flushall();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Delete a value from the cache.
|
|
61
|
+
* @param key The key to delete the value for.
|
|
62
|
+
*/
|
|
63
|
+
async delete(key) {
|
|
64
|
+
await this.redis.del(key);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if a value exists in the cache.
|
|
68
|
+
* @param key The key to check for.
|
|
69
|
+
* @returns True if the key exists in the cache, false otherwise.
|
|
70
|
+
*/
|
|
71
|
+
async exists(key) {
|
|
72
|
+
return Boolean(await this.redis.exists(key));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Set the time-to-live for a cache entry.
|
|
76
|
+
* @param key The key to set the time-to-live for.
|
|
77
|
+
* @param ttl The time-to-live value in milliseconds.
|
|
78
|
+
*/
|
|
79
|
+
async expire(key, ttl) {
|
|
80
|
+
await this.redis.pexpire(key, ttl);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.RedisCache = RedisCache;
|
|
84
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEscUNBQW1EO0FBQ25ELDJDQUEyQztBQU0zQzs7Ozs7OztHQU9HO0FBQ0gsTUFBYSxVQUFXLFNBQVEsMEJBQWE7SUE2QjNDLFlBQW1CLEtBQTRCO1FBQzdDLEtBQUssRUFBRSxDQUFDO1FBRVIsSUFBSSxLQUFLLFlBQVksZUFBSyxFQUFFLENBQUM7WUFDM0IsSUFBSSxDQUFDLEtBQUssR0FBRyxLQUFLLENBQUM7UUFDckIsQ0FBQzthQUFNLENBQUM7WUFDTixJQUFJLENBQUMsS0FBSyxHQUFHLElBQUksZUFBSyxDQUFDLEtBQUssSUFBSSxFQUFFLENBQUMsQ0FBQztRQUN0QyxDQUFDO1FBRUQsSUFBSSxDQUFDLFNBQVMsR0FBRyxJQUFJLENBQUMsU0FBUyxDQUFDO1FBQ2hDLElBQUksQ0FBQyxXQUFXLEdBQUcsSUFBSSxDQUFDLEtBQUssQ0FBQztJQU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lBQzVCLENBQUM7SUFFRDs7OztPQUlHO0lBQ0ksS0FBSyxDQUFDLE1BQU0sQ0FBQyxHQUFXO1FBQzdCLE9BQU8sT0FBTyxDQUFDLE1BQU0sSUFBSSxDQUFDLEtBQUssQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQztJQUMvQyxDQUFDO0lBRUQ7Ozs7T0FJRztJQUNJLEtBQUssQ0FBQyxNQUFNLENBQUMsR0FBVyxFQUFFLEdBQVc7UUFDMUMsTUFBTSxJQUFJLENBQUMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxHQUFHLEVBQUUsR0FBRyxDQUFDLENBQUM7SUFDckMsQ0FBQztDQUNGO0FBekdELGdDQXlHQyJ9
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@commandkit/redis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Redis cache provider for CommandKit",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/underctrl-io/commandkit.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"commandkit",
|
|
16
|
+
"redis",
|
|
17
|
+
"cache"
|
|
18
|
+
],
|
|
19
|
+
"author": "twilight <hello@twlite.dev>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/underctrl-io/commandkit/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/underctrl-io/commandkit#readme",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"ioredis": "^5.4.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"typescript": "^5.7.3",
|
|
30
|
+
"commandkit": "0.1.11",
|
|
31
|
+
"tsconfig": "0.0.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc"
|
|
35
|
+
}
|
|
36
|
+
}
|