python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python LRU缓存应用

Python之LRU缓存应用与实例

作者:AI手记叨叨礼拜天

LRU(最近最少使用)是高效缓存淘汰算法,通过OrderedDict维护访问顺序,实现O(1)时间复杂度的get/put操作,适用于Web应用和配置管理,但不适用于强一致性场景或超大数据集

一、什么是LRU

LRU(Least Recently Used,最近最少使用)是一种常用的缓存淘汰算法,用于在缓存空间不足时决定哪些数据应该被移除。

核心思想

如果一个数据最近被访问过,那么它将来被访问的概率也更高。因此,当缓存空间不足时,应该优先淘汰最久未被访问的数据。

工作原理

访问数据时

缓存满时

主要特性

二、核心实现

1. 数据结构

使用 OrderedDict 存储键值对,并维护访问顺序:

2. 关键方法

__init__(self, capacity)

初始化缓存,设置最大容量。

cache = LatestCache(1000)  # 最大存储 1000 个条目

get(self, key)

获取缓存中的值,如果不存在则返回 None

value = cache.get("some_key")

put(self, key, value)

向缓存中添加或更新键值对。

cache.put("some_key", "some_value")

三、使用示例

1. 基本用法

from collections import OrderedDict

class LatestCache:
    def __init__(self, capacity):
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        if key not in self.cache:
            return None
        self.cache.move_to_end(key)  # 移至最新位置
        return self.cache[key]

    def put(self, key, value):
        if key in self.cache:
            self.cache.move_to_end(key)  # 更新时移至最新位置
        self.cache[key] = value
        if len(self.cache) > self.capacity:
            self.cache.popitem(last=False)  # 移除最旧的条目


# 初始化缓存
cache = LatestCache(3)

# 添加数据
cache.put("a", 1)
cache.put("b", 2)
cache.put("c", 3)

# 查询数据
print(cache.get("a"))  # 输出: 1

# 缓存满时自动淘汰
cache.put("d", 4)      # 淘汰最久未访问的键 "b"
print(cache.get("b"))  # 输出: None(已被淘汰)

2. 适用场景

四、优化建议

1. 线程安全改进

当前实现 非线程安全,多线程环境下可能导致数据竞争。可引入 threading.RLock 加锁:

from threading import RLock

class LatestCache:
    def __init__(self, capacity):
        self._lock = RLock()
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        with self._lock:
            if key not in self.cache:
                return None
            self.cache.move_to_end(key)
            return self.cache[key]

    def put(self, key, value):
        with self._lock:
            if key in self.cache:
                self.cache.move_to_end(key)
            self.cache[key] = value
            if len(self.cache) > self.capacity:
                self.cache.popitem(last=False)

2. 缓存命中率统计

增加 hitsmisses 统计,评估缓存效率:

from threading import RLock
from collections import OrderedDict


class LatestCache:
    def __init__(self, capacity):
        self._lock = RLock()
        self.hits = 0
        self.misses = 0
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        with self._lock:
            if key in self.cache:
                self.hits += 1
                self.cache.move_to_end(key)
                return self.cache[key]
            self.misses += 1
            return None

    def put(self, key, value):
        with self._lock:
            if key in self.cache:
                self.cache.move_to_end(key)
            self.cache[key] = value
            if len(self.cache) > self.capacity:
                self.cache.popitem(last=False)

    def hit_rate(self):
        with self._lock:
            total = self.hits + self.misses
            return (self.hits / total) if total > 0 else 0.0


# 初始化缓存
cache = LatestCache(3)

# 添加数据
cache.put("a", 1)
cache.put("b", 2)
cache.put("c", 3)

# 查询数据
print(cache.get("a"))  # 命中,输出: 1
print(cache.get("b"))  # 命中,输出: 2
print(cache.get("a"))  # 命中,输出: 1
print(cache.get("x"))  # 未命中,输出: None

# 缓存满时自动淘汰
cache.put("d", 4)  # 淘汰最久未访问的键 "c"
print(cache.get("c"))  # 未命中(已被淘汰),输出: None

# 查看命中率统计
print(f"命中次数: {cache.hits}")  # 输出: 3 (aba)
print(f"未命中次数: {cache.misses}")  # 输出: 2 (xc)
print(f"命中率: {cache.hit_rate():.2%}")  # 输出: 60.00% (3命中/(3命中+2未命中))

3. 支持 TTL

TTL(Time To Live)是数据在缓存中存活的生存时间,过期后自动失效。

from collections import OrderedDict
import time
import random


class LatestCache:
    def __init__(self, capacity):
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        if key not in self.cache:
            return None
        value, expire_time = self.cache[key]
        if expire_time and time.time() > expire_time:
            del self.cache[key]  # 自动清理过期数据
            return None
        self.cache.move_to_end(key)  # 更新为最近使用
        return value

    def put(self, key, value, ttl=None):
        expire_time = time.time() + ttl if ttl else None
        if key in self.cache:
            self.cache.move_to_end(key)
        self.cache[key] = (value, expire_time)
        if len(self.cache) > self.capacity:
            self.cache.popitem(last=False)  # 移除最久未使用的


# 初始化缓存(容量为3)
cache = LatestCache(3)

# 添加数据(带TTL和不带TTL的混合)
cache.put("a", 1, ttl=2)  # 2秒后过期
cache.put("b", 2)  # 永不过期
cache.put("c", 3, ttl=4)  # 4秒后过期

# 立即查询(全部命中)
print(f"初始查询: a={cache.get('a')}, b={cache.get('b')}, c={cache.get('c')}")
# 输出: 初始查询: a=1, b=2, c=3

# 模拟2秒后('a'已过期)
print("等待2秒后...")
time.sleep(2)

print(f"查询: a={cache.get('a')}, b={cache.get('b')}, c={cache.get('c')}")
# 输出: 查询: a=None , b=2, c=3

五、总结

1. 优点

2. 适用场景

3. 不适用场景

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文