zoukankan      html  css  js  c++  java
  • LRU算法实现

    LRU是Last Recent Used 缩写,做为一种缓存算法,将最近较少使用的缓存失效。memcache采用了该算法。如下采用了一种PHP的实现方式。该算法将每次新增的内容,放到缓存顶部,达到缓存极限时,将缓存底部的内容清除。可以通过如下PHP代码来模拟。

    <?php
    class LRUCache {
    
        private $head;
    
        private $tail;
    
        private $capacity;
    
        private $hashmap;
    
        public function __construct($capacity) {
            $this->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;
        }
    
    }
    





  • 相关阅读:
    Kubernetes 两步验证
    实战优化丨如何借助 CODING 实现云开发中的云函数的自动化部署
    弹性配置为构建提速
    一分钟开始持续集成之旅系列之:C 语言 + Makefile
    一分钟开始持续集成之旅系列之:Java + GWT
    静态网站架构的演进和最佳实践
    DevOps
    真香!CODING DevOps “极速构建计划”,再也不用担心构建慢了!
    CODING 敏捷实战系列课第二讲:Scrum 敏捷项目管理核心要素之 3355
    一分钟开始持续集成之旅系列之: Vue + 腾讯云 COS 上传部署
  • 原文地址:https://www.cnblogs.com/phonecom/p/6218455.html
Copyright © 2011-2022 走看看