1 var Helpers = require('./Helpers');
  2 
  3 /**
  4  * Code adapted from MicroCache library:
  5  * https://github.com/jeromeetienne/microcache.js
  6  * License: https://github.com/jeromeetienne/MicroCache.js/blob/master/MIT-LICENSE.txt
  7  */
  8 
  9 var Cache = function (expireAfterSeconds) {
 10   var _values = {};
 11   var createdTime = (new Date()).getTime();
 12 
 13   var _expire = function () {
 14     if (typeof expireAfterSeconds != 'undefined' && expireAfterSeconds > 0) {
 15       if ((new Date()).getTime() - createdTime >= expireAfterSeconds * 1000) {
 16         _values = {};
 17         createdTime = (new Date()).getTime();
 18       }
 19     }
 20   };
 21 
 22   return {
 23     get      : function (key) {
 24       _expire();
 25       return _values[key];
 26     },
 27     _set     : function (key, value) {
 28       _expire();
 29       _values[key] = value;
 30     },
 31     contains : function (key) {
 32       _expire();
 33       return key in _values;
 34     },
 35     /**
 36      * Get a value from cache; if the value is not found, then add it to the cache
 37      */
 38     getSet   : function (key, value) {
 39       _expire();
 40       if (!this.contains(key)) {
 41         this._set(key, value)
 42       }
 43       return this.get(key);
 44     },
 45     /**
 46      * Clear all values in cache
 47      */
 48     clear    : function () {
 49       _values = {};
 50     }
 51   }
 52 };
 53 
 54 if (typeof module != 'undefined' && ('exports' in module)) {
 55   module.exports = Cache;
 56 }
 57 
 58 
 59