LRU Cache In Javascript

Jun 08, 2010 2 Comments by Mahdi Pedram

About two weeks ago, I was questioned about a LRU ( least recently used ) Cache in javascript , so I started doing some research on it and apparently I found a good idea to implement that . now the question is what is a LRU cache and why would we need it .

well basically the algorithm always discards the least recently used item in the Cache . so you can always store items in your and  the cache always has items that have been used frequently .

why do we need LRU Cache :
In large front-end web applications when we do have a lot of heavy duty ajax calls in the client side, sometimes we face a situation that we are calling the same ajax call again and again so instead of using the bandwidth and having the user wait for the ajax request we could easily take advantage of our LRU Cache and save Items in our Cache, and as we know we have some limitation in our Cache size ( memory limitation in the browser ) ,so we use Least recently used algorithm to get around it.
therefore before firing an ajax call first of all we check our cache if our data is in our cache we just fetch the data otherwise we fire our ajax request .
there are many other reasons we could use this object but this one was pretty obvious .

LRU Cache Features

  • Absolute Expiration for cache and any Items ( by Seconds )
  • Sliding Expiration for Cache and any Item ( by Seconds )
  • Specifying a Size for your cache .

Read below for documentation

// Create a new instantiation fo the Object
/*
* Public Functions :
*/
myCache.settings()
myCache.setSize()
myCache.getSize()
myCache.add()
myCache.flush()
myCache.get()
myCache.getValue()
myCache.getCacheRecords()
myCache.getCache()
/***************** Usage :*****************************/
 
/*
* Create :
*
*
* How to create a new instance of the Cache
* By default the Cache Holds 1000 Items
* and there is no Expiration for it
* and No Callback Function
*/
 
var myCache = Object.create( Cache );
 
/* configuration :
*
*  maxsize : you could change the size of the cache easily
*  absoluteExpire: your cache will be expired after N seconds if you set the absolute Expire
*  slidingExpire : your cache will be expired after N seconds of the Last time the Cache was used .
*  callbak function : this function will be triggered when the cache is expired
*/
 
myCache.settings({
   absoluteExpire : 50 ,
   slidingExpire  : 20 ,
   maxsize        : 10 ,
   callback        : function(data){ }
});
 
/*
* Set Cache size :
* if sometime along the way you want to set the Cache you can use this function
*/
 
myCache.setSize( 3 );
 
/*
* Get Cache size :
*
*/
 
  myCache.getSize();
 
/*
* Add Records :
*
* if you want to add a record to the Cache
* you have to pass a pair of key / value to this function below.
*/
 
  myCache.add( 'key' , 'value' );
 
/*
* if you want to add Expiration to the Record you can
*  absoluteExpire: your record will be expired after N seconds if you set the absolute Expire
*  slidingExpire : your record will be expired after N seconds of the Last time the Record was Fetched .
*  callback      : callback function will be triggered when  records expired OR Cache is flushed OR noSpace in Cache
*/
 
myCache.add('key' , 'value' , {
                        absoluteExpire : 100 ,
                        slidingExpire : 5
                         callback:function( data ){ console.log(data); }
                        }
});
 
/*
* Flush Cache :
*
* if you want to empty your Cache use this
*/
 
myCache.flush();
 
/*
* Get Record :
*
* Input is the Key value of the record
* returns the record Object
*/
myCache.get('key')
 
/*
* Get Record Value :
*
* Input is the Key value of the record
* returns the Value of the record
*/
 
myCache.getValue('key');
 
/*Get All Records
   returns all records of the cache
*/
 
myCache.getCacheRecords();
 
/* Get All Information Of the Cache
*/
 
myCache.getCache();
http://www.davidsbridal.com/webapp/wcs/stores/servlet/ProductDisplayView?storeId=10052&catalogId=10051&categoryId=3001783&currentIdx=3&subCategory=-49999486%257c3001447%257c3001518%257c3001783&catentryId=6144139&sort=
Coder, LRU cache, javascript

About the author

I'm Web Developer , I spent most of my time on programming web applications ,I'm both Front-end , Back-end Developer . Coding is part of my life , I don't do programming for life , I am alive to Code

2 Responses to “LRU Cache In Javascript”

Leave a Reply