-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcacheAdapter.js
More file actions
158 lines (138 loc) · 4.37 KB
/
cacheAdapter.js
File metadata and controls
158 lines (138 loc) · 4.37 KB
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
const { promiseAll, getSeconds } = require('./utils')
const kAdapter = Symbol('adapter')
const Promise = require("bluebird")
let kIncrementOrDecrement = Symbol('incrementOrDecrement')
const kEnabled = Symbol('enabled')
class Cache {
[kAdapter];
[kEnabled];
constructor($adapter, $enabled = true) {
this[kAdapter] = $adapter;
this[kEnabled] = $enabled;
}
async has(key) {
if (this[kEnabled] == false) {
return false
}
const data = await this.get(key);
return Boolean(typeof data !== undefined && data) || false
}
async get(key, defaultValue = null) {
if (this[kEnabled] == false) {
return null
}
return this[kAdapter].get(key).then(result => result.value)
.catch(err => defaultValue)
}
async put(key, value, seconds = 0) {
if (this[kEnabled] == false) {
return true
}
seconds = getSeconds(seconds)
if (isNaN(seconds)) {
seconds = 0
}
return this[kAdapter].put(key, value, seconds)
.then(result => {
if (result == true)
return true
else
return Promise.reject(false)
})
.catch(err => false)
}
async pull(key, defaultValue = null) {
return this.get(key, defaultValue).then(async (value) => {
return this.forget(key).then(res => value).catch(err => null)
})
}
async many(keys) {
return Promise.all(keys.map(key => this.get(key)))
.then(values => {
let mappedValues = {}
for (let i = 0; i < keys.length; i++) {
mappedValues[keys[i]] = values[i]
}
return mappedValues
})
}
async putMany(object = {}, seconds) {
for (let prop in object) {
object[prop] = this.put(prop, object[prop], getSeconds(seconds));
}
return promiseAll(object)
}
async [kIncrementOrDecrement](key, value, callback) {
return this[kAdapter].get(key).then((result) => {
if (Date.now() / 1000 >= Number(result.expiration)) {
this.forget(key)
return false
}
if (result.value === undefined) {
return false
}
const currentValue = parseInt(result.value)
if (isNaN(currentValue)) {
return false
}
const newValue = callback(currentValue)
return this.put(key, newValue, result.expiration)
.then(result => newValue)
.catch(err => false)
})
.catch(err => false)
}
async increment(key, value = 1) {
return this[kIncrementOrDecrement](key, value, (currentValue) => {
return currentValue + value
})
.catch(err => false)
}
async decrement(key, value = 1) {
return this[kIncrementOrDecrement](key, value, (currentValue) => {
return currentValue - value
})
.catch(err => false)
}
async forever(key, value) {
return this.put(key, value, Infinity)
}
async remember(key, seconds = 0, cb) {
if (typeof cb != 'function') {
throw Error('Callback is required.')
}
return this.get(key).then(async res => {
if (res == null) {
if (cb.length == 1) {
res = Promise.fromCallback(cb)
} else {
res = cb()
}
res = await Promise.resolve(res)
if (res) {
await this.put(key, res, getSeconds(seconds))
}
}
return res
})
}
async rememberForever(key, cb) {
if (typeof cb != 'function') {
throw Error('Callback should be required.')
}
return this.remember(key, Infinity, cb)
}
async forget(key) {
if (this[kEnabled] == false) {
return true
}
return this[kAdapter].forget(key).then(res => true).catch(err => false)
}
async flush() {
if (this[kEnabled] == false) {
return true
}
return this[kAdapter].flush().then(res => true).catch(err => false)
}
}
module.exports = Cache