A cache object that deletes the least-\/recently-\/used items.

\href{https://travis-ci.org/isaacs/node-lru-cache}{\texttt{ }} \href{https://coveralls.io/github/isaacs/node-lru-cache}{\texttt{ }}\hypertarget{md_sfcode_node_modules_lru_cache_README_autotoc_md16304}{}\doxysection{Installation\+:}\label{md_sfcode_node_modules_lru_cache_README_autotoc_md16304}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install lru-\/cache -\/-\/save }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_lru_cache_README_autotoc_md16305}{}\doxysection{Usage\+:}\label{md_sfcode_node_modules_lru_cache_README_autotoc_md16305}

\begin{DoxyCode}{0}
\DoxyCodeLine{var LRU = require("{}lru-\/cache"{})}
\DoxyCodeLine{  , options = \{ max: 500}
\DoxyCodeLine{              , length: function (n, key) \{ return n * 2 + key.length \}}
\DoxyCodeLine{              , dispose: function (key, n) \{ n.close() \}}
\DoxyCodeLine{              , maxAge: 1000 * 60 * 60 \}}
\DoxyCodeLine{  , cache = new LRU(options)}
\DoxyCodeLine{  , otherCache = new LRU(50) // sets just the max size}
\DoxyCodeLine{}
\DoxyCodeLine{cache.set("{}key"{}, "{}value"{})}
\DoxyCodeLine{cache.get("{}key"{}) // "{}value"{}}
\DoxyCodeLine{}
\DoxyCodeLine{// non-\/string keys ARE fully supported}
\DoxyCodeLine{// but note that it must be THE SAME object, not}
\DoxyCodeLine{// just a JSON-\/equivalent object.}
\DoxyCodeLine{var someObject = \{ a: 1 \}}
\DoxyCodeLine{cache.set(someObject, 'a value')}
\DoxyCodeLine{// Object keys are not toString()-\/ed}
\DoxyCodeLine{cache.set('[object Object]', 'a different value')}
\DoxyCodeLine{assert.equal(cache.get(someObject), 'a value')}
\DoxyCodeLine{// A similar object with same keys/values won't work,}
\DoxyCodeLine{// because it's a different object identity}
\DoxyCodeLine{assert.equal(cache.get(\{ a: 1 \}), undefined)}
\DoxyCodeLine{}
\DoxyCodeLine{cache.reset()    // empty the cache }

\end{DoxyCode}


If you put more stuff in it, then items will fall out.

If you try to put an oversized thing in it, then it\textquotesingle{}ll fall out right away.\hypertarget{md_sfcode_node_modules_lru_cache_README_autotoc_md16306}{}\doxysection{Options}\label{md_sfcode_node_modules_lru_cache_README_autotoc_md16306}

\begin{DoxyItemize}
\item {\ttfamily max} The maximum size of the cache, checked by applying the length function to all values in the cache. Not setting this is kind of silly, since that\textquotesingle{}s the whole purpose of this lib, but it defaults to {\ttfamily Infinity}. Setting it to a non-\/number or negative number will throw a {\ttfamily Type\+Error}. Setting it to 0 makes it be {\ttfamily Infinity}.
\item {\ttfamily max\+Age} Maximum age in ms. Items are not pro-\/actively pruned out as they age, but if you try to get an item that is too old, it\textquotesingle{}ll drop it and return undefined instead of giving it to you. Setting this to a negative value will make everything seem old! Setting it to a non-\/number will throw a {\ttfamily Type\+Error}.
\item {\ttfamily length} Function that is used to calculate the length of stored items. If you\textquotesingle{}re storing strings or buffers, then you probably want to do something like {\ttfamily function(n, key)\{return n.\+length\}}. The default is {\ttfamily function()\{return 1\}}, which is fine if you want to store {\ttfamily max} like-\/sized things. The item is passed as the first argument, and the key is passed as the second argumnet.
\item {\ttfamily dispose} Function that is called on items when they are dropped from the cache. This can be handy if you want to close file descriptors or do other cleanup tasks when items are no longer accessible. Called with {\ttfamily key, value}. It\textquotesingle{}s called {\itshape before} actually removing the item from the internal cache, so if you want to immediately put it back in, you\textquotesingle{}ll have to do that in a {\ttfamily next\+Tick} or {\ttfamily set\+Timeout} callback or it won\textquotesingle{}t do anything.
\item {\ttfamily stale} By default, if you set a {\ttfamily max\+Age}, it\textquotesingle{}ll only actually pull stale items out of the cache when you {\ttfamily get(key)}. (That is, it\textquotesingle{}s not pre-\/emptively doing a {\ttfamily set\+Timeout} or anything.) If you set {\ttfamily stale\+:true}, it\textquotesingle{}ll return the stale value before deleting it. If you don\textquotesingle{}t set this, then it\textquotesingle{}ll return {\ttfamily undefined} when you try to get a stale entry, as if it had already been deleted.
\item {\ttfamily no\+Dispose\+On\+Set} By default, if you set a {\ttfamily dispose()} method, then it\textquotesingle{}ll be called whenever a {\ttfamily set()} operation overwrites an existing key. If you set this option, {\ttfamily dispose()} will only be called when a key falls out of the cache, not when it is overwritten.
\item {\ttfamily update\+Age\+On\+Get} When using time-\/expiring entries with {\ttfamily max\+Age}, setting this to {\ttfamily true} will make each item\textquotesingle{}s effective time update to the current time whenever it is retrieved from cache, causing it to not expire. (It can still fall out of cache based on recency of use, of course.)
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_lru_cache_README_autotoc_md16307}{}\doxysection{API}\label{md_sfcode_node_modules_lru_cache_README_autotoc_md16307}

\begin{DoxyItemize}
\item {\ttfamily set(key, value, max\+Age)}
\item {\ttfamily get(key) =$>$ value}

Both of these will update the \char`\"{}recently used\char`\"{}-\/ness of the key. They do what you think. {\ttfamily max\+Age} is optional and overrides the cache {\ttfamily max\+Age} option if provided.

If the key is not found, {\ttfamily get()} will return {\ttfamily undefined}.

The key and val can be any value.
\item {\ttfamily peek(key)}

Returns the key value (or {\ttfamily undefined} if not found) without updating the \char`\"{}recently used\char`\"{}-\/ness of the key.

(If you find yourself using this a lot, you {\itshape might} be using the wrong sort of data structure, but there are some use cases where it\textquotesingle{}s handy.)
\item {\ttfamily del(key)}

Deletes a key out of the cache.
\item {\ttfamily reset()}

Clear the cache entirely, throwing away all values.
\item {\ttfamily has(key)}

Check if a key is in the cache, without updating the recent-\/ness or deleting it for being stale.
\item {\ttfamily for\+Each(function(value,key,cache), \mbox{[}thisp\mbox{]})}

Just like {\ttfamily Array.\+prototype.\+for\+Each}. Iterates over all the keys in the cache, in order of recent-\/ness. (Ie, more recently used items are iterated over first.)
\item {\ttfamily rfor\+Each(function(value,key,cache), \mbox{[}thisp\mbox{]})}

The same as {\ttfamily cache.\+for\+Each(...)} but items are iterated over in reverse order. (ie, less recently used items are iterated over first.)
\item {\ttfamily keys()}

Return an array of the keys in the cache.
\item {\ttfamily values()}

Return an array of the values in the cache.
\item {\ttfamily length}

Return total length of objects in cache taking into account {\ttfamily length} options function.
\item {\ttfamily item\+Count}

Return total quantity of objects currently in cache. Note, that {\ttfamily stale} (see options) items are returned as part of this item count.
\item {\ttfamily dump()}

Return an array of the cache entries ready for serialization and usage with \textquotesingle{}destination\+Cache.\+load(arr)\`{}.
\item {\ttfamily load(cache\+Entries\+Array)}

Loads another cache entries array, obtained with {\ttfamily source\+Cache.\+dump()}, into the cache. The destination cache is reset before loading new entries
\item {\ttfamily prune()}

Manually iterates over the entire cache proactively pruning old entries 
\end{DoxyItemize}