網站首頁 編程語言 正文
本文實例為大家分享了Python實現FIFO緩存置換算法的具體代碼,供大家參考,具體內容如下
在上一節中我們實現了雙向鏈表DoubleLinkedList類,本節我們基于雙向鏈表實現FIFO(先進先出)緩存置換算法。
一、FIFO實現
代碼邏輯很簡單,就是遵循先進先出的原則,具體流程都寫在注釋中了。通過一個map來實現查找時的O(1)復雜度
class FIFOCache(object):
? ? def __init__(self, capacity=0xffffffff):
? ? ? ? """
? ? ? ? FIFO緩存置換算法
? ? ? ? :param capacity:
? ? ? ? """
? ? ? ? self.capacity = capacity
? ? ? ? self.map = {}
? ? ? ? self.size = 0
? ? ? ? self.list = DoubleLinkedList(capacity)
? ? def get(self, key):
? ? ? ? """
? ? ? ? 獲取元素
? ? ? ? ? ? 不存在 返回None
? ? ? ? ? ? 已存在 則返回緩存值
? ? ? ? :param key:
? ? ? ? :return:
? ? ? ? """
? ? ? ? # 當前緩存中不存在
? ? ? ? if key not in self.map:
? ? ? ? ? ? return None
? ? ? ? # 當前緩存中存在
? ? ? ? node = self.map.get(key)
? ? ? ? return node.value
? ? def put(self, key, value):
? ? ? ? """
? ? ? ? 添加元素
? ? ? ? ? ? 已存在 更新值并添加至鏈表尾部
? ? ? ? ? ? 不存在 判斷緩存容量大小后添加
? ? ? ? :param key:
? ? ? ? :param value:
? ? ? ? :return: 已添加的節點
? ? ? ? """
? ? ? ? # 當前緩存中已存在
? ? ? ? if key in self.map:
? ? ? ? ? ? node = self.map.get(key)
? ? ? ? ? ? self.list.remove(node)
? ? ? ? ? ? node.value = value
? ? ? ? ? ? self.list.append(node)
? ? ? ? else:
? ? ? ? ? ? # 緩存容量達到上限 刪除頭結點
? ? ? ? ? ? if self.size >= self.capacity:
? ? ? ? ? ? ? ? old_node = self.list.pop()
? ? ? ? ? ? ? ? del self.map[old_node.key]
? ? ? ? ? ? ? ? self.size -= 1
? ? ? ? ? ? node = Node(key, value)
? ? ? ? ? ? self.map[key] = node
? ? ? ? ? ? self.list.append(node)
? ? ? ? ? ? self.size += 1
? ? ? ? return node
? ? def print(self):
? ? ? ? """
? ? ? ? 打印當前鏈表
? ? ? ? :return:
? ? ? ? """
? ? ? ? self.list.print()
? ? ? ? # print(self.map)
二、測試邏輯
if __name__ == '__main__':
? ? fifo_cache = FIFOCache(2)
? ? fifo_cache.put(1, 1)
? ? fifo_cache.print()
? ? fifo_cache.put(2, 2)
? ? fifo_cache.print()
? ? print(fifo_cache.get(2))
? ? fifo_cache.put(3, 3)
? ? fifo_cache.print()
? ? print(fifo_cache.get(1))
? ? fifo_cache.put(2, 4)
? ? fifo_cache.print()
測試結果:
原文鏈接:https://blog.csdn.net/wang_xiaowang/article/details/105911640
相關推薦
- 2022-10-29 css屬性選擇器*=與~=的區別
- 2022-05-06 golang導入私有倉庫報錯:“server response: not found:xxx: in
- 2022-10-18 pandas重復行刪除操作df.drop_duplicates和df.duplicated的區別_p
- 2022-06-19 C++簡明講解類型轉換的使用與作用_C 語言
- 2022-07-04 如何用python實現結構體數組_python
- 2023-04-20 el-table多選+搜索
- 2022-07-28 C語言實現會員計費系統_C 語言
- 2022-04-09 MyBatis 查詢返回數據類型Map,空字段數據不返回
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支