Example of cache Algorithm Implementation in JS (FIFO/LRU), fifolru

Source: Internet
Author: User

Example of cache Algorithm Implementation in JS (FIFO/LRU), fifolru

FIFO

The simplest caching algorithm is to set the upper limit of the cache. When the upper limit of the cache is reached, it is eliminated according to the first-in-first-out policy and then added to the new k-v.

An object is used as the cache, and an array is used together with the order in which records are added to the object to determine whether the upper limit is reached. If the upper limit is reached, the first element key in the array is obtained, corresponding to the key value in the deleted object.

/*** Implement cache for the FIFO queue algorithm * requires an object and an array as an auxiliary * array record to enter the Order */class condition ocache {constructor (limit) {this. limit = limit | 10 this. map = {} this. keys = []} set (key, value) {let map = this. map let keys = this. keys if (! Object. prototype. hasOwnProperty. call (map, key) {if (keys. length = this. limit) {delete map [keys. shift ()] // first-in-first-out, delete the first element of the queue} keys. push (key)} map [key] = value // assign a value to the key in the map whether it exists or not} get (key) {return this. map [key]} module. exports = export ocache

LRU

LRU (Least recently used, Least recently used) algorithm. The idea of this algorithm is that the recently accessed data will have a high probability of access in the future. When the cache is full, the most unattended users will be eliminated first.

Algorithm Implementation idea: based on the data structure of a double-stranded table, the new k-v is placed in the head of the linked list without full members, in the future, when k-v in the cache is obtained, the k-v is moved to the front. When the cache is full, the end of the k-v is eliminated first.

A two-way linked list has a head-and-tail pointer. Each node has a prev (prev) and a next (next) pointer pointing to its previous and next nodes respectively.

Key Point: In the Insert Process of a double-stranded table, we should pay attention to the sequence problem. We must first process the pointer while keeping the linked list constant, and finally point the original header pointer to the newly inserted element, in the implementation of the Code, please pay attention to the order I noted in the comments!

Class LruCache {constructor (limit) {this. limit = limit | 10 // head pointer pointing to the Header element, that is, the most common element this. head = this. tail = undefined this. map = {} this. size = 0} get (key, IfreturnNode) {let node = this. map [key] // if the cache object containing the property 'key' is not found if (node = undefined) return // if the cache object found is already tail (recently used) if (node = this. head) {// determines whether the node is the first node. // If yes, the return returnnode is returned without moving any element? Node: node. value} // It is not a header node. The iron must move the element if (node. prev) {// first, determine whether the node has a forward if (node = this. tail) {// There is a precursor. If it is a tail node, let the tail pointer point to the front of the current node this. tail = node. prev} // forward the successor of the current node to the current node. Node. prev. next = node. next} if (node. next) {// determine if the node has a successor. // if there is a successor, direct the successor to the current node. next. prev = node. prev // the entire process is to take out the current node and ensure that the linked list continues, and the current node is moved below} node. prev = undefined // move to the front, so there is no front node. next = this. head // note !!! Here we need to first give the previous line to the hand !!!! Point the successor of the current node to the original header if (this. head) {this. head. prev = node // point the previous header to the current node} this. head = node // This step can be performed only after the handover is completed! Otherwise, we won't be able to find the previous headers! Return IfreturnNode? Node: node. value} set (key, value) {// The previous algorithm can directly store k-v, but now we need to encapsulate a simple k-v into a node that meets the double-stranded table // 1. check whether this node is available. get (key, true) if (! Node) {if (this. size = this. limit) {// determines whether the cache has reached the upper limit. // Delete the last node. If (this. tail) {this. tail = this. tail. prev this. tail. prev. next = undefined // After the chain is smoothly disconnected, the current node is destroyed. tail. prev = this. tail. next = undefined this. map [this. tail. key] = undefined // The current cache memory releases a slot this. size --} node = {key: key} this. map [key] = node if (this. head) {// determine if there is a node in the cache this. head. prev = node. next = this. head} else {// there is no value in the cache. It is a pleasure to let the head direct to the new node. head = node this. tail = node} this. size ++ // reduce a cache slot} // assign a new value to the node if the node does not exist. value = value} module. exports = LruCache

The specific idea is that if the node you want to get is not a header node (that is, a node that is already in use recently and does not need to move the node location), you must perform smooth chain disconnection first, after processing the pointer pointing relationship, take out the node that needs to be moved to the front and insert the linked list.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.