Skip to content

Commit

Permalink
Use decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
sidjha1 committed Nov 12, 2024
1 parent f9ab752 commit dc3ef22
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lotus/cache.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
from collections import OrderedDict
from typing import Any
from functools import wraps
from typing import Any, Callable

import lotus


def require_cache_enabled(func: Callable) -> Callable:
"""Decorator to check if caching is enabled before calling the function."""

@wraps(func)
def wrapper(self, *args, **kwargs):
if not lotus.settings.enable_cache:
return None
return func(self, *args, **kwargs)

return wrapper


class Cache:
def __init__(self, max_size: int):
self.max_size = max_size
self.cache: OrderedDict[str, Any] = OrderedDict()

@require_cache_enabled
def get(self, key: str) -> Any | None:
if not lotus.settings.enable_cache:
return None

if key in self.cache:
lotus.logger.debug(f"Cache hit for {key}")

return self.cache.get(key)

@require_cache_enabled
def insert(self, key: str, value: Any):
if not lotus.settings.enable_cache:
return

self.cache[key] = value

# LRU eviction
Expand Down

0 comments on commit dc3ef22

Please sign in to comment.