memcached integration idea + other ideas

Sergey Pofiriev parf at
Fri Oct 6 23:08:01 MSD 2006

Memcached integration as a caching mechanism


1. caching layer
    if url + params or F(url,params) is in memcached return cached data 
w/o backend call
     F(x) - function such as crc32 / md5 / sha / ...

     location /dir {
          mem_cache on;  // activate cache check prior to backend query

2. Static data auto caching:
    cache files according to criteria and specified timeout.
    Example: cache all gif / jpg (  /\.(jpg|gif)$/i  ) files for 10 minutes

    location ~* ^.+\.(jpg|jpeg|gif)$ {
       mem_cache on;                 //  turn on caching (#1)
       mem_cache_ttl 600;            //  default cache time

3. Dynamic data caching:
    Cache files according to  X-cache header
      header("X-cache: $timeout");   // cache url call for given timeout

4. Internal server redirects
    header("X-redirect: $timeout");

    same as in Java Servlets forward.
    a.    NGINX -> BACKEND1
    b.    backend returns X-redirect and URL in  REDIR_URL
    c.    NGINX -> REDIR_URL

    maybe this already implemented

5. Redirect to "hash key"
    When page returns X-redirect-cache header:
    Return page stored in memcached key (key = value of 
"X-redirect-cache" header)
    OR (if no page found)
    Internal redirect to  $no_key_url  and return content to caller
       in case of redirect server should pass "key" to $no_key_url ( as 
a header or as a GET field )

    header("X-redirect-cache: $key $no_key_url")

    // guess what we want to show
    $page="index-".random_number( from => 1, to => 10)
    header("X-cache-key: $page /page-generator")

More information about the nginx-ru mailing list