From b7a8daef6070816e08438fd64b2b9a41571eb333 Mon Sep 17 00:00:00 2001
From: antirez <antirez@gmail.com>
Date: Mon, 5 Jul 2010 19:38:12 +0200
Subject: [PATCH] WATCH will now consider touched keys target of EXPIRE command
 after the WATCH is performed, but not before

---
 src/db.c           |  2 ++
 tests/unit/cas.tcl | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/src/db.c b/src/db.c
index e1e82cb2..d5e0d1e8 100644
--- a/src/db.c
+++ b/src/db.c
@@ -472,11 +472,13 @@ void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
     if (seconds <= 0) {
         if (dbDelete(c->db,key)) server.dirty++;
         addReply(c, shared.cone);
+        touchWatchedKey(c->db,key);
         return;
     } else {
         time_t when = time(NULL)+seconds;
         if (setExpire(c->db,key,when)) {
             addReply(c,shared.cone);
+            touchWatchedKey(c->db,key);
             server.dirty++;
         } else {
             addReply(c,shared.czero);
diff --git a/tests/unit/cas.tcl b/tests/unit/cas.tcl
index dc6a5ef7..d420d9e2 100644
--- a/tests/unit/cas.tcl
+++ b/tests/unit/cas.tcl
@@ -111,4 +111,25 @@ start_server {tags {"cas"}} {
         r ping
         r exec
     } {PONG}
+
+    test {WATCH will consider touched keys target of EXPIRE} {
+        r del x
+        r set x foo
+        r watch x
+        r expire x 10
+        r multi
+        r ping
+        r exec
+    } {}
+
+    test {WATCH will not consider touched expired keys} {
+        r del x
+        r set x foo
+        r expire x 2
+        r watch x
+        after 3000
+        r multi
+        r ping
+        r exec
+    } {PONG}
 }