/
lodis.coffee
347 lines (267 loc) · 8.57 KB
/
lodis.coffee
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
class @Lodis
constructor: ->
this.storage = window.localStorage
_pack: (value) -> JSON.stringify value
_unpack: (value) -> JSON.parse value
_expire_key: (key) =>
this.del(key)
delete this._expiration_hash[key]
_get_set_or_default: (key, default_value) ->
this._unpack(this.get(key)) or default_value
_get_set: (key) ->
this._get_set_or_default(key, [])
_get_hash: (key) ->
this._get_set_or_default(key, {})
_set_packed: (key, value) ->
value = this._pack(value)
this.set(key, value)
_get_from_hash: (key, options = with_keys: true, with_values: true, only: []) ->
hash = this._get_hash(key)
result = []
for key, value of hash
result.push key if options["with_keys"]
result.push value if options["with_values"] or ( options["only"] and key in options["only"])
result
_alter_int_value: (key, quantity) ->
if this.exists(key)
value = parseInt this.get(key)
if typeof value is "number"
value = value + quantity
this.set(key, value)
value
else
throw new Error
_expiration_hash: {}
del: (keys...) ->
this.storage.removeItem(key) for key in keys
true
set: (key, value) ->
this.storage.setItem(key, value)
true
get: (key) ->
this.storage.getItem(key)
exists: (key) ->
this.get(key)?
dbsize: -> this.storage.length
keys: (regexp) ->
found_keys = []
for i in [0..this.dbsize() - 1]
key = this.storage.key(i)
found_keys.push key if key.match(regexp)
found_keys
expire: (key, seconds) ->
miliseconds = seconds*1000
timeout_id = setTimeout this._expire_key, miliseconds, key
this._expiration_hash[key] = id: timeout_id, timeout: new Date().getTime() + miliseconds
true
expireat: (key, miliseconds) ->
return false if (miliseconds < new Date().getTime()) or !this.exists(key)
seconds = (miliseconds - new Date().getTime()) / 1000
this.expire key, seconds
true
ttl: (key) ->
if this.exists(key)
if this._expiration_hash[key]
Math.floor((this._expiration_hash[key].timeout - new Date().getTime()) / 1000)
else
-1
append: (key, value) ->
this.set(key, "#{this.get(key)}#{value}") if this.exists(key)
auth: (password) ->
true # this should do someting?
bgrewriteaof: -> true
bgsave: -> true
blpop: ->
lrange: (key, start, end) ->
end += 1
set = this._get_set(key)
end = set.length + end if end < 1
result = set.slice(start, end)
result = [result] if result.constructor is String
result
lpush: (key, item) ->
set = this._get_set(key)
set.unshift(item)
this._set_packed(key, set)
rpush: (key, item) ->
set = this._get_set(key)
set.push(item)
this._set_packed(key, set)
rpushx: (key, item) ->
this.rpush(key, item) if this.exists(key)
decr: (key) ->
this.decrby(key, 1)
incr: (key) ->
this.incrby(key, 1)
decrby: (key, quantity = 1) ->
this._alter_int_value(key, -quantity)
incrby: (key, quantity = 1) ->
this._alter_int_value(key, quantity)
echo: (message) -> message
flushall: ->
this.storage.clear()
flushdb: ->
this.flushall()
getrange: (key, start, end) ->
if this.exists(key)
string = this.get(key)
start = string.length + start if start < 0
end = string.length + end if end < 0
string.substr start, end + 1
getset: (key, value) ->
if this.exists(key)
old_value = this.get(key)
this.set(key, value)
old_value
hset: (hash_key, key, value) ->
hash = this._get_hash(hash_key)
hash[key] = value
this._set_packed(hash_key, hash)
true
hget: (hash_key, key) ->
if this.exists(hash_key)
hash = this._get_hash(hash_key)
hash[key]
hgetall: (hash_key) ->
this._get_from_hash(hash_key) if this.exists(hash_key)
hexists: (hash_key, key) ->
this.hget(hash_key, key)?
hkeys: (hash_key) ->
this._get_from_hash(hash_key, with_keys: true, with_values: false) if this.exists(hash_key)
hlen: (hash_key) ->
this.hkeys(hash_key).length if this.exists(hash_key)
hincrby: (hash_key, key, quantity) ->
if this.hexists(hash_key, key)
old_value = parseInt this.hget(hash_key, key)
if typeof old_value is "number"
new_value = old_value + quantity
this.hset(hash_key, key, new_value)
new_value
else
throw new Error("Invalid type")
hmget: (hash_key, keys...) ->
this._get_from_hash(hash_key, with_values: true, with_keys: false, only: keys) if this.exists(hash_key)
hmset: (hash_key, keys_and_values...) ->
result = {}
result[keys_and_values[i-1]] = value for i, value of keys_and_values when i % 2
this._set_packed(hash_key, result)
hsetnx: (hash_key, key, value) ->
if !this.exists(hash_key)
this.hset(hash_key, key, value)
true
else
false
hvals: (hash_key) ->
this._get_from_hash(hash_key, with_keys: false, with_values: true) if this.exists(hash_key)
lindex: (key, index) ->
if this.exists(key)
hash = this._get_set(key)
index = hash.length + index if index < 0
hash[index] or false
linsert: (key, direction, reference_value, value) ->
if this.exists(key)
direction = switch direction.toUpperCase()
when "BEFORE" then -1
when "AFTER" then 1
set = this._get_set(key)
reference_value = set.indexOf(reference_value) + direction
[left_side, right_side] = [set[0...reference_value], set[reference_value..-1]]
result = left_side.concat([value])
result = result.concat(right_side)
this._set_packed(key, result)
llen: (key) ->
if this.exists(key)
set = this._get_set(key)
set.length
lpop: (key) ->
if this.exists(key)
set = this._get_set(key)
value = set[1..-1]
this._set_packed(key, value)
lpushx: (key, value) ->
if this.exists(key)
this.lpush(key, value)
else
false
lrem: (key, count, item) ->
if this.exists(key)
quantity = Math.abs(count)
set = this._get_set(key)
set = set.reverse() if count < 0
result = []
for value in set
if value == item and quantity > 0
quantity -= 1
else
result.push value
result = result.reverse() if count < 0
this._set_packed(key, result)
lset: (key, index, value) ->
if this.exists(key)
set = this._get_set(key)
index = set.length + index if index < 0
set[index] = value
this._set_packed(key, set)
ltrim: (key, start, end) ->
if this.exists(key)
set = this._get_set(key)
end = set.length + end if end < 0
result = set[start..end]
this._set_packed(key, result)
mget: (keys...) ->
result = []
result.push this.get(key) for key in keys
result
mset: (keys_and_values...) ->
this.set(keys_and_values[i-1], value) for i, value of keys_and_values when i % 2
msetnx: (keys_and_values...) ->
return for i, key_or_value of keys_and_values when (!(i % 2) and this.exists(key_or_value))
this.mset(keys_and_values...)
persist: (key) ->
if this.exists(key)
if this._expiration_hash[key]
clearTimeout this._expiration_hash[key].id
delete this._expiration_hash[key]
ping: -> "PONG"
randomkey: ->
keys = this.keys()
keys[Math.floor(Math.random() * keys.length)]
rename: (key, new_key) ->
value = this.get(key)
this.del(key)
this.set(new_key, value)
renamenx: (key, new_key) ->
this.rename(key, new_key) if !this.exists(new_key)
rpop: (key) ->
if this.exists(key)
set = this._get_set(key)
value = set.pop()
this._set_packed(key, set)
value
rpoplpush: (hash_key, other_hash_key) ->
if this.exists(hash_key)
value = this.rpop(hash_key)
this.lpush(other_hash_key, value)
value
sadd: (key, members...) ->
set = this._get_set(key)
this.lpush(key, member) for member in members when member not in set
smembers: (key) ->
this._get_set(key) if this.exists(key)
save: -> true # ?
scard: (key) ->
this._get_set(key).length
sdiff: (keys...) ->
[head, tail...] = keys
set = this._get_set(head)
other_set = result = []
other_set = other_set.concat(this._get_set(key)) for key in tail
result.push value for value in set when value not in other_set
result.reverse()
sdiffstore: (destination, keys...) ->
this._set_packed(destination, this.sdiff(keys...))
select: (db) -> db is 0
setex: (key, expire, value) ->
this.set(key, value)
this.expire(key, expire)
setnx: (key, value) -> this.set(key, value) if !this.exists(key)