All files / ima/cache CacheEntry.js

100% Statements 9/9
100% Branches 0/0
100% Functions 4/4
100% Lines 9/9
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    5x                                     58x               58x             58x                 10x 10x                   5x                   6x       5x  
import ns from '../namespace';
 
ns.namespace('ima.cache');
 
/**
 * The cache entry is a typed container of cache data used to track the
 * creation and expiration of cache entries.
 */
export default class CacheEntry {
  /**
	 * Initializes the cache entry.
	 *
	 * @param {*} value The cache entry value.
	 * @param {number} ttl The time to live in milliseconds.
	 */
  constructor(value, ttl) {
    /**
		 * Cache entry value.
		 *
		 * @type {*}
		 */
    this._value = value;
 
    /**
		 * The time to live in milliseconds. The cache entry is considered
		 * expired after this time.
		 *
		 * @type {number}
		 */
    this._ttl = ttl;
 
    /**
		 * The timestamp of creation of this cache entry.
		 *
		 * @type {number}
		 */
    this._created = Date.now();
  }
 
  /**
	 * Returns {@code true} if this entry has expired.
	 *
	 * @return {boolean} {@code true} if this entry has expired.
	 */
  isExpired() {
    let now = Date.now();
    return now > this._created + this._ttl;
  }
 
  /**
	 * Exports this cache entry into a JSON-serializable object.
	 *
	 * @return {{value: *, ttl: number}} This entry exported to a
	 *         JSON-serializable object.
	 */
  serialize() {
    return { value: this._value, ttl: this._ttl };
  }
 
  /**
	 * Returns the entry value. If entry value is type of object returns clone
	 * of that object.
	 *
	 * @return {*} The entry value.
	 */
  getValue() {
    return this._value;
  }
}
 
ns.ima.cache.CacheEntry = CacheEntry;