MEDIUM: cache: max-age configuration keyword

Add a configuration keyword to change the max-age.
The default one is still 60s.
This commit is contained in:
William Lallemand 2017-11-24 18:53:43 +01:00 committed by Willy Tarreau
parent a71cd1d407
commit 49b4453b58

View File

@ -326,7 +326,7 @@ char *directive_value(const char *sample, int slen, const char *word, int wlen)
* - the default-max-age of the cache * - the default-max-age of the cache
* *
*/ */
int http_calc_maxage(struct stream *s) int http_calc_maxage(struct stream *s, struct cache *cache)
{ {
struct http_txn *txn = s->txn; struct http_txn *txn = s->txn;
struct hdr_ctx ctx; struct hdr_ctx ctx;
@ -335,8 +335,6 @@ int http_calc_maxage(struct stream *s)
int maxage = -1; int maxage = -1;
/* TODO: forced maxage configuration */
ctx.idx = 0; ctx.idx = 0;
/* loop on the Cache-Control values */ /* loop on the Cache-Control values */
@ -367,14 +365,12 @@ int http_calc_maxage(struct stream *s)
if (smaxage > 0) if (smaxage > 0)
return smaxage; return MIN(smaxage, cache->maxage);
if (maxage > 0) if (maxage > 0)
return maxage; return MIN(maxage, cache->maxage);
/* TODO: return default value */ return cache->maxage;
return 60;
} }
@ -402,7 +398,8 @@ enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
struct filter *filter; struct filter *filter;
struct hdr_ctx ctx; struct hdr_ctx ctx;
struct shared_block *first = NULL; struct shared_block *first = NULL;
struct shared_context *shctx = shctx_ptr((struct cache *)rule->arg.act.p[0]); struct cache *cache = (struct cache *)rule->arg.act.p[0];
struct shared_context *shctx = shctx_ptr(cache);
struct cache_entry *object; struct cache_entry *object;
@ -484,7 +481,7 @@ enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
/* Insert the node later on caching success */ /* Insert the node later on caching success */
shctx_lock(shctx); shctx_lock(shctx);
if (entry_exist((struct cache *)rule->arg.act.p[0], txn->cache_hash)) { if (entry_exist(cache, txn->cache_hash)) {
shctx_unlock(shctx); shctx_unlock(shctx);
if (filter->ctx) { if (filter->ctx) {
object->eb.key = 0; object->eb.key = 0;
@ -497,8 +494,7 @@ enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
/* store latest value and expiration time */ /* store latest value and expiration time */
object->latest_validation = now.tv_sec; object->latest_validation = now.tv_sec;
object->expire = now.tv_sec + http_calc_maxage(s); object->expire = now.tv_sec + http_calc_maxage(s, cache);
} }
return ACT_RET_CONT; return ACT_RET_CONT;
} }
@ -765,7 +761,7 @@ int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
file, linenum, tmp_cache_config->id); file, linenum, tmp_cache_config->id);
err_code |= ERR_WARN; err_code |= ERR_WARN;
} }
tmp_cache_config->maxage = 60;
tmp_cache_config->maxblocks = 0; tmp_cache_config->maxblocks = 0;
} }
} else if (strcmp(args[0], "total-max-size") == 0) { } else if (strcmp(args[0], "total-max-size") == 0) {
@ -780,6 +776,19 @@ int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE; maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
tmp_cache_config->maxblocks = maxsize; tmp_cache_config->maxblocks = maxsize;
} else if (strcmp(args[0], "max-age") == 0) {
if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
err_code |= ERR_ABORT;
goto out;
}
if (!*args[1]) {
ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
file, linenum, args[0]);
err_code |= ERR_WARN;
}
tmp_cache_config->maxage = atoi(args[1]);
} else if (*args[0] != 0) { } else if (*args[0] != 0) {
ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]); ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;