二、实现页面置换算法
意义:页面置换算法作为高速缓存的运行算法是我们需要掌握的。
1、FIFO先进先出算法(first input first output)
把高速缓存看做是一个先进先出的队列;
优先替换最先进入队列的字块;
(I)、python版本
新建一个fifo.py文件
#! -*- encoding=utf-8 -*-# FIFO先进先出页面缓存置换算法# 1、当需要淘汰缓存时,把最先进入列表的字块淘汰from node.Node import Node,DoubleLinkedListclass FIFO(object):def __init__(self,capacity):self.capacity = capacityself.size = 0self.map = {}self.list = DoubleLinkedList(self.capacity)def get(self,key):if key not in self.map:return -1else:return self.map.get(key).valuedef put(self,key,value):if self.capacity == 0:returnif key in self.map:node = self.map.get(key)self.list.remove(node)node.value = valueself.list.append(node)else:if self.size == self.capacity:node = self.list.pop()del self.map[node.key]self.size -= 1node = Node(key,value)self.list.append(node)self.map[key] = nodeself.size += 1def print(self):self.list.print()# 测试逻辑if __name__ == '__main__':cache = FIFO(2)cache.put(1,1)cache.print()cache.put(2,2)cache.print()print(cache.get(1))cache.print()cache.put(3,3)cache.print()print(cache.get(2))cache.print()cache.put(4,4)cache.print()print(cache.get(1))# exit()# print(__name__)# print(__name__)
(II)、php版本
新建一个fifo.php文件
<?phprequire_once 'node/Node.php';# FIFO先进先出页面缓存置换算法# 1、当需要淘汰缓存时,把最先进入列表的字块淘汰class FIFO extends DoubleLinkedList{public $capacity;public $size = 0;public $map = [];public $list = [];public function __construct($capacity){$this->capacity = $capacity;$this->list = new DoubleLinkedList($capacity);}public function get($key){if(!array_key_exists($key,$this->map)) return -1;return $this->map[$key]->value;}public function put($key,$value){if($this->capacity == 0) return;if(array_key_exists($key,$this->map)){$node = $this->map[$key];$this->list->remove($node);$node->value = $value;$this->list->append($node);}else{if($this->size == $this->capacity){$node = $this->list->pop();unset($this->map[$node->key]);$this->size -= 1;}$node = new Node($key,$value);$this->list->append($node);$this->map[$key] = $node;$this->size += 1;}}public function console(){$this->list->console();}}//逻辑测试$cache = new FIFO(2);$cache->put(1,1);$cache->console();$cache->put(2,2);$cache->console();print($cache->get(1));$cache->console();$cache->put(3,3);$cache->console();print($cache->get(2));$cache->console();$cache->put(4,4);$cache->console();print($cache->get(1));$cache->console();print($cache->get(4));$cache->console();?>
2、LRU最近最少使用算法(Least Recently Used)
优先淘汰一段时间没有使用的字块;
有多种实现方式,一般使用双向链表;
把最新访问节点置于链表前端,保证链表头部节点是最近使用的;
(I)、python版本
新建一个lru.py文件
#! -*- encoding=utf-8 -*-# LRU最近最少使用页面置换算法# 剔除最少使用的字块,将最近使用的字块提前到链表头部from node.Node import Node,DoubleLinkedListclass LRU(object):def __init__(self,capacity):self.capacity = capacityself.map = {}self.list = DoubleLinkedList(self.capacity)self.size = 0def get(self,key):if key in self.map:node = self.map[key]self.list.remove(node)self.list.append_front(node)return node.valueelse:return -1def put(self,key,value):if key in self.map:node = self.map.get(key)self.list.remove(node)node.value = valueself.list.append_front(node)# passelse:node = Node(key,value)if self.size >= self.capacity:old_node = self.list.remove()self.map.pop(old_node.key)self.size -= 1self.list.append_front(node)self.map[key] = nodeself.size += 1# passdef print(self):self.list.print()# 测试逻辑if __name__ == '__main__':cache = LRU(2)cache.put(2,2)cache.print()cache.put(1,1)cache.print()cache.put(3,3)cache.print()print(cache.get(1))cache.print()print(cache.get(2))cache.print()print(cache.get(3))cache.print()
(II)、php版本
新建一个lru.php文件
<?phprequire_once 'node/Node.php';# LRU最近最少使用页面置换算法# 剔除最少使用的字块,将最近使用的字块提前到链表头部class LRU extends DoubleLinkedList{public $capacity;public $size = 0;public $map = [];public $list = [];public function __construct($capacity){$this->capacity = $capacity;$this->list = new DoubleLinkedList($this->capacity);}public function get($key){if(!array_key_exists($key,$this->map)) return -1;$node = $this->map[$key];$this->list->remove($node);$this->list->append_front($node);return $node->value;}public function put($key,$value){if(array_key_exists($key,$this->map)){$node = $this->map[$key];$this->list->remove($node);$node->value = $value;$this->list->append_front($node);}else{$node = new Node($key,$value);if($this->size >= $this->capacity){$old_node = $this->list->remove();unset($this->map[$old_node->key]);$this->size -= 1;}$this->list->append_front($node);$this->map[$key] = $node;$this->size += 1;}}public function console(){$this->list->console();}}//逻辑测试$cache = new LRU(2);$cache->put(2,2);$cache->console();$cache->put(1,1);$cache->console();$cache->put(3,3);$cache->console();print($cache->get(1));$cache->console();print($cache->get(2));$cache->console();print($cache->get(3));$cache->console();
文章转载自智慧大数据,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




