-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'AlboCode-feature/cache' into develop
- Loading branch information
Showing
16 changed files
with
333 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
class BaseCache(ABC): | ||
|
||
@abstractmethod | ||
def insert(self, cache_item): | ||
pass | ||
|
||
@abstractmethod | ||
def get_item(self, key): | ||
pass | ||
|
||
@abstractmethod | ||
def get_value(self, key): | ||
pass | ||
|
||
@abstractmethod | ||
def delete(self, key): | ||
pass | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import time | ||
|
||
|
||
class CacheItem: | ||
|
||
def __init__(self, key, value, ttl): | ||
self.key = key | ||
self.value = value | ||
self.ttl = ttl | ||
self.created_at = time.time() | ||
|
||
def is_expired(self): | ||
if self.ttl == -1 or self.ttl is None: | ||
return False | ||
|
||
return (self.created_at + self.ttl) < time.time() | ||
|
||
def __repr__(self): | ||
return f'CacheItem(key={self.key}, value={self.value}, ttl={self.ttl}, created_at={self.created_at})' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from cat.env import get_env | ||
|
||
from cat.utils import singleton | ||
|
||
|
||
@singleton | ||
class CacheManager: | ||
"""Class to instantiate different cache types.""" | ||
|
||
def __init__(self): | ||
|
||
self.cache_type = get_env("CCAT_CACHE_TYPE") | ||
|
||
if self.cache_type == "in_memory": | ||
from cat.cache.in_memory_cache import InMemoryCache | ||
self.cache = InMemoryCache() | ||
elif self.cache_type == "file_system": | ||
cache_dir = get_env("CCAT_CACHE_DIR") | ||
from cat.cache.file_system_cache import FileSystemCache | ||
self.cache = FileSystemCache(cache_dir) | ||
else: | ||
raise ValueError(f"Cache type {self.cache_type} not supported") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import os | ||
import pickle | ||
from cat.cache.base_cache import BaseCache | ||
from cat.utils import singleton | ||
|
||
|
||
@singleton | ||
class FileSystemCache(BaseCache): | ||
"""Cache implementation using the file system. | ||
Attributes | ||
---------- | ||
cache_dir : str | ||
Directory to store the cache. | ||
""" | ||
|
||
def __init__(self, cache_dir): | ||
self.cache_dir = cache_dir | ||
if not os.path.exists(self.cache_dir): | ||
os.makedirs(self.cache_dir) | ||
|
||
def _get_file_path(self, key): | ||
return os.path.join(self.cache_dir, f"{key}.cache") | ||
|
||
def insert(self, cache_item): | ||
"""Insert a key-value pair in the cache. | ||
Parameters | ||
---------- | ||
cache_item : CacheItem | ||
Cache item to store. | ||
""" | ||
|
||
with open(self._get_file_path(cache_item.key), "wb") as f: | ||
pickle.dump(cache_item, f) | ||
|
||
def get_item(self, key): | ||
"""Get the value stored in the cache. | ||
Parameters | ||
---------- | ||
key : str | ||
Key to retrieve the value. | ||
Returns | ||
------- | ||
any | ||
Value stored in the cache. | ||
""" | ||
file_path = self._get_file_path(key) | ||
if not os.path.exists(file_path): | ||
return None | ||
|
||
with open(file_path, "rb") as f: | ||
cache_item = pickle.load(f) | ||
|
||
if cache_item.is_expired(): | ||
os.remove(file_path) | ||
return None | ||
|
||
return cache_item | ||
|
||
def get_value(self, key): | ||
"""Get the value stored in the cache. | ||
Parameters | ||
---------- | ||
key : str | ||
Key to retrieve the value. | ||
Returns | ||
------- | ||
any | ||
Value stored in the cache. | ||
""" | ||
|
||
cache_item = self.get_item(key) | ||
if cache_item: | ||
return cache_item.value | ||
return None | ||
|
||
def delete(self, key): | ||
"""Delete a key-value pair from the cache. | ||
Parameters | ||
---------- | ||
key : str | ||
Key to delete the value. | ||
""" | ||
file_path = self._get_file_path(key) | ||
if os.path.exists(file_path): | ||
os.remove(file_path) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from cat.cache.base_cache import BaseCache | ||
from cat.cache.cache_item import CacheItem | ||
|
||
from cat.utils import singleton | ||
|
||
|
||
@singleton | ||
class InMemoryCache(BaseCache): | ||
"""Cache implementation using a python dictionary. | ||
Attributes | ||
---------- | ||
cache : dict | ||
Dictionary to store the cache. | ||
""" | ||
|
||
def __init__(self): | ||
self.cache = {} | ||
|
||
def insert(self, cache_item): | ||
"""Insert a key-value pair in the cache. | ||
Parameters | ||
---------- | ||
cache_item : CacheItem | ||
Cache item to store. | ||
""" | ||
self.cache[cache_item.key] = cache_item | ||
|
||
def get_item(self, key) -> CacheItem: | ||
"""Get the value stored in the cache. | ||
Parameters | ||
---------- | ||
key : str | ||
Key to retrieve the value. | ||
Returns | ||
------- | ||
any | ||
Value stored in the cache. | ||
""" | ||
item = self.cache.get(key) | ||
|
||
if item and item.is_expired(): | ||
del self.cache[key] | ||
return None | ||
|
||
return item | ||
|
||
def get_value(self, key): | ||
"""Get the value stored in the cache. | ||
Parameters | ||
---------- | ||
key : str | ||
Key to retrieve the value. | ||
Returns | ||
------- | ||
any | ||
Value stored in the cache. | ||
""" | ||
|
||
|
||
item = self.get_item(key) | ||
if item: | ||
return item.value | ||
return None | ||
|
||
def delete(self, key): | ||
"""Delete a key-value pair from the cache. | ||
Parameters | ||
---------- | ||
key : str | ||
Key to delete the value. | ||
""" | ||
if key in self.cache: | ||
del self.cache[key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.