[PATCH] Upstream: add $upstream_cache_age variable

Roman Arutyunyan arut at nginx.com
Fri Oct 3 13:29:09 UTC 2014


On 03 Oct 2014, at 01:22, Piotr Sikora <piotr at cloudflare.com> wrote:

> # HG changeset patch
> # User Piotr Sikora <piotr at cloudflare.com>
> # Date 1412284916 25200
> #      Thu Oct 02 14:21:56 2014 -0700
> # Node ID 488aa8f86041dbe99a8f8ebbb3df533b9ce3e19e
> # Parent  a215d9021f137b9e2d4f69c37c7f992a2bef12c6
> Upstream: add $upstream_cache_age variable.
> 
> This variable represents the amount of time that passed since the response
> was fetched (or successfully revalidated) from the upstream server.
> 
> Signed-off-by: Piotr Sikora <piotr at cloudflare.com>
> 
> diff -r a215d9021f13 -r 488aa8f86041 src/http/ngx_http_upstream.c
> --- a/src/http/ngx_http_upstream.c	Tue Sep 30 17:20:33 2014 +0400
> +++ b/src/http/ngx_http_upstream.c	Thu Oct 02 14:21:56 2014 -0700
> @@ -21,6 +21,8 @@ static ngx_int_t ngx_http_upstream_cache
>     ngx_http_variable_value_t *v, uintptr_t data);
> static ngx_int_t ngx_http_upstream_cache_etag(ngx_http_request_t *r,
>     ngx_http_variable_value_t *v, uintptr_t data);
> +static ngx_int_t ngx_http_upstream_cache_age(ngx_http_request_t *r,
> +    ngx_http_variable_value_t *v, uintptr_t data);
> #endif
> 
> static void ngx_http_upstream_init_request(ngx_http_request_t *r);
> @@ -373,6 +375,10 @@ static ngx_http_variable_t  ngx_http_ups
>       ngx_http_upstream_cache_etag, 0,
>       NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
> 
> +    { ngx_string("upstream_cache_age"), NULL,
> +      ngx_http_upstream_cache_age, 0,
> +      NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
> +
> #endif
> 
>     { ngx_null_string, NULL, NULL, 0, 0, 0 }
> @@ -4851,6 +4857,32 @@ ngx_http_upstream_cache_etag(ngx_http_re
>     return NGX_OK;
> }
> 
> +
> +static ngx_int_t
> +ngx_http_upstream_cache_age(ngx_http_request_t *r,
> +    ngx_http_variable_value_t *v, uintptr_t data)
> +{
> +    if (r->upstream == NULL
> +        || r->upstream->cache_status <= NGX_HTTP_CACHE_EXPIRED
> +        || r->upstream->cache_status == NGX_HTTP_CACHE_REVALIDATED)
> +    {
> +        v->not_found = 1;
> +        return NGX_OK;
> +    }
> +

Probably it would be more clear to list the valid 3 statuses here.
One extra operation though.

    if (r->upstream == NULL) {
        v->not_found = 1;
        return NGX_OK;
    }

    if (r->upstream->cache_status != NGX_HTTP_CACHE_STALE
        && r->upstream->cache_status != NGX_HTTP_CACHE_UPDATING
        && r->upstream->cache_status != NGX_HTTP_CACHE_HIT)
    {
        v->not_found = 1;
        return NGX_OK;
    }

> +    v->data = ngx_pnalloc(r->pool, NGX_TIME_T_LEN);
> +    if (v->data == NULL) {
> +        return NGX_ERROR;
> +    }
> +
> +    v->len = ngx_sprintf(v->data, "%T", ngx_time() - r->cache->date) - v->data;
> +    v->valid = 1;
> +    v->no_cacheable = 0;
> +    v->not_found = 0;
> +
> +    return NGX_OK;
> +}
> +
> #endif
> 
> 
> 
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel
> 



More information about the nginx-devel mailing list