NGINX for the Developer
[ 162 ]
Caching in the database
If your application currently caches prerendered pages in a database, it should be
possible without too much additional effort to place those pages into a memcached
instance instead. NGINX is capable of answering requests directly from what is
stored in memcached. The logic is shown in the following figure:
The interface is very simple, allowing it to be as flexible as possible. NGINX looks
up a key in the store. If it is found, the value is returned to the client. Constructing
the proper key is a configuration task, which we will discuss next. Storing the value
at that key is outside the scope of what NGINX was designed to do. That job belongs
to the application.
Determining which key to use is a fairly simple task. For resources that
are not personalized, the best key to use is the URI itself. This is set in the
$memcached_key variable:
location / {
set $memcached_key $uri;
memcached_pass 127.0.0.1:11211;
}