CACHE for LRU algorithm implemented by PHP

Source: Internet
Author: User
LRU is short for LastRecentUsed. it is used as a cache algorithm to invalidate the cache that is rarely used recently. Memcache uses this algorithm. The following uses a PHP implementation method. This algorithm puts each added content on the top of the cache to clear the content at the bottom of the cache when the cache is reached. You can use the following PHP code

LRU is short for Last Recent Used. it is Used as a cache algorithm to invalidate the cache that is rarely Used recently. Memcache uses this algorithm. The following uses a PHP implementation method. This algorithm puts each added content on the top of the cache to clear the content at the bottom of the cache when the cache is reached. The following PHP code can be used for simulation.

 capacity = $capacity;        $this->hashmap = array();        $this->head = new Node(null, null);        $this->tail = new Node(null, null);        $this->head->setNext($this->tail);        $this->tail->setPrevious($this->head);    }    public function get($key) {        if (!isset($this->hashmap[$key])) { return null; }        $node = $this->hashmap[$key];        if (count($this->hashmap) == 1) { return $node->getData(); }        // refresh the access        $this->detach($node);        $this->attach($this->head, $node);        return $node->getData();    }    public function put($key, $data) {        if ($this->capacity <= 0) { return false; }        if (isset($this->hashmap[$key]) && !empty($this->hashmap[$key])) {            $node = $this->hashmap[$key];            // update data            $this->detach($node);            $this->attach($this->head, $node);            $node->setData($data);        }        else {            $node = new Node($key, $data);            $this->hashmap[$key] = $node;            $this->attach($this->head, $node);            // check if cache is full            if (count($this->hashmap) > $this->capacity) {                // we're full, remove the tail                $nodeToRemove = $this->tail->getPrevious();                $this->detach($nodeToRemove);                unset($this->hashmap[$nodeToRemove->getKey()]);            }        }        return true;    }    private function attach($head, $node) {        $node->setPrevious($head);        $node->setNext($head->getNext());        $node->getNext()->setPrevious($node);        $node->getPrevious()->setNext($node);    }    private function detach($node) {        $node->getPrevious()->setNext($node->getNext());        $node->getNext()->setPrevious($node->getPrevious());    }}/** * Class that represents a node in a doubly linked list */class Node {    private $key;    // the content of the node    private $data;    // the next node    private $next;    // the previous node    private $previous;    public function __construct($key, $data) {        $this->key = $key;        $this->data = $data;    }    public function setData($data) {        $this->data = $data;    }    public function setNext($next) {        $this->next = $next;    }    public function setPrevious($previous) {        $this->previous = $previous;    }    public function getKey() {        return $this->key;    }    public function getData() {        return $this->data;    }    public function getNext() {        return $this->next;    }    public function getPrevious() {        return $this->previous;    }}

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.