In order to implement some local caching before memcached, and to properly handle expirations, it would be nice if the remaining TTL could be sent to the client. So, the client knows "this value is fresh and can be used for another 3601 seconds". This patch is a sample implementation, it just adds the remaining ttl to the end of the get's response. What do you think? Is this feature worthwhile?

Thanks!

PS: The protocol is not very expansible, adding a value there can be risky. I've tested this with PHP's memcache client library and it seems to work Ok.

diff -ubBr memcached-1.2.2/items.c memcached-1.2.2.new/items.c
--- memcached-1.2.2/items.c	2007-05-02 19:58:51.000000000 -0300
+++ memcached-1.2.2.new/items.c	2007-12-04 00:27:30.000000000 -0300
@@ -67,7 +67,7 @@
 static size_t item_make_header(const uint8_t nkey, const int flags, const int nbytes,
                      char *suffix, uint8_t *nsuffix) {
     /* suffix is defined at 40 chars elsewhere.. */
-    *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2);
+    *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d", flags, nbytes - 2);
     return sizeof(item) + nkey + *nsuffix + nbytes;
 }
 
diff -ubBr memcached-1.2.2/memcached.c memcached-1.2.2.new/memcached.c
--- memcached-1.2.2/memcached.c	2007-05-02 19:58:51.000000000 -0300
+++ memcached-1.2.2.new/memcached.c	2007-12-04 00:29:15.000000000 -0300
@@ -1053,6 +1053,7 @@
     int i = 0;
     item *it;
     token_t *key_token = &tokens[KEY_TOKEN];
+    char ttlstr[20];
 
     assert(c != NULL);
 
@@ -1096,6 +1097,8 @@
                     } else break;
                 }
 
+                sprintf(ttlstr, " %u\r\n", it->exptime - current_time);
+
                 /*
                  * Construct the response. Each hit adds three elements to the
                  * outgoing data list:
@@ -1105,7 +1108,9 @@
                  */
                 if (add_iov(c, "VALUE ", 6) != 0 ||
                     add_iov(c, ITEM_key(it), it->nkey) != 0 ||
-                    add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0)
+                    add_iov(c, ITEM_suffix(it), it->nsuffix) != 0 ||
+                    add_iov(c, ttlstr, strlen(ttlstr)) != 0 ||
+                    add_iov(c, ITEM_suffix(it) + it->nsuffix, it->nbytes) != 0)
                     {
                         break;
                     }

Reply via email to