How to make nginx proxy_cache_purge faster?

106 Views Asked by At

I have multiple different caches on nginx for my product_id=24133994 depending on GET params:

For example:

    httpsfoo.bar.com/api/final/24133994?lang_id=4&param1=2&param2=1
    httpsfoo.bar.com/api/final/24133994?lang_id=4&param1=8&param2=2
    httpsfoo.bar.com/api/final/24133994?lang_id=2&param1=2&param2=3
    httpsfoo.bar.com/api/final/24133994?lang_id=3&param1=8&param2=3
    ...
    httpsfoo.bar.com/api/final/24133994?lang_id=1&param1=9&param2=100

Zone config:

proxy_cache_path /mnt/ssd/nginx/cache/my_zone_final levels=1:2 keys_zone=my_zone_final:1024m inactive=1200m max_size=10g

Clearing cache with proxy_cache_purge by exact proxy_cache_key works fast enough (~150 ms), but I can't predict all possible values combinations for my "lang_id", "param1" and "param2" in future.

    location ~ ^/clear_cache_final(/.*) {
        proxy_cache_purge my_zone_final $scheme$host$1$is_args$args;
    }

So, I tried using asterisk for matching all possible keys for single product_id=24133994:

httpsfoo.bar.com/api/final/24133994*

It works correct, but it takes about 10 seconds to find and clear all caches, so I can't use it in production, because 100 requests at the same time will kill my SSD and system will be down.

Is there any way to make this process faster without brute force search? Maybe another proxy_cache_key or something like indices in DB? Or maybe there is a way to set separate folder for each product_id caches?

0

There are 0 best solutions below