JSFiddle - React, Tailwind, and code Playground

by Andrew Gerst

HTML

<input type="text" placeholder="Next Cache Value"/>

JavaScript

/**
 * Creates a new Cache object.
 * @param {number} maxSize The maximum size of the cache (or -1 for no max).
 * @param {object} storage Where to store cache?
 * @constructor
 */
function Cache(maxSize, storage){
    this.maxSize_ = maxSize || -1;
    this.storage_ = storage || new Cache.CacheStorage();
    this.stats_ = {};
    this.stats_['hits'] = 0;
    this.stats_['misses'] = 0;
}

/**
 * Memory cache storage backend.
 * @constructor
 */
Cache.CacheStorage = function(){
    this.items_ = {};
    this.count_ = 0;
};

Cache.CacheStorage.prototype.get = function(key){
	return this.items_[key];
};

Cache.CacheStorage.prototype.set = function(key, value){
	if (typeof this.get(key) === "undefined")
		this.count_++;
	this.items_[key] = value;
};

Cache.CacheStorage.prototype.size = function(key, value){
	return this.count_;
};

Cache.CacheStorage.prototype.remove = function(key){
	var item = this.get(key);
	if (typeof item !== "undefined")
		this.count_--;
	delete this.items_[key];
	return item;
};

Cache.CacheStorage.prototype.keys = function(){
	var ret = [], p;
	for (p in this.items_) ret.push(p);
	return ret;
};

/**
 * Retrieves an item from the cache.
 * @param {string} key The key to retrieve.
 * @return {object} The item, or null if it doesn't exist.
 */
Cache.prototype.get = function(key){
    var item = this.storage_.get(key);
};

Cache.prototype.set = function(key, value){
    
};

/**
 * Removes all items from the cache.
 */
Cache.prototype.clear = function(){
    var keys = this.storage_.keys();
    for (var i = 0; i < keys.length; i++) {
        this.remove(keys[i]);
    }
};

/**
 * @return {Object} The hits and misses on the cache.
 */
Cache.prototype.getStats = function(){
    return this.stats_;
};

/**
 * Remove an item from the cache.
 * @param {string} key The key of the item to remove.
 * @return {string} Returns value of item in the cache or null if it doesn't exist in cache.
 */
Cache.prototype.remove = function(key){
    var item =...