File size: 1,477 Bytes
1161dd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from contextlib import contextmanager
from typing import Generator
from diskcache import Cache, Lock
from server.app.utils.diskcache_client import diskcache_client
from server.constant.constants import (DISTRIBUTED_LOCK_ID,
                                       DISTRIBUTED_LOCK_EXPIRE_TIME)


class DiskcacheLock:
    def __init__(self,
                 cache: Cache,
                 lock_id: str,
                 expire_time: int = DISTRIBUTED_LOCK_EXPIRE_TIME) -> None:
        """
        Initialize the DiskcacheLock with a Diskcache client and an optional expiration time.

        Args:
            cache (Cache): The diskcache Cache object.
            lock_id (str): A unique string identifier for the lock.
            expire_time (int): The expiration time for the lock in seconds.
        """
        self.cache: Cache = cache
        self.lock_id: str = lock_id
        self.expire_time: int = expire_time

    @contextmanager
    def lock(self) -> Generator[None, None, None]:
        """
        Context manager for acquiring and automatically releasing a lock.

        Yields:
            Generator[None, None, None]: Yields nothing and holds the lock until the 'with' block is completed.
        """
        with Lock(self.cache, self.lock_id, expire=self.expire_time):
            yield  # Hold the lock until the 'with' block completes


# Initialize Diskcache distributed lock
diskcache_lock = DiskcacheLock(diskcache_client.cache, DISTRIBUTED_LOCK_ID)