Exemplo n.º 1
0
    def expire(self, key, expire_secs):
        if key not in self._storage:
            return 0

        expires_at = (monotonic_ns_time() + expire_secs *
                      (10**9) if expire_secs else None)
        # TODO: already expired?
        self._storage[key].update({"expires_at": expires_at})
        return 1
Exemplo n.º 2
0
    def ttl(self, key: bytes):

        if key not in self._storage:
            return -2

        expires_at = self._storage[key]["expires_at"]

        if not expires_at:
            return -1

        ttl = ceil((expires_at - monotonic_ns_time()) / (10**9))
        return ttl if ttl >= 0 else -2
Exemplo n.º 3
0
    def __getitem__(self, key):
        if key not in self._storage:
            return None
        val = self._storage[key]

        value = val["value"]
        expires_at = val["expires_at"]
        if not expires_at or expires_at > monotonic_ns_time():
            return value

        # item expired
        del self._storage[key]
        return None
Exemplo n.º 4
0
 def __setitem__(self, key, val, expire_secs: int = None):
     # TODO: add type checking for the data
     expires_at = (monotonic_ns_time() + expire_secs *
                   (10**9) if expire_secs else None)
     self._storage.update({key: {"value": val, "expires_at": expires_at}})