dovecot-2.2: dict-redis: Added support for expire_secs=n parameter.

dovecot at dovecot.org dovecot at dovecot.org
Tue Nov 25 01:10:57 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/5ee74bc88f18
changeset: 18107:5ee74bc88f18
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Nov 25 03:10:41 2014 +0200
description:
dict-redis: Added support for expire_secs=n parameter.
It performs EXPIRE command for the added keys immediately after the SET.

diffstat:

 src/lib-dict/dict-redis.c |  34 ++++++++++++++++++++++++++--------
 1 files changed, 26 insertions(+), 8 deletions(-)

diffs (79 lines):

diff -r 2553f3aae9bb -r 5ee74bc88f18 src/lib-dict/dict-redis.c
--- a/src/lib-dict/dict-redis.c	Tue Nov 25 02:56:37 2014 +0200
+++ b/src/lib-dict/dict-redis.c	Tue Nov 25 03:10:41 2014 +0200
@@ -43,7 +43,7 @@
 
 struct redis_dict {
 	struct dict dict;
-	char *username, *key_prefix;
+	char *username, *key_prefix, *expire_value;
 	unsigned int timeout_msecs;
 
 	struct ioloop *ioloop, *prev_ioloop;
@@ -316,7 +316,7 @@
 {
 	struct redis_dict *dict;
 	struct ip_addr ip;
-	unsigned int port = REDIS_DEFAULT_PORT;
+	unsigned int secs, port = REDIS_DEFAULT_PORT;
 	const char *const *args, *unix_path = NULL;
 	int ret = 0;
 
@@ -351,6 +351,14 @@
 		} else if (strncmp(*args, "prefix=", 7) == 0) {
 			i_free(dict->key_prefix);
 			dict->key_prefix = i_strdup(*args + 7);
+		} else if (strncmp(*args, "expire_secs=", 12) == 0) {
+			if (str_to_uint(*args + 12, &secs) < 0 || secs == 0) {
+				*error_r = t_strdup_printf(
+					"Invalid expire_secs: %s", *args+14);
+				ret = -1;
+			}
+			i_free(dict->expire_value);
+			dict->expire_value = i_strdup(*args + 12);
 		} else if (strncmp(*args, "timeout_msecs=", 14) == 0) {
 			if (str_to_uint(*args+14, &dict->timeout_msecs) < 0) {
 				*error_r = t_strdup_printf(
@@ -404,6 +412,7 @@
 	str_free(&dict->conn.last_reply);
 	array_free(&dict->replies);
 	array_free(&dict->input_states);
+	i_free(dict->expire_value);
 	i_free(dict->key_prefix);
 	i_free(dict->username);
 	i_free(dict);
@@ -630,19 +639,28 @@
 	struct redis_dict_transaction_context *ctx =
 		(struct redis_dict_transaction_context *)_ctx;
 	struct redis_dict *dict = (struct redis_dict *)_ctx->dict;
-	const char *cmd;
+	string_t *cmd;
 
 	if (redis_check_transaction(ctx) < 0)
 		return;
 
 	key = redis_dict_get_full_key(dict, key);
-	cmd = t_strdup_printf("*3\r\n$3\r\nSET\r\n$%u\r\n%s\r\n$%u\r\n%s\r\n",
-			      (unsigned int)strlen(key), key,
-			      (unsigned int)strlen(value), value);
-	if (o_stream_send_str(dict->conn.conn.output, cmd) < 0)
-		ctx->failed = TRUE;
+	cmd = t_str_new(128);
+	str_printfa(cmd, "*3\r\n$3\r\nSET\r\n$%u\r\n%s\r\n$%u\r\n%s\r\n",
+		    (unsigned int)strlen(key), key,
+		    (unsigned int)strlen(value), value);
 	redis_input_state_add(dict, REDIS_INPUT_STATE_MULTI);
 	ctx->cmd_count++;
+	if (dict->expire_value != NULL) {
+		str_printfa(cmd, "*3\r\n$6\r\nEXPIRE\r\n$%u\r\n%s\r\n$%u\r\n%s\r\n",
+			    (unsigned int)strlen(key), key,
+			    (unsigned int)strlen(dict->expire_value),
+			    dict->expire_value);
+		redis_input_state_add(dict, REDIS_INPUT_STATE_MULTI);
+		ctx->cmd_count++;
+	}
+	if (o_stream_send(dict->conn.conn.output, str_data(cmd), str_len(cmd)) < 0)
+		ctx->failed = TRUE;
 }
 
 static void redis_unset(struct dict_transaction_context *_ctx,


More information about the dovecot-cvs mailing list