Changeset View
Changeset View
Standalone View
Standalone View
swh/fuse/cache.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from abc import ABC | from abc import ABC | ||||
import json | import json | ||||
from pathlib import Path | |||||
from typing import Any, AsyncGenerator, Dict, Optional | from typing import Any, AsyncGenerator, Dict, Optional | ||||
import aiosqlite | import aiosqlite | ||||
from swh.model.identifiers import SWHID, parse_swhid | from swh.model.identifiers import SWHID, parse_swhid | ||||
from swh.web.client.client import typify_json | from swh.web.client.client import typify_json | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | class AbstractCache(ABC): | ||||
def __init__(self, conf: Dict[str, Any]): | def __init__(self, conf: Dict[str, Any]): | ||||
self.conf = conf | self.conf = conf | ||||
async def __aenter__(self): | async def __aenter__(self): | ||||
# In-memory (thus temporary) caching is useful for testing purposes | # In-memory (thus temporary) caching is useful for testing purposes | ||||
if self.conf.get("in-memory", False): | if self.conf.get("in-memory", False): | ||||
path = ":memory:" | path = ":memory:" | ||||
else: | else: | ||||
path = self.conf["path"] | path = Path(self.conf["path"]) | ||||
path.parent.mkdir(parents=True, exist_ok=True) | |||||
self.conn = await aiosqlite.connect(path) | self.conn = await aiosqlite.connect(path) | ||||
return self | return self | ||||
async def __aexit__(self, type=None, val=None, tb=None) -> None: | async def __aexit__(self, type=None, val=None, tb=None) -> None: | ||||
await self.conn.close() | await self.conn.close() | ||||
class MetadataCache(AbstractCache): | class MetadataCache(AbstractCache): | ||||
▲ Show 20 Lines • Show All 59 Lines • Show Last 20 Lines |