Skip to content

Commit

Permalink
Made the cache object public for faster direct access, added comments…
Browse files Browse the repository at this point in the history
… to the raw cache object
  • Loading branch information
kartikk221 committed Sep 12, 2022
1 parent cd94b5b commit bbb3792
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
*/
class CachedLookup {
#lookup;
#cache = {};
#promises = {};

/**
* Contains the cached values identified by the serialized arguments.
*/
cache = {};

/**
* @typedef {Boolean|Number|String} SupportedArgumentTypes
*/
Expand Down Expand Up @@ -52,7 +56,7 @@ class CachedLookup {
const identifier = this._arguments_to_identifier(args);

// Attempt to lookup the value record for the specified arguments
const record = this.#cache[identifier];
const record = this.cache[identifier];

// Return the value record if it exists and is not older than the specified maximum age
if (record && record.updated_at > Date.now() - max_age) return record;
Expand All @@ -70,15 +74,15 @@ class CachedLookup {
const identifier = this._arguments_to_identifier(args);

// Initialize the record structure for the specified arguments if it does not exist
if (!this.#cache[identifier])
this.#cache[identifier] = {
if (!this.cache[identifier])
this.cache[identifier] = {
value: null,
updated_at: null,
};

// Fill the record values with the provided value and current timestamp
this.#cache[identifier].value = value;
this.#cache[identifier].updated_at = Date.now();
this.cache[identifier].value = value;
this.cache[identifier].updated_at = Date.now();
}

/**
Expand Down Expand Up @@ -169,8 +173,8 @@ class CachedLookup {
const identifier = this._arguments_to_identifier(Array.from(arguments));

// Remove the cached value record for the specified arguments
if (this.#cache[identifier]) {
delete this.#cache[identifier];
if (this.cache[identifier]) {
delete this.cache[identifier];
return true;
}
return false;
Expand Down Expand Up @@ -201,18 +205,7 @@ class CachedLookup {
const identifier = this._arguments_to_identifier(Array.from(arguments));

// Return the updated_at timestamp for the specified arguments
return this.#cache[identifier] ? this.#cache[identifier].updated_at : undefined;
}

/* CachedLookup Getters */

/**
* Returns the underlying cache object which contains the cached values identified by their serialized arguments.
*
* @returns {Map<String, ValueRecord>}
*/
get cache() {
return this.#cache;
return this.cache[identifier] ? this.cache[identifier].updated_at : undefined;
}
}

Expand Down

0 comments on commit bbb3792

Please sign in to comment.