all files / src/ Cache.js

68.97% Statements 40/58
50% Branches 9/18
55.56% Functions 5/9
70.18% Lines 40/57
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                  10× 10× 10×           10×                                                                                          
'use strict';
 
var log = require('./Log.js').Logger('libZotero:Cache');
 
//build a consistent string from an object to use as a cache key
//put object key/value pairs into array, sort array, and concatenate
//array with '/'
module.exports = function(store) {
	this.store = store;
	var registry = this.store._registry;
	Eif(registry === null || typeof registry == 'undefined'){
		registry = {};
		this.store._registry = JSON.stringify(registry);
	}
};
 
module.exports.prototype.objectCacheString = function(params){
	var paramVarsArray = [];
	Object.keys(params).forEach(function(index){
		var value = params[index];
		Iif(!value) { return; }
		else Iif(Array.isArray(value)){
			value.forEach(function(v){
				paramVarsArray.push(index + '/' + encodeURIComponent(v) );
			});
		}
		else{
			paramVarsArray.push(index + '/' + encodeURIComponent(value) );
		}
	});
	paramVarsArray.sort();
	log.debug(paramVarsArray, 4);
	var objectCacheString = paramVarsArray.join('/');
	return objectCacheString;
};
 
//should use setItem and getItem if I extend that to the case where no Storage object is available in the browser
module.exports.prototype.save = function(params, object, cachetags){
	//cachetags for expiring entries
	Eif(!Array.isArray(cachetags)){
		cachetags = [];
	}
	//get registry object from storage
	var registry = JSON.parse(this.store._registry);
	Iif(!registry){
		registry = {};
	}
	var objectCacheString = this.objectCacheString(params);
	//save object in storage
	this.store[objectCacheString] = JSON.stringify(object);
	//make registry entry for object
	var registryEntry = {'id':objectCacheString, saved:Date.now(), cachetags:cachetags};
	registry[objectCacheString] = registryEntry;
	//save registry back to storage
	this.store._registry = JSON.stringify(registry);
};
 
module.exports.prototype.load = function(params){
	log.debug('Zotero.Cache.load', 3);
	var objectCacheString = this.objectCacheString(params);
	log.debug(objectCacheString, 4);
	try{
		var s = this.store[objectCacheString];
		if(!s){
			log.warn('No value found in cache store - ' + objectCacheString, 3);
			return null;
		}
		else{
			return JSON.parse(s);
		}
	}
	catch(e){
		log.error(`Error parsing retrieved cache data: ${objectCacheString} : ${s}`);
		return null;
	}
};
 
module.exports.prototype.expireCacheTag = function(tag){
	log.debug('Zotero.Cache.expireCacheTag', 3);
	var registry = JSON.parse(this.store._registry);
	var store = this.store;
	Object.keys(registry).forEach(function(index){
		var value = registry[index];
		if(value.cachetags.indexOf(tag) != (-1)){
			log.debug('tag ' + tag + ' found for item ' + value['id'] + ' : expiring', 4);
			delete store[value['id']];
			delete registry[value['id']];
		}
	});
};
 
module.exports.prototype.clear = function(){
	if(typeof(this.store.clear) == 'function'){
		this.store.clear();
	}
	else{
		this.store = {};
	}
};