<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>cache &#8211; JSAlgorithm</title>
	<atom:link href="https://jsalgorithm.com/tag/cache/feed/" rel="self" type="application/rss+xml" />
	<link>https://jsalgorithm.com</link>
	<description></description>
	<lastBuildDate>Wed, 13 May 2020 16:12:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.5.15</generator>
<site xmlns="com-wordpress:feed-additions:1">177240629</site>	<item>
		<title>LRU Cache</title>
		<link>https://jsalgorithm.com/2020/05/13/lru-cache/</link>
					<comments>https://jsalgorithm.com/2020/05/13/lru-cache/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Wed, 13 May 2020 16:12:11 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[leetcode]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=60</guid>

					<description><![CDATA[]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-code"><code>class LRUCache {
constructor(capacity) {
    this.capacity = capacity;
    this.cache = {};
    this.keys = &#91;];
};

get(key) {
    // if value is already in the cache,
    // reorder keys array and put the key on the end of queue
    let val = this.cache&#91;key];
    if (val !== undefined) {
        this.keys.splice(this.keys.indexOf(key), 1);
        this.keys.push(key);
        return val;
    }

    return -1;
};

put(key, value) {
    let obj = this.cache&#91;key];

    // if key is already in the cache
    // remove key so we can add it on the end later
    if (obj !== undefined) {
        this.keys.splice(this.keys.indexOf(key), 1);
    }

    // if adding key is already over the capacity
    // we are going to remove the first item on the keys
    if (obj === undefined &amp;&amp; this.keys.length === this.capacity) {
        const oldKey = this.keys.shift(); //?
        delete this.cache&#91;oldKey]; //?
    }

    // add key to the keys, this is either an existing key or a new key
    this.keys.push(key); //?

    this.cache&#91;key] = value;
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/13/lru-cache/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">60</post-id>	</item>
	</channel>
</rss>
