From: Talkabout Date: Sun, 29 Mar 2020 13:22:10 +0000 (+0200) Subject: added logic for redis to honor ttl when serve_expired is not enabled X-Git-Tag: 1.11.0rc1~68^2~1^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ec02f72294aa4d33307c658c321d4e9066e4699;p=thirdparty%2Funbound.git added logic for redis to honor ttl when serve_expired is not enabled --- diff --git a/cachedb/cachedb.c b/cachedb/cachedb.c index c5be51622..8d2220a86 100644 --- a/cachedb/cachedb.c +++ b/cachedb/cachedb.c @@ -160,7 +160,7 @@ testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env, static void testframe_store(struct module_env* env, struct cachedb_env* cachedb_env, - char* key, uint8_t* data, size_t data_len) + char* key, uint8_t* data, size_t data_len, uint64_t ttl) { struct testframe_moddata* d = (struct testframe_moddata*) cachedb_env->backend_data; @@ -606,7 +606,8 @@ cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie) /* call backend */ (*ie->backend->store)(qstate->env, ie, key, sldns_buffer_begin(qstate->env->scratch_buffer), - sldns_buffer_limit(qstate->env->scratch_buffer)); + sldns_buffer_limit(qstate->env->scratch_buffer), + (uint64_t)qstate->return_msg->rep->ttl); } /** diff --git a/cachedb/cachedb.h b/cachedb/cachedb.h index 27187dc56..1bed80668 100644 --- a/cachedb/cachedb.h +++ b/cachedb/cachedb.h @@ -84,7 +84,7 @@ struct cachedb_backend { /** Store (env, cachedb_env, key, data, data_len) */ void (*store)(struct module_env*, struct cachedb_env*, char*, - uint8_t*, size_t); + uint8_t*, size_t, uint64_t); }; #define CACHEDB_HASHSIZE 256 /* bit hash */ diff --git a/cachedb/redis.c b/cachedb/redis.c index 3dfbf8f7a..fdc6716eb 100644 --- a/cachedb/redis.c +++ b/cachedb/redis.c @@ -249,16 +249,32 @@ redis_lookup(struct module_env* env, struct cachedb_env* cachedb_env, static void redis_store(struct module_env* env, struct cachedb_env* cachedb_env, - char* key, uint8_t* data, size_t data_len) + char* key, uint8_t* data, size_t data_len, uint64_t ttl) { redisReply* rep; - char cmdbuf[4+(CACHEDB_HASHSIZE/8)*2+3+1]; /* "SET " + key + " %b" */ int n; + int size; - verbose(VERB_ALGO, "redis_store %s (%d bytes)", key, (int)data_len); + if (env->cfg->serve_expired) { + size = 4+(CACHEDB_HASHSIZE/8)*2+3+1; + } + else { + size = 4+(CACHEDB_HASHSIZE/8)*2+3+4+sizeof(uint64_t)+1; + } + + char cmdbuf[size]; /* "SET " + key + " %b EX " + ttl */ + + if (env->cfg->serve_expired) { + verbose(VERB_ALGO, "redis_store %s (%d bytes)", key, (int)data_len); + /* build command to set to a binary safe string */ + n = snprintf(cmdbuf, sizeof(cmdbuf), "SET %s %%b EX %d", key, ttl); + } + else { + verbose(VERB_ALGO, "redis_store %s (%d bytes) with ttl %d", key, (int)data_len, ttl); + /* build command to set to a binary safe string */ + n = snprintf(cmdbuf, sizeof(cmdbuf), "SET %s %%b EX %d", key, ttl); + } - /* build command to set to a binary safe string */ - n = snprintf(cmdbuf, sizeof(cmdbuf), "SET %s %%b", key); if(n < 0 || n >= (int)sizeof(cmdbuf)) { log_err("redis_store: unexpected failure to build command"); return;