Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Nginx cache api delete

322 views

Published on

nginx cache api delete by Lua

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Nginx cache api delete

  1. 1. Nginx Cache Delete API I want to use Nginx cache delete api. Yuki Iwamoto 02/12/2019
  2. 2. Who am I? • Yuki Iwamoto • Freelance Engineer • Quitting smoking (40days)
  3. 3. I’m searching for a Nginx function • Delete cache function • Only one uri (Not all cache)
  4. 4. Example • I want to delete only this uri cache.
 http://localhost/entry/11/112233 • Sending request like this, 
 then delete “/entry/11/112233” cache
 http://localhost:8080/cache_clear_uri/entry/11/112233
  5. 5. About nginx cache • File name is md5 hashed from proxy_cache_key • The first folder would be the last (n) character
 from levels in proxy_cache_path • find and delete the file
  6. 6. proxy_cache_path /data/nginx/cache/yi levels=1:1:2 keys_zone=yi:256m max_size=100g inactive=7d; proxy_cache_key “$uri"; server { listen 8080 default_server; server_name localhost; location ~ /cache_clear_uri/(.*) { set $cache_uri $1; content_by_lua ' local cjson = require “cjson" local cache_base_path = "/data/nginx/cache/yi" local cache_uri = "/" .. ngx.var.cache_uri local uri_md5 = ngx.md5( cache_uri ) local first_path = string.sub( uri_md5, -1, -1 ) local second_path = string.sub( uri_md5, -2,-2 ) local third_path = string.sub( uri_md5, -4, -3 ) local cache_file = cache_base_path .. "/" .. first_path .. "/" .. second_path .. "/" .. third_path .. "/" .. uri_md5 local code = os.execute( "/bin/rm " .. cache_file ) ngx.say(cjson.encode({ code = code , cache_file = cache_file })) ngx.header.content_type = "application/json; charset=utf-8" '; } }
  7. 7. • (.*) = ($1) setting to cache_uri
 
 set $cache_uri $1;
  8. 8. • When proxy_cache_key is uri, 
 this case is only uri
 
 proxy_cache_key “$uri";
  9. 9. • Convert uri to md5
 
 local uri_md5 = ngx.md5( cache_uri )
  10. 10. • The first folder would be the last (1) character (from -1 to -1)
 from levels in proxy_cache_path
 
 local first_path = string.sub( uri_md5, -1, -1 ) • The second folder would be the last (2) character(from -2 to -2)
 from levels in proxy_cache_path
 
 local second_path = string.sub( uri_md5, -2,-2 ) • The third folder would be the last (3) character (from -3 to -4)
 from levels in proxy_cache_path
 local third_path = string.sub( uri_md5, -4, -3 )
  11. 11. • Delete by os.execute ( rm command )
 
 local code = os.execute( "/bin/rm " .. cache_file )
 
 
 

  12. 12. • Return json (delete or not , and which file)
 
 ngx.say(cjson.encode({ code = code , cache_file = cache_file })) ngx.header.content_type = "application/json; charset=utf-8" { “code”:0, “cache_file”:” /data/nginx/cache/yi/ab/0/1/123ab456cd789ef” } • Json result example
  13. 13. I found a better solution • https://scene-si.org/2016/11/02/purging-cached- items-from-nginx-with-lua/
  14. 14. Conclusion • We can do something by Lua • Sometimes, difficult to find a good solution

×