Skip to content

Commit

Permalink
Merge pull request #35 from sopherapps/bugfix/error-multiple-redis-in…
Browse files Browse the repository at this point in the history
…stances-can-be-passed-to-store

Fix possibility of Passing Multiple Redis Instances to the Store
  • Loading branch information
Tinitto authored Jul 1, 2024
2 parents a0f79e2 + be749d7 commit 3d7d960
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
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

0 comments on commit 3d7d960

Please sign in to comment.