Created
June 14, 2013 15:54
-
-
Save seckcoder/5782977 to your computer and use it in GitHub Desktop.
A demo for redis transation's strong exception guarantee.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A demo for redis transation's strong exception guarantee. | |
""" | |
import logging | |
import redis | |
client = redis.StrictRedis(host="127.0.0.1", | |
port=6379, | |
db=1) | |
client.set("hello", 1) | |
def foo(): | |
with client.pipeline() as pipe: | |
pipe.set("hello", 2) | |
raise Exception("An exception throwed") | |
def bar(): | |
def transaction_func(pipe): | |
hello_value = int(pipe.get("hello")) | |
pipe.multi() | |
pipe.set("hello", hello_value+1) | |
raise Exception("Another exception throwed") | |
client.transaction(transaction_func, "hello") | |
try: | |
foo() | |
except Exception as e: | |
logging.exception(e) | |
print client.get("hello") # Note the value will not be changed since the operation failed. Redis is still in valid state | |
try: | |
bar() | |
except Exception as e: | |
logging.exception(e) | |
print client.get("hello") # this is the same |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment