Lines Matching +full:set +full:- +full:env

2  * cachedb/redis.c - cachedb redis module
55 redisContext** ctxs; /* thread-specific redis contexts */
73 if((*moddata)->ctxs) { in moddata_clean()
75 for(i = 0; i < (*moddata)->numctxs; i++) { in moddata_clean()
76 if((*moddata)->ctxs[i]) in moddata_clean()
77 redisFree((*moddata)->ctxs[i]); in moddata_clean()
79 free((*moddata)->ctxs); in moddata_clean()
90 if(moddata->server_path && moddata->server_path[0]!=0) { in redis_connect()
91 ctx = redisConnectUnixWithTimeout(moddata->server_path, in redis_connect()
92 moddata->connect_timeout); in redis_connect()
94 ctx = redisConnectWithTimeout(moddata->server_host, in redis_connect()
95 moddata->server_port, moddata->connect_timeout); in redis_connect()
97 if(!ctx || ctx->err) { in redis_connect()
100 errstr = ctx->errstr; in redis_connect()
104 if(redisSetTimeout(ctx, moddata->command_timeout) != REDIS_OK) { in redis_connect()
105 log_err("failed to set redis timeout"); in redis_connect()
108 if(moddata->server_password && moddata->server_password[0]!=0) { in redis_connect()
110 rep = redisCommand(ctx, "AUTH %s", moddata->server_password); in redis_connect()
111 if(!rep || rep->type == REDIS_REPLY_ERROR) { in redis_connect()
118 if(moddata->logical_db > 0) { in redis_connect()
120 rep = redisCommand(ctx, "SELECT %d", moddata->logical_db); in redis_connect()
121 if(!rep || rep->type == REDIS_REPLY_ERROR) { in redis_connect()
122 log_err("failed to set logical database (%d)", in redis_connect()
123 moddata->logical_db); in redis_connect()
139 redis_init(struct module_env* env, struct cachedb_env* cachedb_env) in redis_init() argument
151 moddata->numctxs = env->cfg->num_threads; in redis_init()
152 moddata->ctxs = calloc(env->cfg->num_threads, sizeof(redisContext*)); in redis_init()
153 if(!moddata->ctxs) { in redis_init()
159 moddata->server_host = env->cfg->redis_server_host; in redis_init()
160 moddata->server_port = env->cfg->redis_server_port; in redis_init()
161 moddata->server_path = env->cfg->redis_server_path; in redis_init()
162 moddata->server_password = env->cfg->redis_server_password; in redis_init()
163 moddata->command_timeout.tv_sec = env->cfg->redis_timeout / 1000; in redis_init()
164 moddata->command_timeout.tv_usec = in redis_init()
165 (env->cfg->redis_timeout % 1000) * 1000; in redis_init()
166 moddata->connect_timeout.tv_sec = env->cfg->redis_timeout / 1000; in redis_init()
167 moddata->connect_timeout.tv_usec = in redis_init()
168 (env->cfg->redis_timeout % 1000) * 1000; in redis_init()
169 if(env->cfg->redis_command_timeout != 0) { in redis_init()
170 moddata->command_timeout.tv_sec = in redis_init()
171 env->cfg->redis_command_timeout / 1000; in redis_init()
172 moddata->command_timeout.tv_usec = in redis_init()
173 (env->cfg->redis_command_timeout % 1000) * 1000; in redis_init()
175 if(env->cfg->redis_connect_timeout != 0) { in redis_init()
176 moddata->connect_timeout.tv_sec = in redis_init()
177 env->cfg->redis_connect_timeout / 1000; in redis_init()
178 moddata->connect_timeout.tv_usec = in redis_init()
179 (env->cfg->redis_connect_timeout % 1000) * 1000; in redis_init()
181 moddata->logical_db = env->cfg->redis_logical_db; in redis_init()
182 for(i = 0; i < moddata->numctxs; i++) { in redis_init()
188 moddata->ctxs[i] = ctx; in redis_init()
190 cachedb_env->backend_data = moddata; in redis_init()
191 if(env->cfg->redis_expire_records) { in redis_init()
195 rep = redis_command(env, cachedb_env, in redis_init()
200 "redis-expire-records option requires the SETEX command " in redis_init()
204 redis_reply_type = rep->type; in redis_init()
212 "redis-expire-records option requires the SETEX command " in redis_init()
225 redis_deinit(struct module_env* env, struct cachedb_env* cachedb_env) in redis_deinit() argument
228 cachedb_env->backend_data; in redis_deinit()
229 (void)env; in redis_deinit()
237 * both SET and GET. If 'data' is non-NULL the command is supposed to be
238 * SET and GET otherwise, but the implementation of this function is agnostic
242 * been established; if not it tries to set up a new one.
245 * if it's non-NULL, it has to free it with freeReplyObject().
248 redis_command(struct module_env* env, struct cachedb_env* cachedb_env, in redis_command() argument
254 cachedb_env->backend_data; in redis_command()
256 /* We assume env->alloc->thread_num is a unique ID for each thread in redis_command()
257 * in [0, num-of-threads). We could treat it as an error condition in redis_command()
261 log_assert(env->alloc->thread_num < d->numctxs); in redis_command()
262 ctx = d->ctxs[env->alloc->thread_num]; in redis_command()
265 * it on a failure, try to re-establish a new one. Failures will be in redis_command()
269 d->ctxs[env->alloc->thread_num] = ctx; in redis_command()
277 /* Once an error as a NULL-reply is returned the context cannot in redis_command()
278 * be reused and we'll need to set up a new connection. */ in redis_command()
280 "closing connection: %s", ctx->errstr); in redis_command()
282 d->ctxs[env->alloc->thread_num] = NULL; in redis_command()
287 * The caller may perform context-dependent checks and logging. */ in redis_command()
288 if(rep->type == REDIS_REPLY_ERROR) in redis_command()
290 data ? "set" : "get", rep->str); in redis_command()
296 redis_lookup(struct module_env* env, struct cachedb_env* cachedb_env, in redis_lookup() argument
312 rep = redis_command(env, cachedb_env, cmdbuf, NULL, 0); in redis_lookup()
315 switch(rep->type) { in redis_lookup()
321 (int)rep->len); in redis_lookup()
322 if((size_t)rep->len > sldns_buffer_capacity(result_buffer)) { in redis_lookup()
324 (size_t)rep->len); in redis_lookup()
328 sldns_buffer_write(result_buffer, rep->str, rep->len); in redis_lookup()
336 rep->type); in redis_lookup()
344 redis_store(struct module_env* env, struct cachedb_env* cachedb_env, in redis_store() argument
349 int set_ttl = (env->cfg->redis_expire_records && in redis_store()
350 (!env->cfg->serve_expired || env->cfg->serve_expired_ttl > 0)); in redis_store()
352 * - "SET " + key + " %b" in redis_store()
353 * - "SETEX " + key + " " + ttl + " %b" in redis_store()
359 /* build command to set to a binary safe string */ in redis_store()
360 n = snprintf(cmdbuf, sizeof(cmdbuf), "SET %s %%b", key); in redis_store()
363 ttl += env->cfg->serve_expired_ttl; in redis_store()
366 /* build command to set to a binary safe string */ in redis_store()
377 rep = redis_command(env, cachedb_env, cmdbuf, data, data_len); in redis_store()
379 verbose(VERB_ALGO, "redis_store set completed"); in redis_store()
380 if(rep->type != REDIS_REPLY_STATUS && in redis_store()
381 rep->type != REDIS_REPLY_ERROR) { in redis_store()
383 rep->type); in redis_store()