Coder Social home page Coder Social logo

agui888 / lua-resty-cache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lloydzhou/lua-resty-cache

0.0 2.0 0.0 18 KB

http cache to redis, can server stale response, and using "lua-resty-lock" only allow one request to populate a new cache

Makefile 6.82% Lua 56.36% Perl 36.83%

lua-resty-cache's Introduction

lua-resty-cache

one lua library to work with srcache, can server stale response, and using "lua-resty-lock" only allow one request to populate a new cache.

  1. if the cache is missing, skip the srcache_fetch, and make single request to populate a new cache, the other request with same cache_key, just wait update cache success.
  2. always set the redis expires to (real expires time + stale time), so can find the stale data from reids.
  3. if get stale data from redis, just send stale data to client(using ngx.eof(), the client can close this connection.)
  4. and then make subrequest to populate a new cache (using lua-resty-lock, so only one request send to backend server).

Install

git clone --branch feature-srcache --recursive https://github.com/lloydzhou/lua-resty-cache
cd lua-resty-cache && sudo make install

Synopsis

upstream www {
    server 127.0.0.1:9999;
}
upstream redis {
    server 127.0.0.1:6379;
    keepalive 1024;
}
lua_shared_dict srcache_locks 1m;
location /api {
    set $cache_lock srcache_locks;
    set $cache_ttl /redisttl;
    set $cache_persist /redispersist;
    set $cache_key "$http_user_agent|$uri";
    set $cache_stale 100;
    set $cache_lock_exptime 30;
    set $cache_backend_lock_timeout 0.01;
    set $cache_lock_timeout 3;
    set $cache_lock_timeout_wait 0.06;
    set $cache_skip_fetch "X-Skip-Fetch";
    set_escape_uri $escaped_key $cache_key;
    
    rewrite_by_lua_file /usr/local/openresty/lualib/resty/cache.lua;
    
    if ($http_x_skip_fetch != TRUE){ srcache_fetch GET /redis $cache_key;}
    srcache_store PUT /redis2 key=$escaped_key&exptime=105;
    add_header X-Cache $srcache_fetch_status;
    add_header X-Store $srcache_store_status;
    #echo hello world;
    proxy_pass http://www;
}
location = /redisttl {
    internal;
    set_unescape_uri $key $arg_key;
    set_md5 $key;
    redis2_query ttl $key;
    redis2_pass redis;
}
location = /redispersist {
    internal;
    set_unescape_uri $key $arg_key;
    set_md5 $key;
    redis2_query persist $key;
    redis2_pass redis;
}
location = /redis {
    internal;
    set_md5 $redis_key $args;
    redis_pass redis;
}
location = /redis2 {
    internal;
    set_unescape_uri $exptime $arg_exptime;
    set_unescape_uri $key $arg_key;
    set_md5 $key;
    redis2_query set $key $echo_request_body;
    redis2_query expire $key $exptime;
    redis2_pass redis;
}

lua-resty-cache's People

Contributors

lloydzhou avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.