-
Notifications
You must be signed in to change notification settings - Fork 56
LCORE-298: @connection decorator and no-op conversion cache #568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tisnik
merged 4 commits into
lightspeed-core:main
from
tisnik:lcore-298-noop-conversion-cache
Sep 22, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,106 @@ | ||
| """No-operation cache implementation.""" | ||
|
|
||
| from cache.cache import Cache | ||
| from models.cache_entry import CacheEntry | ||
| from log import get_logger | ||
| from utils.connection_decorator import connection | ||
|
|
||
| logger = get_logger("cache.noop_cache") | ||
|
|
||
|
|
||
| class NoopCache(Cache): | ||
| """No-operation cache implementation.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| """Create a new instance of no-op cache.""" | ||
|
|
||
| def connect(self) -> None: | ||
| """Initialize connection to database.""" | ||
| logger.info("Connecting to storage") | ||
|
|
||
| def connected(self) -> bool: | ||
| """Check if connection to cache is alive.""" | ||
| return True | ||
|
|
||
| def initialize_cache(self) -> None: | ||
| """Initialize cache.""" | ||
|
|
||
| @connection | ||
| def get( | ||
| self, user_id: str, conversation_id: str, skip_user_id_check: bool = False | ||
| ) -> list[CacheEntry]: | ||
| """Get the value associated with the given key. | ||
|
|
||
| Args: | ||
| user_id: User identification. | ||
| conversation_id: Conversation ID unique for given user. | ||
| skip_user_id_check: Skip user_id suid check. | ||
|
|
||
| Returns: | ||
| Empty list. | ||
| """ | ||
| # just check if user_id and conversation_id are UUIDs | ||
| super().construct_key(user_id, conversation_id, skip_user_id_check) | ||
| return [] | ||
|
|
||
| @connection | ||
| def insert_or_append( | ||
| self, | ||
| user_id: str, | ||
| conversation_id: str, | ||
| cache_entry: CacheEntry, | ||
| skip_user_id_check: bool = False, | ||
| ) -> None: | ||
| """Set the value associated with the given key. | ||
|
|
||
| Args: | ||
| user_id: User identification. | ||
| conversation_id: Conversation ID unique for given user. | ||
| cache_entry: The `CacheEntry` object to store. | ||
| skip_user_id_check: Skip user_id suid check. | ||
|
|
||
| """ | ||
| # just check if user_id and conversation_id are UUIDs | ||
| super().construct_key(user_id, conversation_id, skip_user_id_check) | ||
|
|
||
| @connection | ||
| def delete( | ||
| self, user_id: str, conversation_id: str, skip_user_id_check: bool = False | ||
| ) -> bool: | ||
| """Delete conversation history for a given user_id and conversation_id. | ||
|
|
||
| Args: | ||
| user_id: User identification. | ||
| conversation_id: Conversation ID unique for given user. | ||
| skip_user_id_check: Skip user_id suid check. | ||
|
|
||
| Returns: | ||
| bool: True in all cases. | ||
|
|
||
| """ | ||
| # just check if user_id and conversation_id are UUIDs | ||
| super().construct_key(user_id, conversation_id, skip_user_id_check) | ||
| return True | ||
|
|
||
| @connection | ||
| def list(self, user_id: str, skip_user_id_check: bool = False) -> list[str]: | ||
| """List all conversations for a given user_id. | ||
|
|
||
| Args: | ||
| user_id: User identification. | ||
| skip_user_id_check: Skip user_id suid check. | ||
|
|
||
| Returns: | ||
| An empty list. | ||
|
|
||
| """ | ||
| super()._check_user_id(user_id, skip_user_id_check) | ||
| return [] | ||
|
|
||
| def ready(self) -> bool: | ||
| """Check if the cache is ready. | ||
|
|
||
| Returns: | ||
| True in all cases. | ||
| """ | ||
| return True |
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| """Decocator that makes sure the object is 'connected' according to it's connected predicate.""" | ||
|
|
||
| from typing import Any, Callable | ||
|
|
||
|
|
||
| def connection(f: Callable) -> Callable: | ||
| """Decocator that makes sure the object is 'connected' according to it's connected predicate. | ||
|
|
||
| Example: | ||
| ```python | ||
| @connection | ||
| def list_history(self) -> list[str]: | ||
| pass | ||
| ``` | ||
| """ | ||
|
|
||
| def wrapper(connectable: Any, *args: Any, **kwargs: Any) -> Callable: | ||
| if not connectable.connected(): | ||
| connectable.connect() | ||
| return f(connectable, *args, **kwargs) | ||
|
|
||
| return wrapper | ||
This file contains hidden or 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 @@ | ||
| """Test cases for conversation history cache implementations.""" |
This file contains hidden or 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,36 @@ | ||
| """Unit tests for CacheFactory class.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from constants import CACHE_TYPE_NOOP | ||
| from models.config import ConversationCacheConfiguration | ||
| from cache.cache_factory import CacheFactory | ||
| from cache.noop_cache import NoopCache | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", name="noop_cache_config_fixture") | ||
| def noop_cache_config(): | ||
| """Fixture containing initialized instance of ConversationCacheConfiguration.""" | ||
| return ConversationCacheConfiguration(type=CACHE_TYPE_NOOP) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", name="invalid_cache_type_config_fixture") | ||
| def invalid_cache_type_config(): | ||
| """Fixture containing instance of ConversationCacheConfiguration with improper settings.""" | ||
| c = ConversationCacheConfiguration() | ||
| c.type = "foo bar baz" | ||
| return c | ||
|
|
||
|
|
||
| def test_conversation_cache_noop(noop_cache_config_fixture): | ||
| """Check if NoopCache is returned by factory with proper configuration.""" | ||
| cache = CacheFactory.conversation_cache(noop_cache_config_fixture) | ||
| assert cache is not None | ||
| # check if the object has the right type | ||
| assert isinstance(cache, NoopCache) | ||
|
|
||
|
|
||
| def test_conversation_cache_wrong_cache(invalid_cache_type_config_fixture): | ||
| """Check if wrong cache configuration is detected properly.""" | ||
| with pytest.raises(ValueError, match="Invalid cache type"): | ||
| CacheFactory.conversation_cache(invalid_cache_type_config_fixture) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decorator needs typing, wraps, and typos fixed.
Preserve metadata with wraps, provide precise typing via Protocol/ParamSpec, and fix docstring typos (“Decorator”, “its”).
Apply this diff: