Exemplo n.º 1
1
void cache_access(struct cache *cache, int access_type,
			 void *addr)
{
	if (!cache)
		return;
	cache->access_count[access_type]++;
	struct decoded_address d_addr;
	decode_address(&d_addr, addr, cache);
	struct set *set = &cache->sets[d_addr.index];

	bool hit = set_access(set, d_addr.tag, access_type);
	if (!hit)
		cache_miss(cache, access_type, set, &d_addr);
}
Exemplo n.º 2
0
/* Devuelve 1 si esta en la cache, completa ls con la info del sector y actualiza su timestamp en cache */
int cache_Search (cacheMem * ch, LogicalSector * ls, tSectorId sectorId) {
	cacheObject * p;

	p = cache_findSector (ch, sectorId);
	if (p == NULL) {
		cache_miss(ch);
		return 0;
	}
	sector_Copy(ls, &(p->sector));
	cache_updateObjectTime(p);
	cache_hit(ch);
	return 1;
}
static ngx_int_t
ngx_http_filter_cache_handler(ngx_http_request_t *r)
{
    ngx_http_filter_cache_ctx_t *ctx = NULL;
    ngx_http_filter_cache_conf_t *conf = NULL;
    ngx_http_variable_value_t      *vv = NULL;
    ngx_http_cache_t  *c = NULL;
    ngx_str_t                    *key = NULL;
    ngx_int_t          rc = NGX_ERROR;

    if(r != r->main) {
        /* we don't currently serve subrequests
         * if we ever do subrequests, we will need a way to associate a ctx with this request
         * maybe keep an r in ctx and compare to r here?
         */
        return cache_miss(r, NULL, 0, 0);
    }

    conf = ngx_http_get_module_loc_conf(r, ngx_http_filter_cache_module);

    /* turned off */
    if (NULL == conf->upstream.cache) {
        return cache_miss(r, NULL, 0, conf->handler);
    }

    ctx = r->filter_cache;

    if(ctx) {
        /*loop detected??*/
        ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                      "cache loop in " __FILE__);
        /* XXX: this causes a 598 to be returned.  Is that what we want???
         * should loop return yet another status code??
         * be configurable and default to 598??
         */
        return cache_miss(r, NULL, 0, conf->handler);
    }

    ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_filter_cache_ctx_t));

    if (ctx == NULL) {
        return NGX_ERROR;
    }
    ctx->cache = NULL;
    ctx->cacheable = FILTER_DONOTCACHE;
    ctx->cache_status = NGX_HTTP_CACHE_MISS;

    /* needed so the ctx works in cache status*/
    r->filter_cache = ctx;

    switch (ngx_http_test_predicates(r, conf->upstream.cache_bypass)) {
    case NGX_ERROR:
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, __FILE__" ngx_http_test_predicates returned an error for bypass");
        return NGX_ERROR;
    case NGX_DECLINED:
        ctx->cache_status = NGX_HTTP_CACHE_BYPASS;
        return cache_miss(r, NULL, 0, conf->handler);
    default: /* NGX_OK */
        break;
    }

    if (!(r->method & conf->upstream.cache_methods)) {
        return cache_miss(r, NULL, 0, conf->handler);
    }

    rc = ngx_http_discard_request_body(r);

    if (rc != NGX_OK && rc != NGX_AGAIN) {
        return rc;
    }

    vv = ngx_http_get_indexed_variable(r, conf->index);

    if (vv == NULL || vv->not_found || vv->len == 0) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "the \"filter_cache_key\" variable is not set");
        return NGX_ERROR;
    }

    ctx->key.data = vv->data;
    ctx->key.len = vv->len;
    c = ctx->cache = NULL;

    if (ngx_http_filter_cache_new(r) != NGX_OK) {
        return NGX_ERROR;
    }

    key = ngx_array_push(&ctx->cache->keys);
    if (key == NULL) {
        return NGX_ERROR;
    }
    key->data = ctx->key.data;
    key->len = ctx->key.len;

    ctx->cache->file_cache = conf->upstream.cache->data;
    ngx_http_filter_cache_create_key(r);
    /* ngx_http_filter_cache_create(r); */

    c = ctx->cache;

    c->min_uses = conf->upstream.cache_min_uses;
    c->body_start = conf->upstream.buffer_size;
    c->file_cache = conf->upstream.cache->data;

    rc = ngx_http_filter_cache_open(r);

    switch(rc) {
    case NGX_HTTP_CACHE_UPDATING:
        if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_UPDATING) {
            if(ctx->cache && conf->grace && ( (ctx->cache->valid_sec - ngx_time() ) < conf->grace)) {
                ctx->cache_status = NGX_HTTP_CACHE_UPDATING;
                rc = NGX_OK;
            } else {
                rc = NGX_HTTP_CACHE_STALE;
            }
        } else {
            rc = NGX_HTTP_CACHE_STALE;
        }
        break;
    case NGX_OK:
        ctx->cache_status = NGX_HTTP_CACHE_HIT;
    }

    switch (rc) {
    case NGX_OK:
        return filter_cache_send(r);
        break;
    case NGX_HTTP_CACHE_STALE:
        ctx->cache_status = NGX_HTTP_CACHE_EXPIRED;
        /*return 599;*/
        break;
    case NGX_DECLINED:
        break;
    case NGX_HTTP_CACHE_SCARCE:
        return cache_miss(r, ctx, 0, conf->handler);
        break;
    case NGX_AGAIN:
        return NGX_BUSY;
    case NGX_ERROR:
        return NGX_ERROR;
    default:
        /*????*/
        break;
    }

    return cache_miss(r, ctx, 1, conf->handler);
}