Skip to content
5 changes: 5 additions & 0 deletions growthbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
BackoffStrategy
)

from .cache_interfaces import (
AbstractFeatureCache,
AbstractAsyncFeatureCache
)

# Plugin support
from .plugins import (
GrowthBookTrackingPlugin,
Expand Down
46 changes: 46 additions & 0 deletions growthbook/cache_interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from abc import abstractmethod, ABC
from typing import Optional, Dict

class AbstractFeatureCache(ABC):
@abstractmethod
def get(self, key: str) -> Optional[Dict]:
pass

@abstractmethod
def set(self, key: str, value: Dict, ttl: int) -> None:
pass

def clear(self) -> None:
pass

class AbstractAsyncFeatureCache(ABC):
"""Abstract base class for async feature caching implementations"""

@abstractmethod
async def get(self, key: str) -> Optional[Dict]:
"""
Retrieve cached features by key.

Args:
key: Cache key

Returns:
Cached dictionary or None if not found/expired
"""
pass

@abstractmethod
async def set(self, key: str, value: Dict, ttl: int) -> None:
"""
Store features in cache with TTL.

Args:
key: Cache key
value: Features dictionary to cache
ttl: Time to live in seconds
"""
pass

async def clear(self) -> None:
"""Clear all cached entries (optional to override)"""
pass
5 changes: 4 additions & 1 deletion growthbook/common_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Any, Callable, Dict, List, Optional, Union, Set, Tuple
from enum import Enum
from abc import ABC, abstractmethod
from .cache_interfaces import AbstractFeatureCache, AbstractAsyncFeatureCache

class VariationMeta(TypedDict):
key: str
Expand Down Expand Up @@ -396,7 +397,7 @@ def get_all_assignments(self, attributes: Dict[str, str]) -> Dict[str, Dict]:
return docs

@dataclass
class StackContext:
class StackContext:
id: Optional[str] = None
evaluated_features: Set[str] = field(default_factory=set)

Expand Down Expand Up @@ -431,6 +432,8 @@ class Options:
on_experiment_viewed: Optional[Callable[[Experiment, Result, Optional[UserContext]], None]] = None
on_feature_usage: Optional[Callable[[str, 'FeatureResult', UserContext], None]] = None
tracking_plugins: Optional[List[Any]] = None
cache: Optional[AbstractFeatureCache] = None
async_cache: Optional[AbstractAsyncFeatureCache] = None


@dataclass
Expand Down
Loading