Permalink
Browse files

merging matti's changes for 'Get and Delete' with some editing

  • Loading branch information...
1 parent 6e436af commit 163b7380880c66df7c65f0bf0015ed9ee13365e7 @tnm tnm committed May 3, 2010
Showing with 15 additions and 29 deletions.
  1. +15 −29 recipes/2_basic_commands/get_and_delete/recipe.md
@@ -4,7 +4,9 @@ You want to atomically GET and then DELETE an object from Redis.
### Solution
-in pseudocode:
+Make use of Redis' built-in atomic functions, and MULTI-EXEC functionalty.
+
+One approach to the problem might go something like this. In pseudo-code:
success = RENAME key key:tmp
if success
@@ -13,49 +15,27 @@ in pseudocode:
return value
end
-using `redis-cli`:
-
- >> SET TOTO 1
- OK
- >> GET TOTO
- 1
- >> RENAME TOTO TOTO:TMP
- OK
- >> GET TOTO:TMP
- 1
- >> DEL TOTO:TMP
- (integer) 1
-
- >> GET TOTO
- (nil)
- >> RENAME TOTO TOTO:TMP
- (error) ERR no such key
-
-### Discussion
-
This is a simple one, but makes good use of Redis' atomic features. The
RENAME function will succeed for first caller, subsequent callers will fail
because the key was already renamed. GET and DEL benefit from the RENAME
function in order to keep other clients from reading the object data between
operations.
-This is potentially dangerous.
-
-### Problems with the solution proposed above
+However, there is an potential problem lurking here. If the execution is
+interrupted on line 2 (if success) or line 3 (value = GET key:tmp), then
+that key stays renamed as `key:tmp` for good in the database.
-If the execution is interrupted in line 2 (if success) or 3 (value = GET key:tmp) then that key stays renamed as key:tmp for good in the database.
+Using Redis' MULTI-EXEC function provides a solution.
-### Real atomic solution
-
-in pseudocode:
+First, in pseudocode:
MULTI
value = GET key
DELETE key
EXEC
return value
-using `redis-cli`:
+And using `redis-cli`:
redis> SET TOTO 1
OK
@@ -72,6 +52,12 @@ using `redis-cli`:
2. (integer) 1
redis> GET TOTO
(nil)
+
+
+### Discussion
+
### See Also
+**Atomically Pipeline Multiple Commands** for more information about MULTI/EXEC.
+

0 comments on commit 163b738

Please sign in to comment.