Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache categories:cid and cid:<cid>:children
these rarely change, no need to go to db for them
- Loading branch information
1 parent
7357926
commit 00a0669
Showing
15 changed files
with
168 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
var LRU = require('lru-cache'); | ||
var pubsub = require('./pubsub'); | ||
|
||
var cache = LRU({ | ||
max: 1000, | ||
maxAge: 0, | ||
}); | ||
cache.hits = 0; | ||
cache.misses = 0; | ||
|
||
const cacheGet = cache.get; | ||
const cacheDel = cache.del; | ||
const cacheReset = cache.reset; | ||
|
||
cache.get = function (key) { | ||
const data = cacheGet.apply(cache, [key]); | ||
if (data === undefined) { | ||
cache.misses += 1; | ||
} else { | ||
cache.hits += 1; | ||
} | ||
return data; | ||
}; | ||
|
||
cache.del = function (key) { | ||
if (!Array.isArray(key)) { | ||
key = [key]; | ||
} | ||
pubsub.publish('local:cache:del', key); | ||
key.forEach(key => cacheDel.apply(cache, [key])); | ||
}; | ||
|
||
cache.reset = function () { | ||
pubsub.publish('local:cache:reset'); | ||
localReset(); | ||
}; | ||
|
||
function localReset() { | ||
cacheReset.apply(cache); | ||
cache.hits = 0; | ||
cache.misses = 0; | ||
} | ||
|
||
pubsub.on('local:cache:reset', function () { | ||
localReset(); | ||
}); | ||
|
||
pubsub.on('local:cache:del', function (keys) { | ||
if (Array.isArray(keys)) { | ||
keys.forEach(key => cacheDel.apply(cache, [key])); | ||
} | ||
}); | ||
|
||
module.exports = cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.