forked from launchdarkly/node-server-sdk
-
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.
prepare 5.6.0 release (launchdarkly#128)
- Loading branch information
1 parent
7bacfe2
commit 5242ab9
Showing
10 changed files
with
846 additions
and
177 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,143 @@ | ||
var NodeCache = require('node-cache'), | ||
dataKind = require('./versioned_data_kind'), | ||
UpdateQueue = require('./update_queue'); | ||
|
||
function cacheKey(kind, key) { | ||
return kind.namespace + ":" + key; | ||
} | ||
|
||
function allCacheKey(kind) { | ||
return "$all:" + kind.namespace; | ||
} | ||
|
||
var initializedKey = "$checkedInit"; | ||
|
||
/* | ||
CachingStoreWrapper provides commonly needed functionality for implementations of an | ||
SDK feature store. The underlyingStore must implement a simplified interface for | ||
querying and updating the data store (see redis_feature_store.js for an example) | ||
while CachingStoreWrapper adds optional caching of stored items and of the | ||
initialized state, and ensures that asynchronous operations are serialized correctly. | ||
*/ | ||
function CachingStoreWrapper(underlyingStore, ttl) { | ||
var cache = ttl ? new NodeCache({ stdTTL: ttl }) : null; | ||
var queue = new UpdateQueue(); | ||
var initialized = false; | ||
|
||
this.underlyingStore = underlyingStore; | ||
|
||
this.init = function(allData, cb) { | ||
queue.enqueue(function(cb) { | ||
underlyingStore.initInternal(allData, function() { | ||
initialized = true; | ||
|
||
if (cache) { | ||
cache.del(initializedKey); | ||
cache.flushAll(); | ||
|
||
// populate cache with initial data | ||
for (var kindNamespace in allData) { | ||
if (Object.hasOwnProperty.call(allData, kindNamespace)) { | ||
var kind = dataKind[kindNamespace]; | ||
var items = allData[kindNamespace]; | ||
cache.set(allCacheKey(kind), items); | ||
for (var key in items) { | ||
cache.set(cacheKey(kind, key), items[key]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
cb(); | ||
}); | ||
}, [], cb); | ||
}; | ||
|
||
this.initialized = function(cb) { | ||
if (initialized) { | ||
cb(true); | ||
} else if (cache && cache.get(initializedKey)) { | ||
cb(false); | ||
} else { | ||
underlyingStore.initializedInternal(function(inited) { | ||
initialized = inited; | ||
if (!initialized) { | ||
cache && cache.set(initializedKey, true); | ||
} | ||
cb(initialized); | ||
}); | ||
} | ||
}; | ||
|
||
this.all = function(kind, cb) { | ||
var items = cache && cache.get(allCacheKey(kind)); | ||
if (items) { | ||
cb(items); | ||
return; | ||
} | ||
|
||
underlyingStore.getAllInternal(kind, function(items) { | ||
var filteredItems = {}; | ||
Object.keys(items).forEach(function(key) { | ||
var item = items[key]; | ||
if (item && !item.deleted) { | ||
filteredItems[key] = item; | ||
} | ||
}); | ||
cache && cache.set(allCacheKey(kind), filteredItems); | ||
cb(filteredItems); | ||
}); | ||
}; | ||
|
||
this.get = function(kind, key, cb) { | ||
if (cache) { | ||
var item = cache.get(cacheKey(kind, key)); | ||
if (item !== undefined) { | ||
cb(itemOnlyIfNotDeleted(item)); | ||
return; | ||
} | ||
} | ||
|
||
underlyingStore.getInternal(kind, key, function(item) { | ||
cache && cache.set(cacheKey(kind, key), item); | ||
cb(itemOnlyIfNotDeleted(item)); | ||
}); | ||
}; | ||
|
||
function itemOnlyIfNotDeleted(item) { | ||
return (!item || item.deleted) ? null : item; | ||
} | ||
|
||
this.upsert = function(kind, newItem, cb) { | ||
queue.enqueue(function (cb) { | ||
flushAllCaches(); | ||
underlyingStore.upsertInternal(kind, newItem, function(err, updatedItem) { | ||
if (!err) { | ||
cache && cache.set(cacheKey(kind, newItem.key), updatedItem); | ||
} | ||
cb(); | ||
}); | ||
}, [], cb); | ||
}; | ||
|
||
this.delete = function(kind, key, version, cb) { | ||
this.upsert(kind, { key: key, version: version, deleted: true }, cb); | ||
}; | ||
|
||
this.close = function() { | ||
cache.close(); | ||
underlyingStore.close(); | ||
}; | ||
|
||
function flushAllCaches() { | ||
if (!cache) { | ||
return; | ||
} | ||
for (var kindNamespace in dataKind) { | ||
cache.del(allCacheKey(dataKind[kindNamespace])); | ||
} | ||
} | ||
} | ||
|
||
module.exports = CachingStoreWrapper; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.