forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APIv3: Cache invalidation + refactoring (nightscout#6688)
* APIv3: isolating documents from tests (not allowing clashes of calculated identifiers) * removing unused async keyword * fixing api v3 swagger and moving it to /api3-docs * APIv3: adding cachedCollection stub of cachedCollection storage implementation * APIv3: mongo cachedCollection storage implementation * APIv3: testing and debugging cache updates * APIv3: more testing on cache updates * APIv3: fixing bad async functions * APIv3: finishing cache invalidation tests Co-authored-by: Petr Ondrusek <[email protected]> Co-authored-by: Petr Ondrůšek <[email protected]> Co-authored-by: Sulka Haro <[email protected]>
- Loading branch information
Showing
18 changed files
with
539 additions
and
119 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash') | ||
|
||
/** | ||
* Storage implementation which wraps mongo baseStorage with caching | ||
* @param {Object} ctx | ||
* @param {Object} env | ||
* @param {string} colName - name of the collection in mongo database | ||
* @param {Object} baseStorage - wrapped mongo storage implementation | ||
*/ | ||
function MongoCachedCollection (ctx, env, colName, baseStorage) { | ||
|
||
const self = this; | ||
|
||
self.colName = colName; | ||
|
||
self.identifyingFilter = baseStorage.identifyingFilter; | ||
|
||
self.findOne = (...args) => baseStorage.findOne(...args); | ||
|
||
self.findOneFilter = (...args) => baseStorage.findOneFilter(...args); | ||
|
||
self.findMany = (...args) => baseStorage.findMany(...args); | ||
|
||
|
||
self.insertOne = async (doc) => { | ||
const result = await baseStorage.insertOne(doc, { normalize: false }); | ||
|
||
if (cacheSupported()) { | ||
updateInCache([doc]); | ||
} | ||
|
||
if (doc._id) { | ||
delete doc._id; | ||
} | ||
return result; | ||
} | ||
|
||
|
||
self.replaceOne = async (identifier, doc) => { | ||
const result = await baseStorage.replaceOne(identifier, doc); | ||
|
||
if (cacheSupported()) { | ||
const rawDocs = await baseStorage.findOne(identifier, null, { normalize: false }) | ||
updateInCache([rawDocs[0]]) | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
self.updateOne = async (identifier, setFields) => { | ||
const result = await baseStorage.updateOne(identifier, setFields); | ||
|
||
if (cacheSupported()) { | ||
const rawDocs = await baseStorage.findOne(identifier, null, { normalize: false }) | ||
|
||
if (rawDocs[0].isValid === false) { | ||
deleteInCache(rawDocs) | ||
} | ||
else { | ||
updateInCache([rawDocs[0]]) | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
self.deleteOne = async (identifier) => { | ||
let invalidateDocs | ||
if (cacheSupported()) { | ||
invalidateDocs = await baseStorage.findOne(identifier, { _id: 1 }, { normalize: false }) | ||
} | ||
|
||
const result = await baseStorage.deleteOne(identifier); | ||
|
||
if (cacheSupported()) { | ||
deleteInCache(invalidateDocs) | ||
} | ||
|
||
return result; | ||
} | ||
|
||
self.deleteManyOr = async (filter) => { | ||
let invalidateDocs | ||
if (cacheSupported()) { | ||
invalidateDocs = await baseStorage.findMany({ filter, | ||
limit: 1000, | ||
skip: 0, | ||
projection: { _id: 1 }, | ||
options: { normalize: false } }); | ||
} | ||
|
||
const result = await baseStorage.deleteManyOr(filter); | ||
|
||
if (cacheSupported()) { | ||
deleteInCache(invalidateDocs) | ||
} | ||
|
||
return result; | ||
} | ||
|
||
self.version = (...args) => baseStorage.version(...args); | ||
|
||
self.getLastModified = (...args) => baseStorage.getLastModified(...args); | ||
|
||
function cacheSupported () { | ||
return ctx.cache | ||
&& ctx.cache[colName] | ||
&& _.isArray(ctx.cache[colName]); | ||
} | ||
|
||
function updateInCache (doc) { | ||
if (doc && doc.isValid === false) { | ||
deleteInCache([doc._id]) | ||
} | ||
else { | ||
ctx.bus.emit('data-update', { | ||
type: colName | ||
, op: 'update' | ||
, changes: doc | ||
}); | ||
} | ||
} | ||
|
||
function deleteInCache (docs) { | ||
let changes | ||
if (_.isArray(docs)) { | ||
if (docs.length === 0) { | ||
return | ||
} | ||
else if (docs.length === 1 && docs[0]._id) { | ||
const _id = docs[0]._id.toString() | ||
changes = [ _id ] | ||
} | ||
} | ||
|
||
ctx.bus.emit('data-update', { | ||
type: colName | ||
, op: 'remove' | ||
, changes | ||
}); | ||
} | ||
} | ||
|
||
module.exports = MongoCachedCollection; |
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.