当前位置: 首页 > 工具软件 > Resty > 使用案例 >

openresty lua-resty-lrucache缓存

吉鸿宝
2023-12-01

openresty lua-resty-lrucache缓存

         

lua-resty-lrucache:https://github.com/openresty/lua-resty-lrucache

            

                   

                                       

lrucache 缓存

          

lrucache.new:创建缓存实例

语法格式:cache, err = lrucache.new(max_items [, load_factor])
* 创建缓存实例,如果创建失败,返回nil、错误信息
* max_items:缓存实例最多保存的缓存数
* load_factor:负载因子,加载resty.lrucache.pureffi使用,取值范围:[0.1-1],默认0.5

            

set:设置缓存

语法格式:cache:set(key, value, ttl?, flags?)
* 设置缓存
* ttl:过期时间,如果设置为nil(默认),表示永不过期
* flags:用户标识

        

get:获取缓存

语法格式:data, stale_data, flags = cache:get(key)
* 获取缓存值
* 如果key不存在,或者已经过期,返回nil
* stale_data:从0.0.3版本开始,如果可以获取过期值,也会返回
* flags:如果没有设置,默认返回0

          

delete:删除缓存值

语法格式:cache:delete(key)
* 删除缓存值

          

count:返回存储的缓存个数,包含过期的缓存

语法格式:count = cache:count()

Returns the number of items currently stored in the cache 
including expired items if any.
* 返回缓存个数,包含过期的缓存

The returned count value will always be greater or equal to 0 and 
smaller than or equal to the size argument given to cache:new
* 返回的count永远 >= 0,<= cache:new设置的最大值

             

capacity:返回最多包含的缓存个数

语法格式:size = cache:capacity()

Returns the maximum number of items the cache can hold. 
The return value is the same as the size argument given 
to cache:new when the cache was created
* 返回缓存最多可包含的缓存个数(cache:new设置的值)

            

get_keys:获取缓存的key列表

语法格式:keys = cache:get_keys(max_count?, res?)

Fetch the list of keys currently inside the cache up to max_count. 
The keys will be ordered in MRU fashion (Most-Recently-Used keys first).
* 获取缓存的key列表
* max_count限制返回的key的个数
* key会按照使用次数排序,最常使用的排在最前面

This function returns a Lua (array) table (with integer keys) containing the keys.
* 函数返回lua table(数组形式)

When max_count is nil or 0, all keys (if any) will be returned.
* 如果max_count设置为nil、0,会返回所有的key

When provided with a res table argument, this function will not allocate a 
table and will instead insert the keys in res, along with a trailing nil value
* 如果提供了res table,会将结果插入res,不会创建新表

            

flush_all:删除所有缓存

语法格式:cache:flush_all()

Flushes all the existing data (if any) in the current cache instance. 
This is an O(1) operation and should be much faster than creating a 
brand new cache instance.
* 删除所有的数据
* 这是一个o(1)操作,比创建cache实例更快

Note however that the flush_all() method of resty.lrucache.pureffi 
is an O(n) operation
* resty.lrucache.pureffi 中,flush_all是o(n)操作

         

               

                                       

使用示例

          

default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    location /test {
        content_by_lua_block {
            local lrucache = require 'resty.lrucache';

            local cache, err = lrucache.new(100);
            if not cache then 
                ngx.say("创建缓存实例失败");
            end

            ngx.say("添加缓存 ==> ", "cache:set('1','瓜田李下')");
            ngx.say("添加缓存 ==> ", "cache:set('2','瓜田李下 2')");
            ngx.say("添加缓存 ==> ", "cache:set('3','瓜田李下 3')");
            ngx.say("添加缓存 ==> ", "cache:set('4','瓜田李下 4')");

            cache:set('1','瓜田李下');
            cache:set('2','瓜田李下 2');
            cache:set('3','瓜田李下 3');
            cache:set("4","瓜田李下 4");

            ngx.say("\n删除缓存 ==> ", "cache:delete('3')");
            cache:delete('3');

            ngx.say("\n获取缓存 ==> ", "cache:get('1')");
            ngx.say("获取缓存 ==> ", "cache:get('2')");
            ngx.say("获取缓存 ==> ", "cache:get('3')");
            ngx.say("获取缓存 ==> ", "cache:get('4')");
 
            local data, stale_data, flags = cache:get('1');
            if not data then 
                ngx.say("获取缓存1 出错");
            end
            ngx.say("缓存 1 ==> ", data);

            data, stale_data, flags = cache:get('2');
            if not data then 
                ngx.say("获取缓存2 出错");
            end
            ngx.say("缓存 2 ==> ", data);

            data, stale_data, flags = cache:get('3');
            if not data then 
                ngx.say("获取缓存3 出错");
            end
            ngx.say("缓存 3 ==> ", data);

            data, stale_data, flags = cache:get('4');
            if not data then 
                ngx.say("获取缓存4 出错");
            end
            ngx.say("缓存 4 ==> ", data);

            ngx.say("\n缓存容量 ==> ", cache:capacity());
            ngx.say("缓存个数 ==> ", cache:count());
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }

}

             

创建容器

docker run -it -d --net fixed --ip 172.18.0.102 -p 8002:80 \
-v /Users/huli/lua/openresty/cache/default.conf:/etc/nginx/conf.d/default.conf \
--name open-cache2 lihu12344/openresty

           

使用测试

huli@hudeMacBook-Pro cache % curl localhost:8002/test
添加缓存 ==> cache:set('1','瓜田李下')
添加缓存 ==> cache:set('2','瓜田李下 2')
添加缓存 ==> cache:set('3','瓜田李下 3')
添加缓存 ==> cache:set('4','瓜田李下 4')

删除缓存 ==> cache:delete('3')

获取缓存 ==> cache:get('1')
获取缓存 ==> cache:get('2')
获取缓存 ==> cache:get('3')
获取缓存 ==> cache:get('4')
缓存 1 ==> 瓜田李下
缓存 2 ==> 瓜田李下 2
获取缓存3 出错
缓存 3 ==> nil
缓存 4 ==> 瓜田李下 4

缓存容量 ==> 100
缓存个数 ==> 3

           

                

 类似资料: