实现redis 的 rollback

问题:项目使用redis 维护了 一个队列 一个栈,数据使用时如果出现异常需要进行回滚,所以实现一下,后续再完善

# -*- encoding: utf-8 -*-
import logging
import redis
from collections import deque


class RedisClientTranslate(object):
    MaxTranslate = 10
    TranslateNums = 0

    def __init__(self, conf):
        if self.__class__.TranslateNums == self.__class__.MaxTranslate:
            logging.error("最大上限")
            raise
        self.__client = redis.Redis(**conf)
        self.history = deque(maxlen=100)
        self.__class__.TranslateNums += 1

    def lpop(self, key):
        value = self.__client.lpop(key)

        def rollback():
            if not value:
                return
            value_str = value.decode()
            self.__client.lpush(key, value_str)
        self.history.append(rollback)
        return value

    def lpush(self, key, value):
        res = self.__client.lpush(key, value)

        def rollback():
            self.__client.lpop(key)
        self.history.append(rollback)
        return res

    def rpop(self, key):
        value = self.__client.rpop(key)

        def rollback():
            if not value:
                return
            self.__client.rpush(key, value)

        self.history.append(rollback)
        return value

    def rpush(self, key, value):
        res = self.__client.rpush(key, value)

        def rollback():
            self.__client.rpop(key)

        self.history.append(rollback)
        return res

    def set(self, key, value, ex):
        res = self.__client.set(key, value, ex=ex)

        def rollback():
            self.__client.delete(key)

        self.history.append(rollback)
        return res

    def setnx(self, key, value):
        res = self.__client.setnx(key, value)

        def rollback():
            self.__client.delete(key)

        self.history.append(rollback)
        return res

    def delete(self, key):
        type = self.__client.type(key).decode()
        if type == "list":
            length = self.__client.llen(key)
            values = self.__client.lrange(key, 0, length)

            def rollback():
                for value in values:
                    self.__client.lpush(key, value)
            res = self.__client.delete(key)
            self.history.append(rollback)
            return res

        if type == "string":
            value = self.__client.get(key)
            ttl = self.__client.ttl(key)

            def rollback():
                self.__client.set(key, value, ex=ttl)

            res = self.__client.delete(key)
            self.history.append(rollback)
            return res

    def rollback(self):
        while True:
            func = self.history.pop()
            if not func:
                break
            func()
        return

    def close(self):
        self.__class__.TranslateNums -= 1


t = RedisClientTranslate(conf={})
t.lpush("test", 1)
t.lpush("test", 2)
t.lpop("test")

t.rpush("test1", 1)
t.rpush("test1", 2)
t.rpop("test1")

t.setnx("test2", 1)

t.set("key", "value", ex=3600)

t.delete("test2")

t.delete("key")

t.rollback()

发布了5 篇原创文章 · 获赞 0 · 访问量 767
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览