node-karin 1.11.1 → 1.11.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # 更新日志
2
2
 
3
+ ## [1.11.2](https://github.com/KarinJS/Karin/compare/core-v1.11.1...core-v1.11.2) (2025-10-07)
4
+
5
+
6
+ ### 🐛 Bug Fixes
7
+
8
+ * 修复 Redis Mock 客户端缺少 setEx 等常用方法的问题 ([#543](https://github.com/KarinJS/Karin/issues/543)) ([3797b4f](https://github.com/KarinJS/Karin/commit/3797b4f7721df1428f0f5b068fb77fda017a3792))
9
+
3
10
  ## [1.11.1](https://github.com/KarinJS/Karin/compare/core-v1.11.0...core-v1.11.1) (2025-10-02)
4
11
 
5
12
 
package/dist/index.mjs CHANGED
@@ -20497,6 +20497,83 @@ var init_mock = __esm({
20497
20497
  return this.#str[key].toString();
20498
20498
  }
20499
20499
  }
20500
+ /**
20501
+ * @description 设置键值对并指定过期时间(秒)
20502
+ * @param key 键
20503
+ * @param seconds 过期时间(秒)
20504
+ * @param value 值
20505
+ */
20506
+ async setEx(key, seconds, value) {
20507
+ return await this.set(key, value, { EX: seconds });
20508
+ }
20509
+ /**
20510
+ * @description 设置键值对并指定过期时间(毫秒)
20511
+ * @param key 键
20512
+ * @param milliseconds 过期时间(毫秒)
20513
+ * @param value 值
20514
+ */
20515
+ async pSetEx(key, milliseconds, value) {
20516
+ return await this.set(key, value, { PX: milliseconds });
20517
+ }
20518
+ /**
20519
+ * @description 仅当键不存在时设置键值对
20520
+ * @param key 键
20521
+ * @param value 值
20522
+ */
20523
+ async setNX(key, value) {
20524
+ if (this.store[key] && !this.checkExpire(key)) {
20525
+ return false;
20526
+ }
20527
+ await this.set(key, value, { NX: true });
20528
+ return true;
20529
+ }
20530
+ /**
20531
+ * @description 获取键值并设置过期时间
20532
+ * @param key 键
20533
+ * @param options 过期时间选项(EX: 秒, PX: 毫秒, EXAT: 秒级时间戳, PXAT: 毫秒级时间戳)
20534
+ */
20535
+ async getEx(key, options) {
20536
+ const value = await this.get(key);
20537
+ if (value === null) return null;
20538
+ if (options?.PERSIST) {
20539
+ if (this.store[key]) {
20540
+ this.store[key].expire = -1;
20541
+ const { type } = this.store[key];
20542
+ const currentValue = this.getValueStringByKey(key);
20543
+ this.#sqlite.set(key, currentValue, type, -1);
20544
+ }
20545
+ } else if (options?.EX !== void 0) {
20546
+ await this.expire(key, options.EX);
20547
+ } else if (options?.PX !== void 0) {
20548
+ const expire = moment().add(options.PX, "milliseconds").valueOf();
20549
+ this.store[key].expire = expire;
20550
+ const { type } = this.store[key];
20551
+ const currentValue = this.getValueStringByKey(key);
20552
+ this.#sqlite.set(key, currentValue, type, expire);
20553
+ } else if (options?.EXAT !== void 0) {
20554
+ this.store[key].expire = options.EXAT * 1e3;
20555
+ const { type } = this.store[key];
20556
+ const currentValue = this.getValueStringByKey(key);
20557
+ this.#sqlite.set(key, currentValue, type, options.EXAT * 1e3);
20558
+ } else if (options?.PXAT !== void 0) {
20559
+ this.store[key].expire = options.PXAT;
20560
+ const { type } = this.store[key];
20561
+ const currentValue = this.getValueStringByKey(key);
20562
+ this.#sqlite.set(key, currentValue, type, options.PXAT);
20563
+ }
20564
+ return value;
20565
+ }
20566
+ /**
20567
+ * @description 获取键值并删除键
20568
+ * @param key 键
20569
+ */
20570
+ async getDel(key) {
20571
+ const value = await this.get(key);
20572
+ if (value !== null) {
20573
+ await this.del(key);
20574
+ }
20575
+ return value;
20576
+ }
20500
20577
  /**
20501
20578
  * @description 删除键
20502
20579
  * @param key 键
@@ -20527,6 +20604,53 @@ var init_mock = __esm({
20527
20604
  this.#sqlite.expire(key, expire);
20528
20605
  return 1;
20529
20606
  }
20607
+ /**
20608
+ * @description 设置键的过期时间戳(秒)
20609
+ * @param key 键
20610
+ * @param timestamp 过期时间戳(秒)
20611
+ */
20612
+ async expireAt(key, timestamp) {
20613
+ if (!this.store[key]) return 0;
20614
+ this.store[key].expire = timestamp * 1e3;
20615
+ this.#sqlite.expire(key, timestamp * 1e3);
20616
+ return 1;
20617
+ }
20618
+ /**
20619
+ * @description 设置键的过期时间(毫秒)
20620
+ * @param key 键
20621
+ * @param milliseconds 过期时间(毫秒)
20622
+ */
20623
+ async pExpire(key, milliseconds) {
20624
+ if (!this.store[key]) return 0;
20625
+ const expire = moment().add(milliseconds, "milliseconds").valueOf();
20626
+ this.store[key].expire = expire;
20627
+ this.#sqlite.expire(key, expire);
20628
+ return 1;
20629
+ }
20630
+ /**
20631
+ * @description 设置键的过期时间戳(毫秒)
20632
+ * @param key 键
20633
+ * @param timestamp 过期时间戳(毫秒)
20634
+ */
20635
+ async pExpireAt(key, timestamp) {
20636
+ if (!this.store[key]) return 0;
20637
+ this.store[key].expire = timestamp;
20638
+ this.#sqlite.expire(key, timestamp);
20639
+ return 1;
20640
+ }
20641
+ /**
20642
+ * @description 移除键的过期时间
20643
+ * @param key 键
20644
+ */
20645
+ async persist(key) {
20646
+ if (!this.store[key]) return 0;
20647
+ if (this.store[key].expire === -1) return 0;
20648
+ this.store[key].expire = -1;
20649
+ const { type } = this.store[key];
20650
+ const currentValue = this.getValueStringByKey(key);
20651
+ this.#sqlite.set(key, currentValue, type, -1);
20652
+ return 1;
20653
+ }
20530
20654
  /**
20531
20655
  * @description 获取键的过期时间
20532
20656
  * @param key 键
@@ -20537,6 +20661,100 @@ var init_mock = __esm({
20537
20661
  if (this.checkExpire(key)) return -2;
20538
20662
  return moment(this.store[key].expire).diff(moment(), "seconds");
20539
20663
  }
20664
+ /**
20665
+ * @description 获取键的过期时间(毫秒)
20666
+ * @param key 键
20667
+ */
20668
+ async pTTL(key) {
20669
+ if (!this.store[key]) return -2;
20670
+ if (this.store[key].expire === -1) return -1;
20671
+ if (this.checkExpire(key)) return -2;
20672
+ return moment(this.store[key].expire).diff(moment(), "milliseconds");
20673
+ }
20674
+ /**
20675
+ * @description 获取字符串长度
20676
+ * @param key 键
20677
+ */
20678
+ async strLen(key) {
20679
+ if (!this.store[key]) return 0;
20680
+ if (this.checkExpire(key)) return 0;
20681
+ const { type } = this.store[key];
20682
+ if (type === "str" /* STR */) {
20683
+ return this.#str[key].length;
20684
+ }
20685
+ return 0;
20686
+ }
20687
+ /**
20688
+ * @description 重命名键
20689
+ * @param key 原键名
20690
+ * @param newKey 新键名
20691
+ */
20692
+ async rename(key, newKey) {
20693
+ if (!this.store[key]) throw new Error("no such key");
20694
+ const { type, expire } = this.store[key];
20695
+ const value = this.getValueStringByKey(key);
20696
+ this.#del(key);
20697
+ this.store[newKey] = { type, expire };
20698
+ switch (type) {
20699
+ case "str" /* STR */:
20700
+ this.#str[newKey] = value;
20701
+ break;
20702
+ case "num" /* NUM */:
20703
+ this.#num[newKey] = Number(value);
20704
+ break;
20705
+ case "hash" /* HASH */:
20706
+ this.#hash[newKey] = JSON.parse(value);
20707
+ break;
20708
+ case "list" /* LIST */:
20709
+ this.#list[newKey] = JSON.parse(value);
20710
+ break;
20711
+ case "set" /* SET */:
20712
+ this.#set[newKey] = new Set(JSON.parse(value));
20713
+ break;
20714
+ case "zset" /* ZSET */:
20715
+ this.#zset[newKey] = JSON.parse(value);
20716
+ break;
20717
+ case "pf" /* PF */:
20718
+ this.#pf[newKey] = new Set(JSON.parse(value));
20719
+ break;
20720
+ case "bit" /* BIT */:
20721
+ this.#bit[newKey] = Buffer.from(value, "base64");
20722
+ break;
20723
+ }
20724
+ this.#sqlite.set(newKey, value, type, expire);
20725
+ return "OK";
20726
+ }
20727
+ /**
20728
+ * @description 仅当新键不存在时重命名键
20729
+ * @param key 原键名
20730
+ * @param newKey 新键名
20731
+ */
20732
+ async renameNX(key, newKey) {
20733
+ if (!this.store[key]) return 0;
20734
+ if (this.store[newKey] && !this.checkExpire(newKey)) return 0;
20735
+ await this.rename(key, newKey);
20736
+ return 1;
20737
+ }
20738
+ /**
20739
+ * @description 返回数据库中键的数量
20740
+ */
20741
+ async dbSize() {
20742
+ const keys = Object.keys(this.store);
20743
+ keys.forEach((key) => this.checkExpire(key));
20744
+ return Object.keys(this.store).length;
20745
+ }
20746
+ /**
20747
+ * @description 从数据库中随机返回一个键
20748
+ */
20749
+ async randomKey() {
20750
+ const keys = Object.keys(this.store);
20751
+ if (keys.length === 0) return null;
20752
+ keys.forEach((key) => this.checkExpire(key));
20753
+ const validKeys = Object.keys(this.store);
20754
+ if (validKeys.length === 0) return null;
20755
+ const randomIndex = Math.floor(Math.random() * validKeys.length);
20756
+ return validKeys[randomIndex];
20757
+ }
20540
20758
  /**
20541
20759
  * @description 获取所有键
20542
20760
  * @param pattern 匹配规则
@@ -20584,6 +20802,40 @@ var init_mock = __esm({
20584
20802
  this.#sqlite.set(key, String(this.#num[key]), "num" /* NUM */, this.store[key].expire);
20585
20803
  return this.#num[key];
20586
20804
  }
20805
+ /**
20806
+ * @description 自增指定值
20807
+ * @param key 键
20808
+ * @param increment 增量
20809
+ */
20810
+ async incrBy(key, increment) {
20811
+ if (!this.#num[key]) {
20812
+ this.#num[key] = 0;
20813
+ this.store[key] = { type: "num" /* NUM */, expire: -1 };
20814
+ } else if (this.checkExpire(key, false)) {
20815
+ this.store[key].expire = -1;
20816
+ this.#num[key] = 0;
20817
+ }
20818
+ this.#num[key] += increment;
20819
+ this.#sqlite.set(key, String(this.#num[key]), "num" /* NUM */, this.store[key].expire);
20820
+ return this.#num[key];
20821
+ }
20822
+ /**
20823
+ * @description 自增指定浮点值
20824
+ * @param key 键
20825
+ * @param increment 增量(浮点数)
20826
+ */
20827
+ async incrByFloat(key, increment) {
20828
+ if (!this.#num[key]) {
20829
+ this.#num[key] = 0;
20830
+ this.store[key] = { type: "num" /* NUM */, expire: -1 };
20831
+ } else if (this.checkExpire(key, false)) {
20832
+ this.store[key].expire = -1;
20833
+ this.#num[key] = 0;
20834
+ }
20835
+ this.#num[key] += increment;
20836
+ this.#sqlite.set(key, String(this.#num[key]), "num" /* NUM */, this.store[key].expire);
20837
+ return this.#num[key];
20838
+ }
20587
20839
  /**
20588
20840
  * @description 自减
20589
20841
  * @param key 键
@@ -20600,6 +20852,23 @@ var init_mock = __esm({
20600
20852
  this.#sqlite.set(key, String(this.#num[key]), "num" /* NUM */, this.store[key].expire);
20601
20853
  return this.#num[key];
20602
20854
  }
20855
+ /**
20856
+ * @description 自减指定值
20857
+ * @param key 键
20858
+ * @param decrement 减量
20859
+ */
20860
+ async decrBy(key, decrement) {
20861
+ if (!this.#num[key]) {
20862
+ this.#num[key] = 0;
20863
+ this.store[key] = { type: "num" /* NUM */, expire: -1 };
20864
+ } else if (this.checkExpire(key, false)) {
20865
+ this.store[key].expire = -1;
20866
+ this.#num[key] = 0;
20867
+ }
20868
+ this.#num[key] -= decrement;
20869
+ this.#sqlite.set(key, String(this.#num[key]), "num" /* NUM */, this.store[key].expire);
20870
+ return this.#num[key];
20871
+ }
20603
20872
  /**
20604
20873
  * @description 追加字符串
20605
20874
  * @param key 键
@@ -20909,46 +21178,6 @@ var init_mock = __esm({
20909
21178
  if (this.checkExpire(key)) return 0;
20910
21179
  return this.#pf[key].size;
20911
21180
  }
20912
- /**
20913
- * 合并多个 HyperLogLog
20914
- * @param destKey 目标 HyperLogLog 的键
20915
- * @param sourceKeys 源 HyperLogLog 的键
20916
- * @returns 返回 1 表示合并成功,0 表示合并失败
20917
- */
20918
- async pExpire(key, seconds) {
20919
- if (!this.#pf[key]) return false;
20920
- this.store[key].expire = moment().add(seconds, "seconds").valueOf();
20921
- this.#sqlite.set(key, JSON.stringify(this.#pf[key]), "pf" /* PF */, this.store[key].expire);
20922
- return true;
20923
- }
20924
- /**
20925
- * 设置 HyperLogLog 的过期时间
20926
- * @param key HyperLogLog 的键
20927
- * @param seconds 过期时间(秒)
20928
- * @returns 返回 1 表示设置成功,0 表示设置失败
20929
- */
20930
- async pTTL(key) {
20931
- if (!this.#pf[key]) return -2;
20932
- if (this.store[key].expire === -1) return -1;
20933
- if (this.checkExpire(key)) return -2;
20934
- const ttl = moment(this.store[key].expire).diff(moment(), "seconds");
20935
- this.store[key].expire = ttl;
20936
- this.#sqlite.set(key, JSON.stringify(this.#pf[key]), "pf" /* PF */, this.store[key].expire);
20937
- return ttl;
20938
- }
20939
- /**
20940
- * 为键设置到某个特定时间点的过期时间
20941
- * @param key HyperLogLog 的键
20942
- * @param seconds 过期时间(毫秒)
20943
- * @returns 返回布尔值
20944
- */
20945
- async pExpireAt(key, timestamp) {
20946
- if (!this.#pf[key]) return false;
20947
- if (this.checkExpire(key)) return false;
20948
- this.store[key].expire = timestamp;
20949
- this.#sqlite.set(key, JSON.stringify(this.#pf[key]), "pf" /* PF */, this.store[key].expire);
20950
- return true;
20951
- }
20952
21181
  /**
20953
21182
  * @description 发布消息到频道
20954
21183
  * @param channel 频道
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "1.11.1",
3
+ "version": "1.11.2",
4
4
  "description": "Lightweight, efficient, concise, and stable robot framework.",
5
5
  "keywords": [
6
6
  "node",