backend/new-overpass #52
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							@@ -2,7 +2,6 @@
 | 
				
			|||||||
import os
 | 
					import os
 | 
				
			||||||
import xml.etree.ElementTree as ET
 | 
					import xml.etree.ElementTree as ET
 | 
				
			||||||
import hashlib
 | 
					import hashlib
 | 
				
			||||||
import ujson
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ..constants import OSM_CACHE_DIR
 | 
					from ..constants import OSM_CACHE_DIR
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -35,43 +34,6 @@ class CachingStrategyBase:
 | 
				
			|||||||
        """Clean up or close any resources used by the caching strategy."""
 | 
					        """Clean up or close any resources used by the caching strategy."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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'
 | 
					 | 
				
			||||||
        if not os.path.exists(self._cache_dir):
 | 
					 | 
				
			||||||
            os.makedirs(self._cache_dir)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def _filename(self, key):
 | 
					 | 
				
			||||||
        return os.path.join(self._cache_dir, f'{key}.json')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def get(self, key):
 | 
					 | 
				
			||||||
        filename = self._filename(key)
 | 
					 | 
				
			||||||
        if os.path.exists(filename):
 | 
					 | 
				
			||||||
            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', encoding='utf-8') as file:
 | 
					 | 
				
			||||||
            ujson.dump(value, file)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class XMLCache(CachingStrategyBase):
 | 
					class XMLCache(CachingStrategyBase):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    A caching strategy that stores and retrieves data in XML format.
 | 
					    A caching strategy that stores and retrieves data in XML format.
 | 
				
			||||||
@@ -138,7 +100,6 @@ class CachingStrategy:
 | 
				
			|||||||
    __strategy = XMLCache()  # Default caching strategy
 | 
					    __strategy = XMLCache()  # Default caching strategy
 | 
				
			||||||
    __strategies = {
 | 
					    __strategies = {
 | 
				
			||||||
        'XML': XMLCache,
 | 
					        'XML': XMLCache,
 | 
				
			||||||
        'JSON': JSONCache,
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user