@less-is-more/less-js 1.5.0-2 → 1.5.0-4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@less-is-more/less-js",
3
- "version": "1.5.0-2",
3
+ "version": "1.5.0-4",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/cache.js CHANGED
@@ -40,10 +40,10 @@ module.exports = class Cache {
40
40
  * @param {*} args 直接传arguments,用于拼接缓存key
41
41
  * @param {function} fn 没有缓存的实现
42
42
  * @param {boolean} zip 是否压缩
43
- * @param {boolean} nas 是否使用Nas缓存
43
+ * @param {boolean} useNas 是否使用Nas缓存
44
44
  * @returns 优先返回对象
45
45
  */
46
- static async auto(keyPrefix, timeSecond, args, fn, zip = false, nas = false) {
46
+ static async auto(keyPrefix, timeSecond, args, fn, zip = false, useNas = false) {
47
47
  let fullKey = keyPrefix;
48
48
  for (let i = 0; i < args.length; i++) {
49
49
  fullKey += ":" + args[i].toString();
@@ -52,7 +52,7 @@ module.exports = class Cache {
52
52
  let savedData = Cache._getLocal(fullKey);
53
53
  let hasLocal = false;
54
54
  if (Param.isBlank(savedData)) {
55
- if (nas) {
55
+ if (useNas) {
56
56
  savedData = Nas.get(fullKey);
57
57
  } else {
58
58
  savedData = await Redis.exec("get", fullKey);
@@ -82,7 +82,7 @@ module.exports = class Cache {
82
82
  if (Cache._needZip(redisContent, zip)) {
83
83
  redisContent = Cache._zip(redisContent);
84
84
  }
85
- if (nas) {
85
+ if (useNas) {
86
86
  Nas.set(fullKey, result, timeSecond);
87
87
  } else {
88
88
  await Redis.exec("setex", fullKey, timeSecond + "", redisContent);
package/src/nas.js CHANGED
@@ -101,7 +101,13 @@ class Nas {
101
101
  if (!isNaN(expireTime)) {
102
102
  if (expireTime === -1 || Date.now() < expireTime) {
103
103
  // 文件未过期,返回值
104
- return Buffer.from(value, "base64").toString("utf8");
104
+ let valueStr = Buffer.from(value, "base64").toString("utf8");
105
+ try {
106
+ return JSON.parse(valueStr);
107
+ } catch (e) {
108
+ // 如果解析JSON失败,则返回原始字符串
109
+ return valueStr;
110
+ }
105
111
  } else {
106
112
  // 文件已过期,删除文件
107
113
  Nas._deleteCacheFile(key);
@@ -124,7 +130,8 @@ class Nas {
124
130
  */
125
131
  static _saveToDisk(key, value, expireTime) {
126
132
  // 将值转换为字符串,如果是对象则转换为JSON字符串
127
- const content = Buffer.from(value, "utf8").toString("base64");
133
+ const valueStr = typeof value === "object" ? JSON.stringify(value) : value;
134
+ const content = Buffer.from(valueStr, "utf8").toString("base64");
128
135
  const filePath = Nas._getFilePath(key);
129
136
 
130
137
  try {
package/test/test-nas.js CHANGED
@@ -8,6 +8,20 @@ describe("Nas Class Tests", function () {
8
8
  assert.equal(value, "testValue");
9
9
  });
10
10
 
11
+ it("should set and get a string object", function () {
12
+ const target = [
13
+ {
14
+ id: 128910,
15
+ supplierCode: "9c6d1ab7ff7a440eba1516b164c06632",
16
+ status: 1,
17
+ city: "",
18
+ },
19
+ ];
20
+ Nas.set("testObject", target);
21
+ const value = Nas.get("testObject");
22
+ assert.deepStrictEqual(value, target);
23
+ });
24
+
11
25
  it("should handle expiration correctly", function (done) {
12
26
  Nas.set("expiringKey", "expiringValue", 1); // 1秒后过期
13
27