美文网首页
lua-resty-lrucache实践

lua-resty-lrucache实践

作者: MOIC_Qu | 来源:发表于2020-05-12 17:51 被阅读0次

    近期想继续深入学习一下openresty,课程笔记~ lua-resty-lrucache仓库实践,api调用
    学习lua-resty-*仓库步骤:1 文档 2目录结构 3 测试案例

    Lua-resty-lrucache

    基于FFI(Foreign Function Interface)实现的LRU缓存库,用于单个worker,如果多个worker间共享,可使用shared dict。 Foreign Function Interface 指 在A语言中调用B语言的机制,泛指其他语言调用C的函数。 代码仓库:https://github.com/openresty/lua-resty-lrucache 仓库内含有两种版本实现,api是一致的。纯ffi(resty.lrucache.pureffi)和非纯ffi(resty.lrucache) resty.lrucache 基于lua table 实现缓存 适用于命中率高的情况 (不适合频繁删除元素) resty.lrucache 基于ffi hash 表 适用于命中率较低的情况

    文档

    双向链表实现的双端队列,使用可变长度数组

    目录结构
    lua-resty-lrucache
    ├── dist.ini //opm
    ├── lib
    │   └── resty //lua代码
    │       ├── lrucache
    │       │   └── pureffi.lua
    │       └── lrucache.lua
    ├── Makefile 
    ├── README.markdown //文档
    ├── t //test 测试样例 可通过测试样例了解api的具体使用方法
    │   ├── 001-sanity.t
    │   ├── 002-should-store-false.t
    │   ├── 003-init-by-lua.t
    │   ├── 004-flush-all.t
    │   ├── 005-capacity.t
    │   ├── 006-count.t
    │   ├── 007-get-keys.t
    │   ├── 008-user-flags.t
    │   ├── 100-pureffi // 测试纯ffi实现的接口
    │   │   ├── 001-sanity.t
    │   │   ├── 002-should-store-false.t
    │   │   ├── 003-init-by-lua.t
    │   │   ├── 004-flush-all.t
    │   │   ├── 005-capacity.t
    │   │   ├── 006-count.t
    │   │   ├── 007-get-keys.t
    │   │   └── 008-user-flags.t
    │   ├── 101-mixed.t
    │   └── TestLRUCache.pm
    └── valgrind.suppress //内存泄露误报</pre>
    
    Method

    new 创建

    syntax: cache, err = lrucache.new(max_items [, load_factor])
    

    get 获取值

    syntax: data, stale_data, flags = cache:get(key)
    

    set 存储值

    syntax: cache:set(key, value, ttl?, flags?)
    

    delete 删除

    syntax: cache:delete(key)
    

    count 计数

    syntax: count = cache:count()
    

    capacity 容量

    syntax: size = cache:capacity()
    

    get_keys 获取多个key

    syntax: keys = cache:get_keys(max_count?, res?)
    

    flush_all 刷新

    syntax: cache:flush_all()
    
    Example
     location = ./t {
       lrucache = require "resty.lrucache"
       --new
       local c, err = lrucache.new(2)
       if not c then
         ngx.log(ngx.ERR, "failed init lrucache: ", err)
         return
       end
    
       --set get
       c:set("key1",1)
       ngx.say("key1:",c:get("key1"))
    
       c:set("key2",2)
       ngx.say("key2:",c:get("key2"))
    
       c:set("key2",9)
       ngx.say("key2:",c:get("key2"))
    
       c:set("key3",3)
       ngx.say("key3:",c:get("key3"))
       ngx.say("key1:",c:get("key1"))
    
       --delete
       c:delete("key2")
       ngx.say("key2:",c:get("key2"))
    
       --count
       ngx.say("count:",c:count())
    
       c:set("key4:",4)
       ngx.say("count:",c:count())
    
       --flush_all
       c:flush_all()
       ngx.say("count:",c:count())
    
       --capacity
       ngx.say("cap:",c:capacity())
    
       --get_keys stack模式 先进在队尾
       c:set("key1",1)
       c:set("key2",2)
       local keys = c:get_keys()
       ngx.say("key_size:",#keys)
       ngx.say("MRU: ", keys[1])
       ngx.say("LRU: ", keys[#keys])
    }
    ​
    ​
    ---output---
    key1:1
    key2:2
    key2:9
    key3:3
    key1:nil
    key2:nil
    count:1
    count:2
    count:0
    cap:2
    key_size:2
    MRU: key2
    LRU: key1
    ------
    

    相关文章

      网友评论

          本文标题:lua-resty-lrucache实践

          本文链接:https://www.haomeiwen.com/subject/dnhinhtx.html