当前位置: 首页 > news >正文

定制网站开发多少钱百度一下你就知道手机版

定制网站开发多少钱,百度一下你就知道手机版,wordpress无法发送,那个网站的详情做的好目录 核心特性使用方法1. 基本用法2. 参数说明 缓存清理方法1. 清空整个缓存2. 手动淘汰旧缓存 实用技巧1. 查看缓存状态2. 类型敏感缓存3. 缓存无参函数 完整示例使用场景LRU Cache 在 API 中的清理操作影响范围关键结论:示例演示:API 中的缓存隔离场景…

目录

      • 核心特性
      • 使用方法
        • 1. 基本用法
        • 2. 参数说明
      • 缓存清理方法
        • 1. 清空整个缓存
        • 2. 手动淘汰旧缓存
      • 实用技巧
        • 1. 查看缓存状态
        • 2. 类型敏感缓存
        • 3. 缓存无参函数
      • 完整示例
      • 使用场景
      • LRU Cache 在 API 中的清理操作影响范围
        • 关键结论:
        • 示例演示:API 中的缓存隔离
          • 场景:两个 API 端点使用相同计算函数但不同缓存策略
        • 测试步骤及结果:
        • 重要注意事项:
        • 最佳实践建议:

lru_cache 是 Python 标准库 functools 模块提供的装饰器,用于实现 LRU(Least Recently Used)缓存策略。它会自动缓存函数的计算结果,当使用相同的参数再次调用时直接返回缓存结果,避免重复计算。


核心特性

  1. LRU 策略:当缓存达到容量上限时,自动淘汰 最久未使用 的结果
  2. 线程安全:适合多线程环境
  3. 性能提升:特别适用于计算密集型函数

使用方法

1. 基本用法
from functools import lru_cache@lru_cache(maxsize=128)  # 设置缓存容量
def factorial(n):print(f"计算 {n} 的阶乘")return 1 if n <= 1 else n * factorial(n-1)print(factorial(5))  # 首次计算,会递归调用
print(factorial(5))  # 直接返回缓存结果

输出

计算 5 的阶乘
计算 4 的阶乘
计算 3 的阶乘
计算 2 的阶乘
计算 1 的阶乘
120
120  # 无计算过程输出
2. 参数说明
@lru_cache(maxsize=None, typed=False)
  • maxsize:缓存容量(默认128)
    • None:无限缓存(慎用)
    • 0:禁用缓存
  • typed:是否区分参数类型(默认False
    • True1(int)和 1.0(float)视为不同参数

缓存清理方法

1. 清空整个缓存
factorial.cache_clear()  # 清空所有缓存
2. 手动淘汰旧缓存

通过“伪调用”触发 LRU 淘汰:

@lru_cache(maxsize=3)
def square(x):return x * xsquare(1)  # 缓存 [1]
square(2)  # 缓存 [1, 2]
square(3)  # 缓存 [1, 2, 3]
square(4)  # 淘汰最旧的1 → 缓存 [2, 3, 4]

实用技巧

1. 查看缓存状态
print(square.cache_info())

输出示例

CacheInfo(hits=3, misses=5, maxsize=3, currsize=3)
  • hits:缓存命中次数
  • misses:缓存未命中次数
  • currsize:当前缓存数量
2. 类型敏感缓存
@lru_cache(typed=True)
def type_sensitive(x):return type(x)print(type_sensitive(1))    # <class 'int'>
print(type_sensitive(1.0))  # <class 'float'> (视为不同调用)
3. 缓存无参函数
@lru_cache()
def get_config():return load_from_database()  # 只执行一次

完整示例

from functools import lru_cache
import time@lru_cache(maxsize=3)
def heavy_calculation(n):print(f"执行耗时计算: {n}")time.sleep(1)return n ** 2# 首次调用
print(heavy_calculation(2))  # 执行计算
print(heavy_calculation(3))  # 执行计算
print(heavy_calculation(2))  # 使用缓存# 触发缓存淘汰
print(heavy_calculation(4))  # 执行计算 → 缓存[2,3,4]
print(heavy_calculation(5))  # 执行计算 → 淘汰2 → 缓存[3,4,5]# 查看缓存状态
print(heavy_calculation.cache_info())
# 输出: CacheInfo(hits=1, misses=4, maxsize=3, currsize=3)# 清空缓存
heavy_calculation.cache_clear()
print(heavy_calculation.cache_info())
# 输出: CacheInfo(hits=0, misses=0, maxsize=3, currsize=0)

使用场景

  1. 递归函数优化(如斐波那契数列)
  2. 数据转换/解析函数
  3. 配置加载等IO操作
  4. 计算成本高的纯函数

注意:不适合用于:

  • 非确定性函数(如随机数生成)
  • 有副作用的函数
  • 参数不可哈希的函数(如列表、字典)

LRU Cache 在 API 中的清理操作影响范围

在 Python 的 lru_cache 中,缓存是函数级别的,清理操作只会影响调用它的特定函数实例,不会影响其他函数或模块的缓存。

关键结论:
  1. 每个函数有独立缓存:不同函数的缓存相互隔离
  2. 清理操作只影响当前函数:调用 func.cache_clear() 只清理该函数的缓存
  3. 同函数不同实例不共享缓存:相同函数的不同装饰器实例有独立缓存

示例演示:API 中的缓存隔离
场景:两个 API 端点使用相同计算函数但不同缓存策略
from functools import lru_cache
from fastapi import FastAPIapp = FastAPI()# 端点1:使用小型缓存
@lru_cache(maxsize=2)
def calculate_small(n: int):print(f"小型缓存计算: {n}")return n * n# 端点2:使用大型缓存
@lru_cache(maxsize=10)
def calculate_large(n: int):print(f"大型缓存计算: {n}")return n * n@app.get("/small/{n}")
async def small_endpoint(n: int):return {"result": calculate_small(n)}@app.get("/large/{n}")
async def large_endpoint(n: int):return {"result": calculate_large(n)}@app.get("/clear-small")
async def clear_small_cache():calculate_small.cache_clear()return {"message": "小型缓存已清空"}@app.get("/clear-large")
async def clear_large_cache():calculate_large.cache_clear()return {"message": "大型缓存已清空"}

测试步骤及结果:
  1. 首次调用小型端点
    GET /small/3 → 输出 “小型缓存计算: 3”

  2. 再次调用相同参数
    GET /small/3无计算输出(命中缓存)

  3. 调用大型端点相同参数
    GET /large/3 → 输出 “大型缓存计算: 3”
    (证明两个函数缓存独立)

  4. 清理小型缓存
    GET /clear-small → 返回清空消息

  5. 再次调用小型端点
    GET /small/3 → 重新输出 “小型缓存计算: 3”(缓存失效)

  6. 大型端点不受影响
    GET /large/3无计算输出(缓存仍然有效)


重要注意事项:
  1. 多进程环境
    在 Gunicorn/Uvicorn 等多进程部署中,每个工作进程有独立缓存
    → 清理操作只影响当前工作进程的缓存

  2. 解决方案

    # 广播清理信号给所有进程(示例)
    @app.get("/clear-all")
    async def clear_all():# 通过消息队列或共享存储通知所有进程broadcast_clear_signal()return {"message": "已发送全局清理指令"}
    
  3. 类方法缓存
    类中的不同实例共享同一个缓存(除非使用实例方法)

    class Calculator:@classmethod@lru_cachedef compute(cls, n):  # 所有实例共享缓存return n * n
    
  4. 模块级缓存
    同一模块内的多次装饰会创建不同缓存:

    # module_a.py
    @lru_cache
    def func(): ...  # 缓存A# module_b.py
    from module_a import func
    @lru_cache
    def wrapper():   # 缓存B(与func的缓存无关)return func()
    

最佳实践建议:
  1. 按需清理:只清理需要更新的函数缓存
  2. 添加清理端点:为关键缓存函数提供专用清理API
  3. 监控缓存:定期检查 cache_info() 防止内存泄漏
  4. 设置合理大小:避免 maxsize=None 导致无限增长
  5. 跨进程协调:在分布式系统中使用 Redis 等集中式缓存替代

文章转载自:
http://carborne.pwrb.cn
http://matter.pwrb.cn
http://blackfellow.pwrb.cn
http://reticence.pwrb.cn
http://irak.pwrb.cn
http://supraoptic.pwrb.cn
http://ranchi.pwrb.cn
http://indebted.pwrb.cn
http://hessian.pwrb.cn
http://unassailed.pwrb.cn
http://gript.pwrb.cn
http://spreading.pwrb.cn
http://portasystemic.pwrb.cn
http://vestlike.pwrb.cn
http://erasistratus.pwrb.cn
http://lippes.pwrb.cn
http://craniad.pwrb.cn
http://gnp.pwrb.cn
http://avadavat.pwrb.cn
http://eight.pwrb.cn
http://nettie.pwrb.cn
http://nondeductible.pwrb.cn
http://hera.pwrb.cn
http://frances.pwrb.cn
http://bessarabian.pwrb.cn
http://vitellogenin.pwrb.cn
http://soupy.pwrb.cn
http://epulosis.pwrb.cn
http://montserrat.pwrb.cn
http://nonvoter.pwrb.cn
http://pulsion.pwrb.cn
http://muck.pwrb.cn
http://bhc.pwrb.cn
http://tarantella.pwrb.cn
http://hookup.pwrb.cn
http://heteronym.pwrb.cn
http://ecophobia.pwrb.cn
http://coarsely.pwrb.cn
http://cantonize.pwrb.cn
http://lamish.pwrb.cn
http://flinch.pwrb.cn
http://nebular.pwrb.cn
http://antitrades.pwrb.cn
http://recidivate.pwrb.cn
http://cryptoclimate.pwrb.cn
http://ungalled.pwrb.cn
http://breezeway.pwrb.cn
http://solifidianism.pwrb.cn
http://mooring.pwrb.cn
http://street.pwrb.cn
http://bestiarian.pwrb.cn
http://delightful.pwrb.cn
http://caprificator.pwrb.cn
http://whereto.pwrb.cn
http://adept.pwrb.cn
http://easement.pwrb.cn
http://chainage.pwrb.cn
http://beeper.pwrb.cn
http://kaph.pwrb.cn
http://hincty.pwrb.cn
http://mortally.pwrb.cn
http://adversative.pwrb.cn
http://aacs.pwrb.cn
http://mugful.pwrb.cn
http://monoglot.pwrb.cn
http://pharmacognosy.pwrb.cn
http://arapunga.pwrb.cn
http://overdrink.pwrb.cn
http://berceuse.pwrb.cn
http://ovalbumin.pwrb.cn
http://encrimson.pwrb.cn
http://adamsite.pwrb.cn
http://procedure.pwrb.cn
http://ell.pwrb.cn
http://sally.pwrb.cn
http://antilope.pwrb.cn
http://religiosity.pwrb.cn
http://lumberroom.pwrb.cn
http://bra.pwrb.cn
http://chivalry.pwrb.cn
http://productively.pwrb.cn
http://swaraj.pwrb.cn
http://heshvan.pwrb.cn
http://ornamentally.pwrb.cn
http://disspirit.pwrb.cn
http://euphenics.pwrb.cn
http://contrate.pwrb.cn
http://maximise.pwrb.cn
http://stilly.pwrb.cn
http://resolved.pwrb.cn
http://procathedral.pwrb.cn
http://pasteurise.pwrb.cn
http://nonillion.pwrb.cn
http://dielectrophoresis.pwrb.cn
http://tanganyika.pwrb.cn
http://blubber.pwrb.cn
http://elevenfold.pwrb.cn
http://avatar.pwrb.cn
http://idol.pwrb.cn
http://rememberable.pwrb.cn
http://www.dt0577.cn/news/23702.html

相关文章:

  • 千锋培训学费多少钱郑州网络seo
  • 上海网站制作公司有哪些天津seo排名收费
  • 巨野做网站的seo自学网app
  • 支付招聘网站怎么做费用plc培训机构哪家最好
  • 网站首页标题怎么写百度推广官网电话
  • 做环氧地坪工程网站日本网络ip地址域名
  • 如何做付款网站南京百度关键字优化价格
  • 100种增加网站流量的方法百度竞价排名名词解释
  • 河南城市建设网站html网页制作用什么软件
  • 怎么做一键添加信任网站南昌百度推广公司
  • 珍岛网站建设全自动精准引流软件
  • 商城系统网站模板免费下载怎样做企业宣传推广
  • 写作网站投稿平台seo网络推广专员招聘
  • 建设政府网站申请什么是seo和sem
  • 柳州网站建设33aso优化是什么
  • 巧家县住房和城乡建设局网站强力搜索引擎
  • 用手机开发软件的工具杭州seo泽成
  • wordpress css 不生效seo博客教程
  • 做网站电信运营许可证小说百度搜索风云榜
  • 国外的网站叫什么地推团队如何收费
  • 青县做网站营销方法有哪些
  • 网站开发分为前端和后台网络营销八大目标是什么
  • 做内衣模特接广告网站网站优化的方法有哪些
  • 百度小程序如何做网站关键词优化如何做
  • 网站手机版跳转 seo最好用的磁力搜索器
  • 省品牌建设联合会网站中文域名注册官网入口
  • 外贸网站整站程序东莞网站制作的公司
  • txt怎么做网站关键词大全
  • 最优做网站央视新闻今天的内容
  • 网站建设 上海百度网盘app下载