]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
added logic for redis to honor ttl when serve_expired is not enabled
authorTalkabout <talk.about@gmx.de>
Sun, 29 Mar 2020 13:22:10 +0000 (15:22 +0200)
committerTalkabout <talk.about@gmx.de>
Sun, 29 Mar 2020 13:22:10 +0000 (15:22 +0200)
cachedb/cachedb.c
cachedb/cachedb.h
cachedb/redis.c

index c5be516225f90c81daf6da05da1c1d895efa0c58..8d2220a864e47f7c811b35ba12792b69454bb8d5 100644 (file)
@@ -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);
 }
 
 /**
index 27187dc56dc69c746c36ed9c3b74d7f21676ba2e..1bed80668c0e4d8f173504149412a6e94256dbf8 100644 (file)
@@ -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 */
index 3dfbf8f7a25c6e630f945477005fecf0d6803648..fdc6716eb71d4e92095654d6e869e92924d75ad5 100644 (file)
@@ -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;