Skip to content
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

Fix possibility of Passing Multiple Redis Instances to the Store #35

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

### Changed

- [BREAKING CHANGE] Removed the 'redis_store' argument from the `Store` constructor
- [BREAKING CHANGE] Made the 'redis_store' property of the `Store` readonly

### Fixed

- Fixed the rendering of the reference docs from the docstrings

## [0.5.0] - 2024-02-10

### Added
Expand Down
13 changes: 9 additions & 4 deletions pydantic_redis/_shared/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class AbstractStore(BaseModel):

name: str
redis_config: RedisConfig
redis_store: Optional[Union[Redis, AioRedis]] = None
life_span_in_seconds: Optional[int] = None
select_all_fields_for_all_ids_script: Optional[Union[AsyncScript, Script]] = None
paginated_select_all_fields_for_all_ids_script: Optional[
Expand All @@ -49,25 +48,31 @@ class AbstractStore(BaseModel):
models: Dict[str, Type["AbstractModel"]] = {}
model_config = ConfigDict(arbitrary_types_allowed=True, from_attributes=True)

# protected properties
_redis_store: Optional[Union[Redis, AioRedis]] = None

def __init__(
self,
name: str,
redis_config: RedisConfig,
redis_store: Optional[Union[Redis, AioRedis]] = None,
life_span_in_seconds: Optional[int] = None,
**data: Any,
):
super().__init__(
name=name,
redis_config=redis_config,
redis_store=redis_store,
life_span_in_seconds=life_span_in_seconds,
**data,
)

self.redis_store = self._connect_to_redis()
self._redis_store = self._connect_to_redis()
self._register_lua_scripts()

@property
def redis_store(self) -> Optional[Union[Redis, AioRedis]]:
"""the redis store for the given store"""
return self._redis_store

def _connect_to_redis(self) -> Union[Redis, AioRedis]:
"""Connects the store to redis.

Expand Down
1 change: 0 additions & 1 deletion pydantic_redis/asyncio/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Store(AbstractStore):
Model name
name (str): the name of this Store
redis_config (pydantic_redis.syncio.RedisConfig): the configuration for connecting to a redis database
redis_store (Optional[redis.Redis]): an Redis instance associated with this store (default: None)
life_span_in_seconds (Optional[int]): the default time-to-live for the records inserted in this store
(default: None)
"""
Expand Down
1 change: 0 additions & 1 deletion pydantic_redis/syncio/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Store(AbstractStore):
Model name
name (str): the name of this Store
redis_config (pydantic_redis.syncio.RedisConfig): the configuration for connecting to a redis database
redis_store (Optional[redis.Redis]): an Redis instance associated with this store (default: None)
life_span_in_seconds (Optional[int]): the default time-to-live for the records inserted in this store
(default: None)
"""
Expand Down