Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis TTL #206

Merged
merged 6 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cachedb/cachedb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
gthess marked this conversation as resolved.
Show resolved Hide resolved
{
struct testframe_moddata* d = (struct testframe_moddata*)
cachedb_env->backend_data;
Expand Down Expand Up @@ -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);
gthess marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
2 changes: 1 addition & 1 deletion cachedb/cachedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
gthess marked this conversation as resolved.
Show resolved Hide resolved
};

#define CACHEDB_HASHSIZE 256 /* bit hash */
Expand Down
32 changes: 27 additions & 5 deletions cachedb/redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "util/alloc.h"
#include "util/config_file.h"
#include "sldns/sbuffer.h"
#include <stdbool.h>
gthess marked this conversation as resolved.
Show resolved Hide resolved

#ifdef USE_REDIS
#include "hiredis/hiredis.h"
Expand Down Expand Up @@ -249,16 +250,37 @@ 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)
gthess marked this conversation as resolved.
Show resolved Hide resolved
{
redisReply* rep;
char cmdbuf[4+(CACHEDB_HASHSIZE/8)*2+3+1]; /* "SET " + key + " %b" */
int n;
int size;
bool set_ttl = !env->cfg->serve_expired || env->cfg->serve_expired_ttl > 0;
Talkabout marked this conversation as resolved.
Show resolved Hide resolved

verbose(VERB_ALGO, "redis_store %s (%d bytes)", key, (int)data_len);
if (env->cfg->serve_expired_ttl > 0) {
ttl += env->cfg->serve_expired_ttl;
}

if (!set_ttl) {
size = 4+(CACHEDB_HASHSIZE/8)*2+3+1;
}
else {
size = 4+(CACHEDB_HASHSIZE/8)*2+3+4+sizeof(uint64_t)+1;
gthess marked this conversation as resolved.
Show resolved Hide resolved
}

char cmdbuf[size]; /* "SET " + key + " %b EX " + ttl */
gthess marked this conversation as resolved.
Show resolved Hide resolved

if (!set_ttl) {
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", key);
}
else {
Talkabout marked this conversation as resolved.
Show resolved Hide resolved
verbose(VERB_ALGO, "redis_store %s (%d bytes) with ttl %d", key, (int)data_len, ttl);
gthess marked this conversation as resolved.
Show resolved Hide resolved
/* build command to set to a binary safe string */
n = snprintf(cmdbuf, sizeof(cmdbuf), "SET %s %%b EX %d", key, ttl);
gthess marked this conversation as resolved.
Show resolved Hide resolved
}

/* 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;
Expand Down