If somebody wants to use memcached as a 2nd level cache, and put a local memory cache before, there's a problem with expirations. There's currently no way to know for how long a given value can be locally cached. So, 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. PPS: I'm resending this, somehow the first time it didn't get through, I hope this time it will =)
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-05 00:39:02.000000000 -0300
@@ -1053,6 +1053,8 @@
     int i = 0;
     item *it;
     token_t *key_token = &tokens[KEY_TOKEN];
+    char ttlstrbuf[20];
+    const char *ttlstr;
 
     assert(c != NULL);
 
@@ -1096,6 +1098,13 @@
                     } else break;
                 }
 
+                if(it->exptime == 0) {
+                  ttlstr = "\r\n";
+                } else {
+                  sprintf(ttlstrbuf, " %u\r\n", it->exptime - current_time);
+                  ttlstr = ttlstrbuf;
+                }
+
                 /*
                  * Construct the response. Each hit adds three elements to the
                  * outgoing data list:
@@ -1105,7 +1114,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