python redis 分布式锁

实现

class DisLockService:
    DEL_SCRIPT = '''
    if redis.call('get', KEYS[1]) == ARGV[1] then
    return redis.call('del', KEYS[1])
    else
        return 0
    end
    '''

    @staticmethod
    def tryLock(redisClient, lockKey, lockVal="1", px=5000):
        return redisClient.set(lockKey, lockVal, px=px, nx=True)

    @staticmethod
    def unLock(redisClient, lockKey, lockVal="1"):
        return redisClient.eval(DisLockService.DEL_SCRIPT, 1, lockKey, lockVal)

用法

nowTs = int(time.time())
lockVal = str(nowTs)
lockKey = 'test'
if DisLockService.tryLock(redisClient, lockKey, lockVal):
    try:
        # 添加自己的业务	
        pass
    finally:
        DisLockService.unLock(redisClient, finishPkLockKey, nowTsStr)

改进

class DisLockService(object):
    DEL_SCRIPT = '''
        if redis.call('get', KEYS[1]) == ARGV[1] then
        return redis.call('del', KEYS[1])
        else
            return 0
        end
        '''

    def __init__(self, redisClient, lockKey, lockVal, ttl=5000):
        '''

        :param redisClient:
        :param lockKey:
        :param lockVal:
        :param ttl:  单位 毫秒
        :param args:
        '''
        self.redisClient = redisClient
        self.lockKey = lockKey
        self.lockVal = lockVal
        self.ttl = ttl

    def __enter__(self):
        ret = redisClient.set(self.lockKey, self.lockVal, px=self.ttl, nx=True)
        if not ret:
            raise Exception("accquire lock failed")
        return self

    def __exit__(self, etype, evalue, tb):
        redisClient.eval(DisLockService.DEL_SCRIPT, 1, self.lockKey, self.lockVal)

用法

with DisLockService(redisClient, 'test', '1'):
    print 'xx'

with DisLockService(redisClient, 'test', '1'):
    print 'xx'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值