linting
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 2m51s
Run linting on the backend code / Build (pull_request) Successful in 37s
Run testing on the backend code / Build (pull_request) Failing after 3m8s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 24s
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 2m51s
Run linting on the backend code / Build (pull_request) Successful in 37s
Run testing on the backend code / Build (pull_request) Failing after 3m8s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 24s
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
"""Module defining the caching strategy for overpass requests."""
|
||||
import os
|
||||
import xml.etree.ElementTree as ET
|
||||
import hashlib
|
||||
@@ -15,18 +16,41 @@ def get_cache_key(query: str) -> str:
|
||||
|
||||
|
||||
class CachingStrategyBase:
|
||||
"""
|
||||
Base class for implementing caching strategies.
|
||||
|
||||
This class defines the structure for a caching strategy with basic methods
|
||||
that must be implemented by subclasses. Subclasses should define how to
|
||||
retrieve, store, and close the cache.
|
||||
"""
|
||||
def get(self, key):
|
||||
"""Retrieve the cached data associated with the provided key."""
|
||||
raise NotImplementedError('Subclass should implement get')
|
||||
|
||||
def set(self, key, data):
|
||||
def set(self, key, value):
|
||||
"""Store data in the cache with the specified key."""
|
||||
raise NotImplementedError('Subclass should implement set')
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
"""Clean up or close any resources used by the caching strategy."""
|
||||
|
||||
|
||||
# For later use if xml does not suit well
|
||||
class JSONCache(CachingStrategyBase):
|
||||
"""
|
||||
A caching strategy that stores and retrieves data in JSON format.
|
||||
|
||||
This class provides methods to cache data as JSON files in a specified directory.
|
||||
The directory is automatically suffixed with '_JSON' to distinguish it from other
|
||||
caching strategies. The data is stored and retrieved using JSON serialization.
|
||||
|
||||
Args:
|
||||
cache_dir (str): The base directory where JSON cache files will be stored.
|
||||
Defaults to 'OSM_CACHE_DIR' with a '_JSON' suffix.
|
||||
|
||||
Methods:
|
||||
get(key): Retrieve cached data from a JSON file associated with the given key.
|
||||
set(key, value): Store data in a JSON file with the specified key.
|
||||
"""
|
||||
def __init__(self, cache_dir=OSM_CACHE_DIR):
|
||||
# Add the class name as a suffix to the directory
|
||||
self._cache_dir = f'{cache_dir}_JSON'
|
||||
@@ -39,16 +63,31 @@ class JSONCache(CachingStrategyBase):
|
||||
def get(self, key):
|
||||
filename = self._filename(key)
|
||||
if os.path.exists(filename):
|
||||
with open(filename, 'r') as file:
|
||||
with open(filename, 'r', encoding='utf-8') as file:
|
||||
return ujson.load(file)
|
||||
return None
|
||||
|
||||
def set(self, key, value):
|
||||
with open(self._filename(key), 'w') as file:
|
||||
with open(self._filename(key), 'w', encoding='utf-8') as file:
|
||||
ujson.dump(value, file)
|
||||
|
||||
|
||||
class XMLCache(CachingStrategyBase):
|
||||
"""
|
||||
A caching strategy that stores and retrieves data in XML format.
|
||||
|
||||
This class provides methods to cache data as XML files in a specified directory.
|
||||
The directory is automatically suffixed with '_XML' to distinguish it from other
|
||||
caching strategies. The data is stored and retrieved using XML serialization.
|
||||
|
||||
Args:
|
||||
cache_dir (str): The base directory where XML cache files will be stored.
|
||||
Defaults to 'OSM_CACHE_DIR' with a '_XML' suffix.
|
||||
|
||||
Methods:
|
||||
get(key): Retrieve cached data from a XML file associated with the given key.
|
||||
set(key, value): Store data in a XML file with the specified key.
|
||||
"""
|
||||
def __init__(self, cache_dir=OSM_CACHE_DIR):
|
||||
# Add the class name as a suffix to the directory
|
||||
self._cache_dir = f'{cache_dir}_XML'
|
||||
@@ -84,13 +123,22 @@ class XMLCache(CachingStrategyBase):
|
||||
|
||||
|
||||
class CachingStrategy:
|
||||
__strategy = XMLCache() # Default caching strategy
|
||||
"""
|
||||
A class to manage different caching strategies.
|
||||
|
||||
# Dictionary to map string identifiers to caching strategy classes
|
||||
This class provides an interface to switch between different caching strategies
|
||||
(e.g., XMLCache, JSONCache) dynamically. It allows caching data in different formats,
|
||||
depending on the strategy being used. By default, it uses the XMLCache strategy.
|
||||
|
||||
Attributes:
|
||||
__strategy (CachingStrategyBase): The currently active caching strategy.
|
||||
__strategies (dict): A mapping between strategy names (as strings) and their corresponding
|
||||
classes, allowing dynamic selection of caching strategies.
|
||||
"""
|
||||
__strategy = XMLCache() # Default caching strategy
|
||||
__strategies = {
|
||||
'XML': XMLCache,
|
||||
'JSON': JSONCache,
|
||||
# Add more strategies here if needed
|
||||
}
|
||||
|
||||
@classmethod
|
||||
@@ -105,13 +153,13 @@ class CachingStrategy:
|
||||
# If a previous strategy exists, close it
|
||||
if cls.__strategy:
|
||||
cls.__strategy.close()
|
||||
|
||||
|
||||
# Retrieve the strategy class based on the strategy name
|
||||
strategy_class = cls.__strategies.get(strategy_name)
|
||||
|
||||
|
||||
if not strategy_class:
|
||||
raise ValueError(f"Unknown caching strategy: {strategy_name}")
|
||||
|
||||
|
||||
# Instantiate the new strategy with the provided arguments
|
||||
cls.__strategy = strategy_class(**kwargs)
|
||||
return cls.__strategy
|
||||
@@ -129,5 +177,3 @@ class CachingStrategy:
|
||||
if not cls.__strategy:
|
||||
raise RuntimeError("Caching strategy has not been set.")
|
||||
cls.__strategy.set(key, value)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user