amazing cache #55
| @@ -14,7 +14,7 @@ from .utils.landmarks_manager import LandmarkManager | |||||||
| from .utils.toilets_manager import ToiletsManager | from .utils.toilets_manager import ToiletsManager | ||||||
| from .optimization.optimizer import Optimizer | from .optimization.optimizer import Optimizer | ||||||
| from .optimization.refiner import Refiner | from .optimization.refiner import Refiner | ||||||
| from .overpass.overpass import fill_cache | from .overpass.caching_strategy import fill_cache | ||||||
| from .cache import client as cache_client | from .cache import client as cache_client | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ import json | |||||||
| import hashlib | import hashlib | ||||||
|  |  | ||||||
| from ..constants import OSM_CACHE_DIR, OSM_TYPES | from ..constants import OSM_CACHE_DIR, OSM_TYPES | ||||||
|  | from .overpass import Overpass | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_cache_key(query: str) -> str: | def get_cache_key(query: str) -> str: | ||||||
| @@ -133,3 +134,27 @@ class CachingStrategy: | |||||||
|                     selector: str, conditions: list=None, out='center'): |                     selector: str, conditions: list=None, out='center'): | ||||||
|         """Create a hollow cache entry.""" |         """Create a hollow cache entry.""" | ||||||
|         cls.__strategy.set_hollow(key, cell, osm_types, selector, conditions, out) |         cls.__strategy.set_hollow(key, cell, osm_types, selector, conditions, out) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def fill_cache(): | ||||||
|  |     """ | ||||||
|  |     Scans the specified cache directory for files starting with 'hollow_' and attempts to load | ||||||
|  |     their contents as JSON to fill the cache of the Overpass system. | ||||||
|  |     """ | ||||||
|  |     overpass = Overpass() | ||||||
|  |  | ||||||
|  |     with os.scandir(OSM_CACHE_DIR) as it: | ||||||
|  |         for entry in it: | ||||||
|  |             if entry.is_file() and entry.name.startswith('hollow_'): | ||||||
|  |  | ||||||
|  |                 try : | ||||||
|  |                     # Read the whole file content as a string | ||||||
|  |                     with open(entry.path, 'r') as f: | ||||||
|  |                         # load data and fill the cache with the query and key | ||||||
|  |                         json_data = json.load(f) | ||||||
|  |                         overpass.fill_cache(json_data) | ||||||
|  |                     # Now delete the file as the cache is filled | ||||||
|  |                     os.remove(entry.path) | ||||||
|  |  | ||||||
|  |                 except Exception as exc : | ||||||
|  |                     overpass.logger.error(f'An error occured while parsing file {f} as .json file') | ||||||
|   | |||||||
| @@ -321,24 +321,3 @@ def get_base_info(elem: dict, osm_type: OSM_TYPES, with_name=False) : | |||||||
|         return osm_id, coords, name |         return osm_id, coords, name | ||||||
|     else : |     else : | ||||||
|         return osm_id, coords |         return osm_id, coords | ||||||
|  |  | ||||||
|  |  | ||||||
| def fill_cache(): |  | ||||||
|  |  | ||||||
|     overpass = Overpass() |  | ||||||
|  |  | ||||||
|     with os.scandir(OSM_CACHE_DIR) as it: |  | ||||||
|         for entry in it: |  | ||||||
|             if entry.is_file() and entry.name.startswith('hollow_'): |  | ||||||
|  |  | ||||||
|                 # Read the whole file content as a string |  | ||||||
|                 with open(entry.path, 'r') as f: |  | ||||||
|                     try : |  | ||||||
|                         # load data and fill the cache with the query and key |  | ||||||
|                         json_data = json.load(f) |  | ||||||
|                         overpass.fill_cache(json_data) |  | ||||||
|                     except Exception as exc : |  | ||||||
|                         overpass.logger.error(f'An error occured while parsing file {f} as .json file') |  | ||||||
|  |  | ||||||
|                 # Now delete the file as the cache is filled |  | ||||||
|                 os.remove(entry.path) |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user