issue #374 cap cached frames for cachedframeprocessor to 100

This commit is contained in:
juliandescottes 2016-11-20 01:10:05 +01:00
parent 4b50dfdb5b
commit d08c101b11

View File

@ -1,8 +1,8 @@
(function () { (function () {
var ns = $.namespace('pskl.model.frame'); var ns = $.namespace('pskl.model.frame');
// 10 * 60 * 1000 = 10 minutes // Maximum number of cache entries
var DEFAULT_CLEAR_INTERVAL = 10 * 60 * 1000; var MAX_CACHE_ENTRIES = 100;
var DEFAULT_FRAME_PROCESSOR = function (frame) { var DEFAULT_FRAME_PROCESSOR = function (frame) {
return pskl.utils.FrameUtils.toImage(frame); return pskl.utils.FrameUtils.toImage(frame);
@ -12,14 +12,16 @@
var DEFAULT_NAMESPACE = '__cache_default__'; var DEFAULT_NAMESPACE = '__cache_default__';
ns.CachedFrameProcessor = function (cacheResetInterval) { ns.CachedFrameProcessor = function () {
// Cache object.
this.cache_ = {}; this.cache_ = {};
this.cacheResetInterval = cacheResetInterval || DEFAULT_CLEAR_INTERVAL;
// Array of [namespace, key] for each cached frame.
this.cacheQueue_ = [];
this.frameProcessor = DEFAULT_FRAME_PROCESSOR; this.frameProcessor = DEFAULT_FRAME_PROCESSOR;
this.outputCloner = DEFAULT_OUTPUT_CLONER; this.outputCloner = DEFAULT_OUTPUT_CLONER;
this.defaultNamespace = DEFAULT_NAMESPACE; this.defaultNamespace = DEFAULT_NAMESPACE;
window.setInterval(this.clear.bind(this), this.cacheResetInterval);
}; };
ns.CachedFrameProcessor.prototype.clear = function () { ns.CachedFrameProcessor.prototype.clear = function () {
@ -69,6 +71,11 @@
} else { } else {
processedFrame = this.frameProcessor(frame); processedFrame = this.frameProcessor(frame);
cache[cacheKey] = processedFrame; cache[cacheKey] = processedFrame;
this.cacheQueue_.unshift([namespace, cacheKey]);
if (this.cacheQueue_.length > MAX_CACHE_ENTRIES) {
var oldestItem = this.cacheQueue_.pop();
this.cache_[oldestItem[0]][oldestItem[1]] = null;
}
} }
return processedFrame; return processedFrame;