cdk
2025-03-24 06e6cf5719a7de9d7919cee2f6fcc3cfc6f9eb9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const db = uniCloud.database()
 
function getType(value) {
  return Object.prototype.toString.call(value).slice(8, -1).toLowerCase()
}
 
const validator = {
  key: function(value) {
    const err = new Error('Invalid key')
    if (typeof value !== 'string') {
      throw err
    }
    const valueTrim = value.trim()
    if (!valueTrim || valueTrim !== value) {
      throw err
    }
  },
  value: function(value) {
    // 仅作简单校验
    const type = getType(value)
    const validValueType = ['null', 'number', 'string', 'array', 'object']
    if (validValueType.indexOf(type) === -1) {
      throw new Error('Invalid value type')
    }
  },
  duration: function(value) {
    const err = new Error('Invalid duration')
    if (value === undefined) {
      return
    }
    if (typeof value !== 'number' || value === 0) {
      throw err
    }
  }
}
 
/**
 * 入库时 expired 为过期时间对应的时间戳,永不过期用-1表示
 * 返回结果时 与redis对齐,-1表示永不过期,-2表示已过期或不存在
 */
class DatabaseCache {
  constructor({
    collection = 'opendb-open-data'
  } = {}) {
    this.type = 'db'
    this.collection = db.collection(collection)
  }
 
  _serializeValue(value) {
    return value === undefined ? null : JSON.stringify(value)
  }
 
  _deserializeValue(value) {
    return value ? JSON.parse(value) : value
  }
 
  async set(key, value, duration) {
    validator.key(key)
    validator.value(value)
    validator.duration(duration)
    value = this._serializeValue(value)
    await this.collection.doc(key).set({
      value,
      expired: duration && duration !== -1 ? Date.now() + (duration * 1000) : -1
    })
  }
 
  async _getWithDuration(key) {
    const getKeyRes = await this.collection.doc(key).get()
    const record = getKeyRes.data[0]
    if (!record) {
      return {
        value: null,
        duration: -2
      }
    }
    const value = this._deserializeValue(record.value)
    const expired = record.expired
    if (expired === -1) {
      return {
        value,
        duration: -1
      }
    }
    const duration = expired - Date.now()
    if (duration <= 0) {
      await this.remove(key)
      return {
        value: null,
        duration: -2
      }
    }
    return {
      value,
      duration: Math.floor(duration / 1000)
    }
  }
 
  async get(key, {
    withDuration = true
  } = {}) {
    const result = await this._getWithDuration(key)
    if (!withDuration) {
      delete result.duration
    }
    return result
  }
 
  async remove(key) {
    await this.collection.doc(key).remove()
  }
}
 
class RedisCache {
  constructor() {
    this.type = 'redis'
    this.redis = uniCloud.redis()
  }
 
  _serializeValue(value) {
    return value === undefined ? null : JSON.stringify(value)
  }
 
  _deserializeValue(value) {
    return value ? JSON.parse(value) : value
  }
 
  async set(key, value, duration) {
    validator.key(key)
    validator.value(value)
    validator.duration(duration)
    value = this._serializeValue(value)
    if (!duration || duration === -1) {
      await this.redis.set(key, value)
    } else {
      await this.redis.set(key, value, 'EX', duration)
    }
  }
 
  async get(key, {
    withDuration = false
  } = {}) {
    let value = await this.redis.get(key)
    value = this._deserializeValue(value)
    if (!withDuration) {
      return {
        value
      }
    }
    const durationSecond = await this.redis.ttl(key)
    let duration
    switch (durationSecond) {
      case -1:
        duration = -1
        break
      case -2:
        duration = -2
        break
      default:
        duration = durationSecond
        break
    }
    return {
      value,
      duration
    }
  }
 
  async remove(key) {
    await this.redis.del(key)
  }
}
 
class Cache {
  constructor({
    type,
    collection
  } = {}) {
    if (type === 'database') {
      return new DatabaseCache({
        collection
      })
    } else if (type === 'redis') {
      return new RedisCache()
    } else {
      throw new Error('Invalid cache type')
    }
  }
}
 
class CacheKey {
  constructor({
    type,
    collection,
    cache,
    key,
    fallback
  } = {}) {
    this.cache = cache || new Cache({
      type,
      collection
    })
    this.key = key
    this.fallback = fallback
  }
 
  async set(value, duration) {
    await this.cache.set(this.key, value, duration)
  }
 
  async setWithSync(value, duration, syncMethod) {
    await Promise.all([
      this.set(this.key, value, duration),
      syncMethod(value, duration)
    ])
  }
 
  async get() {
    let {
      value,
      duration
    } = await this.cache.get(this.key)
    if (value !== null && value !== undefined) {
      return {
        value,
        duration
      }
    }
    if (!this.fallback) {
      return {
        value: null,
        duration: -2
      }
    }
    const fallbackResult = await this.fallback()
    value = fallbackResult.value
    duration = fallbackResult.duration
    if (value !== null && duration !== undefined) {
      await this.cache.set(this.key, value, duration)
    }
    return {
      value,
      duration
    }
  }
 
  async remove() {
    await this.cache.remove(this.key)
  }
}
 
class CacheKeyCascade {
  constructor({
    layers, // [{cache, type, collection, key}] 从低级到高级排序,[DbCacheKey, RedisCacheKey]
    fallback
  } = {}) {
    this.layers = layers
    this.cacheLayers = []
    let lastCacheKey
    for (let i = 0; i < layers.length; i++) {
      const {
        type,
        cache,
        collection,
        key
      } = layers[i]
      const lastCacheKeyTemp = lastCacheKey
      try {
        const currentCacheKey = new CacheKey({
          type,
          collection,
          cache,
          key,
          fallback: i === 0 ? fallback : function() {
            return lastCacheKeyTemp.get()
          }
        })
        this.cacheLayers.push(currentCacheKey)
        lastCacheKey = currentCacheKey
      } catch (e) {}
    }
    this.highLevelCache = lastCacheKey
  }
 
  async set(value, duration) {
    return Promise.all(
      this.cacheLayers.map(item => {
        return item.set(value, duration)
      })
    )
  }
 
  async setWithSync(value, duration, syncMethod) {
    const setPromise = this.cacheLayers.map(item => {
      return item.set(value, duration)
    })
    return Promise.all(
      [
        ...setPromise,
        syncMethod(value, duration)
      ]
    )
  }
 
  async get() {
    return this.highLevelCache.get()
  }
 
  async remove() {
    await Promise.all(
      this.cacheLayers.map(cacheKeyItem => {
        return cacheKeyItem.remove()
      })
    )
  }
}
 
module.exports = {
  Cache,
  DatabaseCache,
  RedisCache,
  CacheKey,
  CacheKeyCascade
}