px普贤教程PuXian
普贤教程 › 包参考

cache 包

TTL / LRU 缓存

项值
包名cache
版本0.2.0
可用版本0.1.0 / 0.2.0
入口cache.px
分类并发与通用模式
文件数 / 行数1 / 151

安装与引入

bash
pxpkg init          # 首次
pxpkg add cache@0.2.0
pxpkg install
px
import cache
所属分类教程:并发与通用模式

公开接口

函数参数
ca_newmax_entries=100
ca_setcache, key, val, ttl_sec=0, now=null
ca_getcache, key, now=null
ca_hascache, key, now=null
ca_lencache, now=null
ca_deletecache, key
其余 11 个 def(内部与辅助,可直接调用但不属稳定接口)
函数参数
ca_nownow
ca_droporder, key
ca_cleancache, now
ca_touchcache, k
ca_clearcache
ca_keyscache, now=null
ca_valuescache, now=null
ca_entriescache, now=null
ca_statscache, now=null
ca_peekcache, key, now=null
ca_set_manycache, pairs, ttl_sec=0, now=null

包内说明(源码头注释)

text
============================================================
普贤生态库:registry/cache —— TTL / LRU 缓存
------------------------------------------------------------
纯函数库(无 IO、无状态,缓存状态以 dict 显式传递),双模式一致。
ca_new(max_entries=100) → dict
ca_set(cache, key, val, ttl_sec=0, now=null) → cache   # ttl 0 = 不过期
ca_get(cache, key, now=null) → Ok(v) | Err            # 缺失/过期 Err
ca_has / ca_len / ca_delete
淘汰:LRU(MRU 在 order 尾部);超限驱逐队首。过期惰性清理。
时间:now 显式传入(默认 now_sec()),测试可控、库本身无 IO。
============================================================