visit
The subject of Web resource caching is as old as the World Wide Web itself. However, I'd like to offer an as-exhaustive-as-possible catalog of how one can improve performance by caching. Web resource caching can happen in two different places: client-side - on the browser and server side. In my previous post, I explained the former; this post focuses on the latter.
A couple of dedicated server-side resource caching solutions have emerged over the years: , , , etc. Other solutions are less focused on web resource caching and more generic, e.g., or .
routes:
- uri: /cache
upstream_id: 1
plugins:
proxy-rewrite:
regex_uri: ["/cache(.*)", "/$1"]
proxy-cache: ~
The default proxy-cache
configuration uses the default disk-based :
proxy_cache: # Proxy Caching configuration
cache_ttl: 10s # The default caching time in disk if the upstream does not specify the cache time
zones: # The parameters of a cache
- name: disk_cache_one # The name of the cache, administrator can specify
# which cache to use by name in the admin api (disk|memory)
memory_size: 50m # The size of shared memory, it's used to store the cache index for
# disk strategy, store cache content for memory strategy (disk|memory)
disk_size: 1G # The size of disk, it's used to store the cache data (disk)
disk_path: /tmp/disk_cache_one # The path to store the cache data (disk)
cache_levels: 1:2 # The hierarchy levels of a cache (disk)
- name: memory_cache
memory_size: 50m
We can test the setup with curl
:
curl -v localhost:9080/cache
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 147
< Connection: keep-alive
< Date: Tue, 29 Nov 2022 13:17:00 GMT
< Last-Modified: Wed, 23 Nov 2022 13:58:55 GMT
< ETag: "637e271f-93"
< Server: APISIX/3.0.0
< Apisix-Cache-Status: MISS #1
< Accept-Ranges: bytes
If we curl
again before the default cache expiration period (300 seconds), the response is from the cache:
< HTTP/1.1 200 OK
...
< Apisix-Cache-Status: HIT
< HTTP/1.1 200 OK
...
< Apisix-Cache-Status: EXPIRED
Note that we can explicitly purge the entire cache by using the custom PURGE
HTTP method:
curl localhost:9080/cache -X PURGE
Note that it's also possible to bypass the cache, e.g., for testing purposes. We can configure the plugin accordingly:
routes:
- uri: /cache*
upstream_id: 1
proxy-cache:
cache_bypass: ["$arg_bypass"] #1
bypass
query parameter with a non-0
value
curl -v localhost:9080/cache?bypass=please
< HTTP/1.1 200 OK
...
< Apisix-Cache-Status: BYPASS
To go further:
Originally published at on December 4<sup>th</sup>, 2022