Skip to content

Commit

Permalink
global store init
Browse files Browse the repository at this point in the history
  • Loading branch information
lrdossan committed Oct 9, 2023
1 parent 93d59bf commit bd91ef6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
16 changes: 14 additions & 2 deletions caimira/apps/calculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .report_generator import ReportGenerator, calculate_report_data
from .data_service import DataService
from .user import AuthenticatedUser, AnonymousUser
from caimira.cache.global_store import GlobalStore

# The calculator version is based on a combination of the model version and the
# semantic version of the calculator itself. The version uses the terms
Expand All @@ -41,7 +42,6 @@
__version__ = "4.12.1"

LOG = logging.getLogger(__name__)


class BaseRequestHandler(RequestHandler):

Expand Down Expand Up @@ -111,7 +111,10 @@ async def post(self) -> None:
data_service: DataService = self.settings["data_service"]
if self.settings["data_service"]:
try:
global_store = await GlobalStore(data_service).get_data('/data')
# print(global_store)
fetched_service_data = await data_service.fetch()

except Exception as err:
error_message = f"Something went wrong with the data service: {str(err)}"
LOG.error(error_message, exc_info=True)
Expand Down Expand Up @@ -366,6 +369,7 @@ def make_app(
calculator_prefix: str = '/calculator',
theme_dir: typing.Optional[Path] = None,
) -> Application:

static_dir = Path(__file__).absolute().parent.parent / 'static'
calculator_static_dir = Path(__file__).absolute().parent / 'static'

Expand Down Expand Up @@ -429,15 +433,23 @@ def make_app(
)
template_environment.globals['get_url']=get_root_url
template_environment.globals['get_calculator_url']=get_root_calculator_url

data_service_credentials = {
'data_service_client_email': os.environ.get('DATA_SERVICE_CLIENT_EMAIL', None),
'data_service_client_password': os.environ.get('DATA_SERVICE_CLIENT_PASSWORD', None),
}

# async def initiate_caching_mechanism(data_service):
# cache_manager = GlobalStore(data_service)
# # Initialize first time
# data = await cache_manager.fetch_data_from_api('/data')
# return data

data_service = None
data_service_enabled = os.environ.get('DATA_SERVICE_ENABLED', 'False').lower() == 'true'
if data_service_enabled:
data_service = DataService(data_service_credentials)
# await initiate_caching_mechanism(data_service)

if debug:
tornado.log.enable_pretty_logging()
Expand Down
Empty file added caimira/cache/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions caimira/cache/global_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from cachetools import TTLCache
import typing

from caimira.apps.calculator.data_service import DataService

class GlobalStore():
def __init__(self, data_service):
self.data_service = data_service
self.global_store = {}
self.cache = TTLCache(maxsize=100, ttl=300)

# Singleton usage
_instance = None

def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.__init_singleton(*args, **kwargs)
return cls._instance

def __init_singleton(self, data_service_instance: DataService):
if not hasattr(self, "_initialized"):
self._initialized = True
self.global_store = {}
self.cache = TTLCache(maxsize=100, ttl=300)

def get_data(self, api_endpoint):
# Check if data is in cache
cached_data = self.cache.get(api_endpoint)
if cached_data:
return cached_data

# If not in cache, follow global store procedure
global_data = self.global_store.get(api_endpoint)
if global_data:
# If found in global store, update cache and return
self.cache[api_endpoint] = global_data
return global_data

# If not found in global store, fetch from API
data = self.fetch_data_from_api(api_endpoint)

return data

async def fetch_data_from_api(self, api_endpoint):
# Check if data is already in the cache
cached_data = self.cache.get(api_endpoint)
if cached_data:
return cached_data

# If not in cache, fetch from API
data = await self.data_service.fetch()

# Store in global store
self.global_store[api_endpoint] = data

# Store in cache
self.cache[api_endpoint] = data

return data

0 comments on commit bd91ef6

Please sign in to comment.