@commandkit/redis 1.2.0-rc.9 → 1.2.1-dev.20260310005506
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 +19 -3
- package/README.md +3 -0
- package/dist/cache-storage.d.ts +72 -0
- package/dist/cache-storage.js +101 -0
- package/dist/index.d.ts +3 -75
- package/dist/index.js +6 -93
- package/package.json +10 -9
package/LICENSE
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) Neplex
|
|
4
4
|
|
|
5
|
-
|
|
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
CHANGED
|
@@ -10,6 +10,9 @@ npm install @commandkit/redis
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
+
> [!WARNING]
|
|
14
|
+
> `RedisCache` from `@commandkit/redis` is deprecated. Import `RedisCache` from `@commandkit/cache/redis` instead.
|
|
15
|
+
|
|
13
16
|
This package provides a commandkit plugin that automatically registers the cache provider with the commandkit instance.
|
|
14
17
|
|
|
15
18
|
```js
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { CacheProvider, CacheEntry } from '@commandkit/cache';
|
|
2
|
+
import Redis, { type RedisOptions } from 'ioredis';
|
|
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
|
+
* setCacheProvider(redisCache);
|
|
10
|
+
* @deprecated Import `RedisCache` from `@commandkit/cache/redis` instead.
|
|
11
|
+
*/
|
|
12
|
+
export declare class RedisCache extends CacheProvider {
|
|
13
|
+
redis: Redis;
|
|
14
|
+
/**
|
|
15
|
+
* Serialize function used to serialize values before storing them in the cache.
|
|
16
|
+
* By default, it uses `JSON.stringify`.
|
|
17
|
+
*/
|
|
18
|
+
serialize: SerializeFunction;
|
|
19
|
+
/**
|
|
20
|
+
* Deserialize function used to deserialize values before returning them from the cache.
|
|
21
|
+
* By default, it uses `JSON.parse`.
|
|
22
|
+
*/
|
|
23
|
+
deserialize: DeserializeFunction;
|
|
24
|
+
/**
|
|
25
|
+
* Create a new RedisCache instance.
|
|
26
|
+
*/
|
|
27
|
+
constructor();
|
|
28
|
+
/**
|
|
29
|
+
* Create a new RedisCache instance with the provided Redis client.
|
|
30
|
+
* @param redis The Redis client to use.
|
|
31
|
+
*/
|
|
32
|
+
constructor(redis: Redis);
|
|
33
|
+
/**
|
|
34
|
+
* Create a new RedisCache instance with the provided Redis options.
|
|
35
|
+
* @param redis The Redis client to use.
|
|
36
|
+
*/
|
|
37
|
+
constructor(redis: RedisOptions);
|
|
38
|
+
/**
|
|
39
|
+
* Retrieve a value from the cache.
|
|
40
|
+
* @param key The key to retrieve the value for.
|
|
41
|
+
* @returns The value stored in the cache, or `undefined` if it does not exist.
|
|
42
|
+
*/
|
|
43
|
+
get<T>(key: string): Promise<CacheEntry<T> | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Store a value in the cache.
|
|
46
|
+
* @param key The key to store the value under.
|
|
47
|
+
* @param value The value to store in the cache.
|
|
48
|
+
* @param ttl The time-to-live for the cache entry in milliseconds.
|
|
49
|
+
*/
|
|
50
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Clear all entries from the cache.
|
|
53
|
+
*/
|
|
54
|
+
clear(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Delete a value from the cache.
|
|
57
|
+
* @param key The key to delete the value for.
|
|
58
|
+
*/
|
|
59
|
+
delete(key: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Check if a value exists in the cache.
|
|
62
|
+
* @param key The key to check for.
|
|
63
|
+
* @returns True if the key exists in the cache, false otherwise.
|
|
64
|
+
*/
|
|
65
|
+
exists(key: string): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Set the time-to-live for a cache entry.
|
|
68
|
+
* @param key The key to set the time-to-live for.
|
|
69
|
+
* @param ttl The time-to-live value in milliseconds.
|
|
70
|
+
*/
|
|
71
|
+
expire(key: string, ttl: number): Promise<void>;
|
|
72
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RedisCache = void 0;
|
|
7
|
+
const cache_1 = require("@commandkit/cache");
|
|
8
|
+
const commandkit_1 = require("commandkit");
|
|
9
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
10
|
+
/**
|
|
11
|
+
* A cache provider that uses Redis as the cache store.
|
|
12
|
+
* @example const redisCache = new RedisCache();
|
|
13
|
+
* setCacheProvider(redisCache);
|
|
14
|
+
* @deprecated Import `RedisCache` from `@commandkit/cache/redis` instead.
|
|
15
|
+
*/
|
|
16
|
+
class RedisCache extends cache_1.CacheProvider {
|
|
17
|
+
constructor(redis) {
|
|
18
|
+
(0, commandkit_1.warnDeprecated)({
|
|
19
|
+
what: 'RedisCache',
|
|
20
|
+
where: '@commandkit/redis',
|
|
21
|
+
message: 'Import `RedisCache` from `@commandkit/cache/redis` instead.',
|
|
22
|
+
});
|
|
23
|
+
super();
|
|
24
|
+
if (redis instanceof ioredis_1.default) {
|
|
25
|
+
this.redis = redis;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.redis = new ioredis_1.default(redis ?? {});
|
|
29
|
+
}
|
|
30
|
+
this.serialize = JSON.stringify;
|
|
31
|
+
this.deserialize = JSON.parse;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Retrieve a value from the cache.
|
|
35
|
+
* @param key The key to retrieve the value for.
|
|
36
|
+
* @returns The value stored in the cache, or `undefined` if it does not exist.
|
|
37
|
+
*/
|
|
38
|
+
async get(key) {
|
|
39
|
+
const value = await this.redis.get(key);
|
|
40
|
+
if (value === null) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const entry = this.deserialize(value);
|
|
44
|
+
if (entry.ttl && Date.now() > entry.ttl) {
|
|
45
|
+
await this.delete(key);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
return entry;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Store a value in the cache.
|
|
52
|
+
* @param key The key to store the value under.
|
|
53
|
+
* @param value The value to store in the cache.
|
|
54
|
+
* @param ttl The time-to-live for the cache entry in milliseconds.
|
|
55
|
+
*/
|
|
56
|
+
async set(key, value, ttl) {
|
|
57
|
+
const entry = {
|
|
58
|
+
value,
|
|
59
|
+
ttl: ttl != null ? Date.now() + ttl : undefined,
|
|
60
|
+
};
|
|
61
|
+
const serialized = this.serialize(entry);
|
|
62
|
+
const finalValue = serialized instanceof Promise ? await serialized : serialized;
|
|
63
|
+
if (typeof ttl === 'number') {
|
|
64
|
+
await this.redis.set(key, finalValue, 'PX', ttl);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
await this.redis.set(key, finalValue);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Clear all entries from the cache.
|
|
72
|
+
*/
|
|
73
|
+
async clear() {
|
|
74
|
+
await this.redis.flushall();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Delete a value from the cache.
|
|
78
|
+
* @param key The key to delete the value for.
|
|
79
|
+
*/
|
|
80
|
+
async delete(key) {
|
|
81
|
+
await this.redis.del(key);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Check if a value exists in the cache.
|
|
85
|
+
* @param key The key to check for.
|
|
86
|
+
* @returns True if the key exists in the cache, false otherwise.
|
|
87
|
+
*/
|
|
88
|
+
async exists(key) {
|
|
89
|
+
return Boolean(await this.redis.exists(key));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Set the time-to-live for a cache entry.
|
|
93
|
+
* @param key The key to set the time-to-live for.
|
|
94
|
+
* @param ttl The time-to-live value in milliseconds.
|
|
95
|
+
*/
|
|
96
|
+
async expire(key, ttl) {
|
|
97
|
+
await this.redis.pexpire(key, ttl);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.RedisCache = RedisCache;
|
|
101
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2FjaGUtc3RvcmFnZS5qcyI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M7SUFDSCxDQUFDO0lBRUQ7O09BRUc7SUFDSSxLQUFLLENBQUMsS0FBSztRQUNoQixNQUFNLElBQUksQ0FBQyxLQUFLLENBQUMsUUFBUSxFQUFFLENBQUM7SUFDOUIsQ0FBQztJQUVEOzs7T0FHRztJQUNJLEtBQUssQ0FBQyxNQUFNLENBQUMsR0FBVztRQUM3QixNQUFNLElBQUksQ0FBQyxLQUFLLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxDQUFDO0lBQzVCLENBQUM7SUFFRDs7OztPQUlHO0lBQ0ksS0FBSyxDQUFDLE1BQU0sQ0FBQyxHQUFXO1FBQzdCLE9BQU8sT0FBTyxDQUFDLE1BQU0sSUFBSSxDQUFDLEtBQUssQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQztJQUMvQyxDQUFDO0lBRUQ7Ozs7T0FJRztJQUNJLEtBQUssQ0FBQyxNQUFNLENBQUMsR0FBVyxFQUFFLEdBQVc7UUFDMUMsTUFBTSxJQUFJLENBQUMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxHQUFHLEVBQUUsR0FBRyxDQUFDLENBQUM7SUFDckMsQ0FBQztDQUNGO0FBNUhELGdDQTRIQyJ9
|
package/dist/index.d.ts
CHANGED
|
@@ -1,78 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type RedisOptions } from 'ioredis';
|
|
2
2
|
import { CommandKitPluginRuntime, RuntimePlugin } from 'commandkit';
|
|
3
|
-
import {
|
|
4
|
-
export type Awaitable<T> = T | Promise<T>;
|
|
5
|
-
export type SerializeFunction = (value: any) => Awaitable<string>;
|
|
6
|
-
export type DeserializeFunction = (value: string) => Awaitable<any>;
|
|
7
|
-
/**
|
|
8
|
-
* A cache provider that uses Redis as the cache store.
|
|
9
|
-
* @example const redisCache = new RedisCache();
|
|
10
|
-
* new CommandKit({
|
|
11
|
-
* ...
|
|
12
|
-
* cacheProvider: redisCache,
|
|
13
|
-
* });
|
|
14
|
-
*/
|
|
15
|
-
export declare class RedisCache extends CacheProvider {
|
|
16
|
-
redis: Redis;
|
|
17
|
-
/**
|
|
18
|
-
* Serialize function used to serialize values before storing them in the cache.
|
|
19
|
-
* By default, it uses `JSON.stringify`.
|
|
20
|
-
*/
|
|
21
|
-
serialize: SerializeFunction;
|
|
22
|
-
/**
|
|
23
|
-
* Deserialize function used to deserialize values before returning them from the cache.
|
|
24
|
-
* By default, it uses `JSON.parse`.
|
|
25
|
-
*/
|
|
26
|
-
deserialize: DeserializeFunction;
|
|
27
|
-
/**
|
|
28
|
-
* Create a new RedisCache instance.
|
|
29
|
-
*/
|
|
30
|
-
constructor();
|
|
31
|
-
/**
|
|
32
|
-
* Create a new RedisCache instance with the provided Redis client.
|
|
33
|
-
* @param redis The Redis client to use.
|
|
34
|
-
*/
|
|
35
|
-
constructor(redis: Redis);
|
|
36
|
-
/**
|
|
37
|
-
* Create a new RedisCache instance with the provided Redis options.
|
|
38
|
-
* @param redis The Redis client to use.
|
|
39
|
-
*/
|
|
40
|
-
constructor(redis: RedisOptions);
|
|
41
|
-
/**
|
|
42
|
-
* Retrieve a value from the cache.
|
|
43
|
-
* @param key The key to retrieve the value for.
|
|
44
|
-
* @returns The value stored in the cache, or `undefined` if it does not exist.
|
|
45
|
-
*/
|
|
46
|
-
get<T>(key: string): Promise<CacheEntry<T> | undefined>;
|
|
47
|
-
/**
|
|
48
|
-
* Store a value in the cache.
|
|
49
|
-
* @param key The key to store the value under.
|
|
50
|
-
* @param value The value to store in the cache.
|
|
51
|
-
* @param ttl The time-to-live for the cache entry in milliseconds.
|
|
52
|
-
*/
|
|
53
|
-
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
54
|
-
/**
|
|
55
|
-
* Clear all entries from the cache.
|
|
56
|
-
*/
|
|
57
|
-
clear(): Promise<void>;
|
|
58
|
-
/**
|
|
59
|
-
* Delete a value from the cache.
|
|
60
|
-
* @param key The key to delete the value for.
|
|
61
|
-
*/
|
|
62
|
-
delete(key: string): Promise<void>;
|
|
63
|
-
/**
|
|
64
|
-
* Check if a value exists in the cache.
|
|
65
|
-
* @param key The key to check for.
|
|
66
|
-
* @returns True if the key exists in the cache, false otherwise.
|
|
67
|
-
*/
|
|
68
|
-
exists(key: string): Promise<boolean>;
|
|
69
|
-
/**
|
|
70
|
-
* Set the time-to-live for a cache entry.
|
|
71
|
-
* @param key The key to set the time-to-live for.
|
|
72
|
-
* @param ttl The time-to-live value in milliseconds.
|
|
73
|
-
*/
|
|
74
|
-
expire(key: string, ttl: number): Promise<void>;
|
|
75
|
-
}
|
|
3
|
+
import { RedisCache } from './cache-storage';
|
|
76
4
|
export declare class RedisPlugin extends RuntimePlugin<RedisOptions> {
|
|
77
5
|
readonly name = "RedisPlugin";
|
|
78
6
|
activate(ctx: CommandKitPluginRuntime): Promise<void>;
|
|
@@ -86,4 +14,4 @@ export declare function redis(options?: RedisOptions): RedisPlugin;
|
|
|
86
14
|
export * from './ratelimit-storage';
|
|
87
15
|
export * from './mutex-storage';
|
|
88
16
|
export * from './semaphore-storage';
|
|
89
|
-
export { RedisCache as RedisCacheProvider };
|
|
17
|
+
export { RedisCache as RedisCacheProvider, RedisCache };
|
package/dist/index.js
CHANGED
|
@@ -14,107 +14,20 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.RedisCache = exports.RedisCacheProvider = exports.RedisPlugin = void 0;
|
|
18
18
|
exports.redis = redis;
|
|
19
|
-
const ioredis_1 = require("ioredis");
|
|
20
19
|
const commandkit_1 = require("commandkit");
|
|
21
20
|
const cache_1 = require("@commandkit/cache");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* new CommandKit({
|
|
26
|
-
* ...
|
|
27
|
-
* cacheProvider: redisCache,
|
|
28
|
-
* });
|
|
29
|
-
*/
|
|
30
|
-
class RedisCache extends cache_1.CacheProvider {
|
|
31
|
-
constructor(redis) {
|
|
32
|
-
super();
|
|
33
|
-
if (redis instanceof ioredis_1.Redis) {
|
|
34
|
-
this.redis = redis;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this.redis = new ioredis_1.Redis(redis ?? {});
|
|
38
|
-
}
|
|
39
|
-
this.serialize = JSON.stringify;
|
|
40
|
-
this.deserialize = JSON.parse;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Retrieve a value from the cache.
|
|
44
|
-
* @param key The key to retrieve the value for.
|
|
45
|
-
* @returns The value stored in the cache, or `undefined` if it does not exist.
|
|
46
|
-
*/
|
|
47
|
-
async get(key) {
|
|
48
|
-
const value = await this.redis.get(key);
|
|
49
|
-
if (value === null) {
|
|
50
|
-
return undefined;
|
|
51
|
-
}
|
|
52
|
-
const entry = this.deserialize(value);
|
|
53
|
-
if (entry.ttl && Date.now() > entry.ttl) {
|
|
54
|
-
await this.delete(key);
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
return entry;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Store a value in the cache.
|
|
61
|
-
* @param key The key to store the value under.
|
|
62
|
-
* @param value The value to store in the cache.
|
|
63
|
-
* @param ttl The time-to-live for the cache entry in milliseconds.
|
|
64
|
-
*/
|
|
65
|
-
async set(key, value, ttl) {
|
|
66
|
-
const entry = {
|
|
67
|
-
value,
|
|
68
|
-
ttl: ttl != null ? Date.now() + ttl : undefined,
|
|
69
|
-
};
|
|
70
|
-
const serialized = this.serialize(entry);
|
|
71
|
-
const finalValue = serialized instanceof Promise ? await serialized : serialized;
|
|
72
|
-
if (typeof ttl === 'number') {
|
|
73
|
-
await this.redis.set(key, finalValue, 'PX', ttl);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
await this.redis.set(key, finalValue);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Clear all entries from the cache.
|
|
81
|
-
*/
|
|
82
|
-
async clear() {
|
|
83
|
-
await this.redis.flushall();
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Delete a value from the cache.
|
|
87
|
-
* @param key The key to delete the value for.
|
|
88
|
-
*/
|
|
89
|
-
async delete(key) {
|
|
90
|
-
await this.redis.del(key);
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Check if a value exists in the cache.
|
|
94
|
-
* @param key The key to check for.
|
|
95
|
-
* @returns True if the key exists in the cache, false otherwise.
|
|
96
|
-
*/
|
|
97
|
-
async exists(key) {
|
|
98
|
-
return Boolean(await this.redis.exists(key));
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Set the time-to-live for a cache entry.
|
|
102
|
-
* @param key The key to set the time-to-live for.
|
|
103
|
-
* @param ttl The time-to-live value in milliseconds.
|
|
104
|
-
*/
|
|
105
|
-
async expire(key, ttl) {
|
|
106
|
-
await this.redis.pexpire(key, ttl);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
exports.RedisCache = RedisCache;
|
|
110
|
-
exports.RedisCacheProvider = RedisCache;
|
|
21
|
+
const cache_storage_1 = require("./cache-storage");
|
|
22
|
+
Object.defineProperty(exports, "RedisCacheProvider", { enumerable: true, get: function () { return cache_storage_1.RedisCache; } });
|
|
23
|
+
Object.defineProperty(exports, "RedisCache", { enumerable: true, get: function () { return cache_storage_1.RedisCache; } });
|
|
111
24
|
class RedisPlugin extends commandkit_1.RuntimePlugin {
|
|
112
25
|
constructor() {
|
|
113
26
|
super(...arguments);
|
|
114
27
|
this.name = 'RedisPlugin';
|
|
115
28
|
}
|
|
116
29
|
async activate(ctx) {
|
|
117
|
-
(0, cache_1.setCacheProvider)(new RedisCache(this.options));
|
|
30
|
+
(0, cache_1.setCacheProvider)(new cache_storage_1.RedisCache(this.options));
|
|
118
31
|
}
|
|
119
32
|
}
|
|
120
33
|
exports.RedisPlugin = RedisPlugin;
|
|
@@ -129,4 +42,4 @@ function redis(options) {
|
|
|
129
42
|
__exportStar(require("./ratelimit-storage"), exports);
|
|
130
43
|
__exportStar(require("./mutex-storage"), exports);
|
|
131
44
|
__exportStar(require("./semaphore-storage"), exports);
|
|
132
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
45
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFrQkEsc0JBRUM7QUFuQkQsMkNBQW9FO0FBQ3BFLDZDQUFxRDtBQUNyRCxtREFBNkM7QUFzQnRCLG1HQXRCZCwwQkFBVSxPQXNCc0I7QUFBRSwyRkF0QmxDLDBCQUFVLE9Bc0JrQztBQXBCckQsTUFBYSxXQUFZLFNBQVEsMEJBQTJCO0lBQTVEOztRQUNrQixTQUFJLEdBQUcsYUFBYSxDQUFDO0lBS3ZDLENBQUM7SUFIUSxLQUFLLENBQUMsUUFBUSxDQUFDLEdBQTRCO1FBQ2hELElBQUEsd0JBQWdCLEVBQUMsSUFBSSwwQkFBVSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDO0lBQ2pELENBQUM7Q0FDRjtBQU5ELGtDQU1DO0FBRUQ7Ozs7R0FJRztBQUNILFNBQWdCLEtBQUssQ0FBQyxPQUFzQjtJQUMxQyxPQUFPLElBQUksV0FBVyxDQUFDLE9BQU8sSUFBSSxFQUFFLENBQUMsQ0FBQztBQUN4QyxDQUFDO0FBRUQsc0RBQW9DO0FBQ3BDLGtEQUFnQztBQUNoQyxzREFBb0MifQ==
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commandkit/redis",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1-dev.20260310005506",
|
|
4
4
|
"description": "Redis cache provider for CommandKit",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
],
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/
|
|
12
|
+
"url": "git+https://github.com/neplextech/commandkit.git",
|
|
13
|
+
"directory": "packages/redis"
|
|
13
14
|
},
|
|
14
15
|
"keywords": [
|
|
15
16
|
"commandkit",
|
|
@@ -22,17 +23,17 @@
|
|
|
22
23
|
],
|
|
23
24
|
"license": "MIT",
|
|
24
25
|
"bugs": {
|
|
25
|
-
"url": "https://github.com/
|
|
26
|
+
"url": "https://github.com/neplextech/commandkit/issues"
|
|
26
27
|
},
|
|
27
|
-
"homepage": "https://
|
|
28
|
+
"homepage": "https://commandkit.dev",
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"ioredis": "^5.
|
|
30
|
+
"ioredis": "^5.10.0"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"typescript": "^5.
|
|
33
|
-
"commandkit": "1.2.
|
|
34
|
-
"tsconfig": "0.0.0-
|
|
35
|
-
"
|
|
33
|
+
"typescript": "^5.9.3",
|
|
34
|
+
"@commandkit/cache": "1.2.1-dev.20260310005506",
|
|
35
|
+
"tsconfig": "0.0.0-dev.20260310005506",
|
|
36
|
+
"commandkit": "1.2.1-dev.20260310005506"
|
|
36
37
|
},
|
|
37
38
|
"scripts": {
|
|
38
39
|
"check-types": "tsc --noEmit",
|