import redis
from django.conf import settings

redis_client = redis.Redis(
    host=settings.REDIS_HOST,
    port=settings.REDIS_PORT,
    password=settings.REDIS_PASSWORD,
    db=2,
    decode_responses=True
)

def acquire_lock(lock_key, timeout=10):
    """获取锁，返回是否成功"""
    return redis_client.set(lock_key, '1', nx=True, ex=timeout)

def release_lock(lock_key):
    """释放锁"""
    redis_client.delete(lock_key)