V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
matrix1010
V2EX  ›  Python

Python 高性能内存缓存库 Theine & 高并发优化 Asyncio 缓存框架 Cacheme

  •  
  •   matrix1010 ·
    Yiling-J · 2023-03-06 21:26:02 +08:00 · 1437 次点击
    这是一个创建于 388 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近对两个库又做了些完善,欢迎试用。顺便附上 benchmarks ,都是与比较流行的库进行对比。具体说明可以去看项目的 Readme ,benchmarks 也是 Readme 里直接复制过来的

    - Theine: https://github.com/Yiling-J/theine

    10k 请求测试

    Read Write Mix Zipf
    Theine(W-TinyLFU) API 3.42 ms 10.14 ms
    Theine(W-TinyLFU) Auto-Key Decorator 7.17 ms 18.41 ms 13.18 ms
    Theine(W-TinyLFU) Custom-Key Decorator 6.45 ms 17.67 ms 11.50 ms
    Cachetools LFU Decorator 15.70 ms 627.10 ms 191.04 ms
    Cacheout LFU Decorator 50.05 ms 704.70 ms 250.95 ms
    Theine(LRU) Custom-Key Decorator 5.70 ms 16.04 ms 10.91 ms
    Cachetools LRU Decorator 14.05 ms 61.06 ms 36.89 ms
    Cacheout LRU Decorator 47.90 ms 94.94 ms 68.25 ms

    - Cacheme: https://github.com/Yiling-J/cacheme

    简单说明一下 benchmarks 先从 redis 获取数据,不存在时调用 load 函数生成数据存入 redis 。load 函数会 sleep 100 ms 。

    200k 请求测试

    1k 并发

    Time Redis GET Load Hits
    Cacheme 30 s 166454 55579
    Cacheme-2 18 s 90681 55632
    Aiocache 46 s 200000 56367
    Aiocache-2 63 s 256492 55417
    Cashews 51 s 200000 56920
    Cashews-2 134 s 200000 55450

    10k 并发

    Time Redis GET Load Hits
    Cacheme 32 s 123704 56736
    Cacheme-2 20 s 83750 56635
    Aiocache 67 s 200000 62568
    Aiocache-2 113 s 263195 55507
    Cashews 68 s 200000 66036
    Cashews-2 175 s 200000 55709

    100k 并发

    Time Redis GET Load Hits
    Cacheme 30 s 60990 56782
    Cacheme-2 27 s 55762 55588
    Aiocache 80 s 200000 125085
    Aiocache-2 178 s 326417 65598
    Cashews 88 s 200000 87894
    Cashews-2 236 s 200000 55647
    4 条回复    2024-03-27 23:57:00 +08:00
    qile1
        1
    qile1  
       2023-03-16 19:41:07 +08:00 via Android
    方便说下用来干啥,如何使用,使用场景
    能 fastapi 调用当缓存吗?
    matrix1010
        2
    matrix1010  
    OP
       2023-03-16 20:50:35 +08:00
    @qile1 Theine 是高速本地内存缓存,Cacheme 是同时支持本地 /远程缓存的缓存框架。如何使用请参考对应项目 github 上的 readme 。使用场景参考各类缓存及我用来对比的几个库的使用场景。Theine 自带 Django cache 支持,没用过 fastapi 不了解
    1311317
        3
    1311317  
       1 天前 via Android
    大佬,这个和 redis 的区别是什么?
    matrix1010
        4
    matrix1010  
    OP
       1 天前
    @1311317 老哥 redis 是这样工作的:Python 对象 - 序列化成字节 - 发送 tcp 请求把字节发送到 redis 服务器 - redis 存储。而我这个是这样工作的:Python 对象 - Theine 里的字典。读取就是反过来,我就不重写一遍了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   994 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 73ms · UTC 22:17 · PVG 06:17 · LAX 15:17 · JFK 18:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.