-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
127 lines (98 loc) · 2.96 KB
/
index.js
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
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var inherits = require('util').inherits
var EventEmitter = require('events').EventEmitter
var Cache = require('lru-cache')
var defaultLeveldown
function CacheDOWN (location, leveldown) {
var self = this
if (!(this instanceof CacheDOWN))
return new CacheDOWN(location, leveldown)
if (typeof location === 'object') {
leveldown = location
location = undefined
}
leveldown = leveldown || defaultLeveldown
this._down = new leveldown(location)
this._cache = new Cache()
;['_open', '_close', '_iterator'].forEach(function (method) {
if (method in self._down) {
self[method] = self._down[method].bind(self._down)
}
})
if (typeof location === 'undefined') {
AbstractLevelDOWN.call(this)
} else {
AbstractLevelDOWN.call(this, location)
}
}
inherits(CacheDOWN, AbstractLevelDOWN)
CacheDOWN.prototype._put = function (key, value, options, callback) {
if (typeof value === 'undefined' || value === null) value = ''
this._cachePut(key, value)
return this._down._put.apply(this._down, arguments)
}
CacheDOWN.prototype._get = function (key, options, callback) {
var self = this
if (this._cacheHas(key)) {
var val = this._cacheGet(key)
if (options.asBuffer !== false && !this._isBuffer(val)) {
val = new Buffer(String(val))
}
return process.nextTick(function () {
callback(null, val)
})
}
return this._down._get(key, options, function (err, val) {
if (err) return callback(err)
self._cachePut(key, val)
callback(err, val)
})
}
CacheDOWN.prototype._del = function (key, options, callback) {
this._cacheDel(key)
return this._down._del.apply(this._down, arguments)
}
CacheDOWN.prototype._batch = function (array, options, callback) {
var self = this
array.forEach(function (item) {
if (item.type === 'del') {
self._cacheDel(item.key)
} else if (item.type === 'put') {
self._cachePut(item.key, item.value)
}
})
return this._down._batch.apply(this._down, arguments)
}
CacheDOWN.prototype._isBuffer = function (obj) {
return Buffer.isBuffer(obj)
}
CacheDOWN.prototype.clearCache = function () {
this._cache.reset()
return this
}
CacheDOWN.prototype._cachePut = function (key, value) {
this._cache.set(key, value)
}
CacheDOWN.prototype._cacheGet = function (key) {
return this._cache.get(key)
}
CacheDOWN.prototype._cacheHas = function (key) {
return this._cache.has(key)
}
CacheDOWN.prototype._cacheDel = function (key, value) {
this._cache.del(key)
}
CacheDOWN.prototype.maxSize = function (size) {
this._cache.max = size
return this
}
CacheDOWN.setLeveldown = function (leveldown) {
defaultLeveldown = leveldown
}
module.exports = CacheDOWN
module.exports.factory = function factory () {
var args = Array.prototype.slice.call(arguments)
return function makeCacheDOWN () {
return CacheDOWN.apply(null, args)
}
}