forked from kriszyp/lmdb-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching.js
110 lines (110 loc) · 3.48 KB
/
caching.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
const { WeakLRUCache } = require('weak-lru-cache')
let getLastVersion
const mapGet = Map.prototype.get
exports.CachingStore = Store => class extends Store {
constructor(dbName, options) {
super(dbName, options)
if (!this.env.cacheCommitter) {
this.env.cacheCommitter = true
this.on('aftercommit', ({ operations, results }) => {
results = results || []
let activeCache
for (let i = 0, l = operations.length; i < l; i++) {
let operation = operations[i]
if (typeof operation[1] === 'object') {
if (activeCache) {
if (results[i] === 0) {
let expirationPriority = ((operation[1] || 0).length || 0) >> 10
let entry = mapGet.call(activeCache, operation[0])
if (entry)
activeCache.used(entry, expirationPriority) // this will enter it into the LRFU
} else
activeCache.delete(operation[0]) // just delete it from the map
}
} else if (operation && operation.length === undefined) {
activeCache = operation.cachingDb && operation.cachingDb.cache
}
}
})
}
this.db.cachingDb = this
this.cache = new WeakLRUCache(options.cache)
}
get(id, cacheMode) {
let value = this.cache.getValue(id)
if (value !== undefined)
return value
value = super.get(id)
if (value && typeof value === 'object' && !cacheMode && typeof id !== 'object') {
let entry = this.cache.setValue(id, value, this.lastSize >> 10)
if (this.useVersions) {
entry.version = getLastVersion()
}
}
return value
}
getEntry(id, cacheMode) {
let entry = this.cache.get(id)
if (entry)
return entry
let value = super.get(id)
if (value === undefined)
return
if (value && typeof value === 'object' && !cacheMode && typeof id !== 'object') {
entry = this.cache.setValue(id, value, this.lastSize >> 10)
} else {
entry = { value }
}
if (this.useVersions) {
entry.version = getLastVersion()
}
return entry
}
putEntry(id, entry, ifVersion) {
let result = super.put(id, entry.value, entry.version, ifVersion)
if (typeof id === 'object')
return result
if (result && result.then)
this.cache.setManually(id, entry) // set manually so we can keep it pinned in memory until it is committed
else // sync operation, immediately add to cache
this.cache.set(id, entry)
}
put(id, value, version, ifVersion) {
// if (this.cache.get(id)) // if there is a cache entry, remove it from scheduledEntries and
let result = super.put(id, value, version, ifVersion)
if (typeof id !== 'object') {
// sync operation, immediately add to cache, otherwise keep it pinned in memory until it is committed
let entry = this.cache.setValue(id, value, result.isSync ? 0 : -1)
if (version !== undefined)
entry.version = version
}
return result
}
putSync(id, value, version, ifVersion) {
if (id !== 'object') {
// sync operation, immediately add to cache, otherwise keep it pinned in memory until it is committed
if (value && typeof value === 'object') {
let entry = this.cache.setValue(id, value)
if (version !== undefined)
entry.version = version
} else // it is possible that a value used to exist here
this.cache.delete(id)
}
return super.putSync(id, value, version, ifVersion)
}
remove(id, ifVersion) {
this.cache.delete(id)
return super.remove(id, ifVersion)
}
removeSync(id, ifVersion) {
this.cache.delete(id)
return super.removeSync(id, ifVersion)
}
clear() {
this.cache.clear()
super.clear()
}
}
exports.setGetLastVersion = (get) => {
getLastVersion = get
}