@@ -151,7 +151,24 @@ def disconnect(self):
151151 """Disconnect from the Redis instance"""
152152 self ._redis_conn = None
153153
154- def _get_key (self , record : Dict [str , Any ], key_field : Optional [str ] = None ) -> str :
154+ def key (self , key_value : str ) -> str :
155+ """
156+ Create a redis key as a combination of an index key prefix (optional) and specified key value.
157+ The key value is typically a unique identifier, created at random, or derived from
158+ some specified metadata.
159+
160+ Args:
161+ key_value (str): The specified unique identifier for a particular document
162+ indexed in Redis.
163+
164+ Returns:
165+ str: The full Redis key including key prefix and value as a string.
166+ """
167+ return f"{ self ._prefix } :{ key_value } " if self ._prefix else key_value
168+
169+ def _create_key (
170+ self , record : Dict [str , Any ], key_field : Optional [str ] = None
171+ ) -> str :
155172 """Construct the Redis HASH top level key.
156173
157174 Args:
@@ -166,13 +183,13 @@ def _get_key(self, record: Dict[str, Any], key_field: Optional[str] = None) -> s
166183 ValueError: If the key field is not found in the record.
167184 """
168185 if key_field is None :
169- key = uuid4 ().hex
186+ key_value = uuid4 ().hex
170187 else :
171188 try :
172- key = record [key_field ] # type: ignore
189+ key_value = record [key_field ] # type: ignore
173190 except KeyError :
174191 raise ValueError (f"Key field { key_field } not found in record { record } " )
175- return f" { self ._prefix } : { key } " if self . _prefix else key
192+ return self .key ( key_value )
176193
177194 @check_connected ("_redis_conn" )
178195 def info (self ) -> Dict [str , Any ]:
@@ -363,7 +380,7 @@ def load(
363380 ttl = kwargs .get ("ttl" )
364381 with self ._redis_conn .pipeline (transaction = False ) as pipe : # type: ignore
365382 for record in data :
366- key = self ._get_key (record , key_field )
383+ key = self ._create_key (record , key_field )
367384 pipe .hset (key , mapping = record ) # type: ignore
368385 if ttl :
369386 pipe .expire (key , ttl )
@@ -516,7 +533,7 @@ async def load(
516533
517534 async def _load (record : dict ):
518535 async with semaphore :
519- key = self ._get_key (record , key_field )
536+ key = self ._create_key (record , key_field )
520537 await self ._redis_conn .hset (key , mapping = record ) # type: ignore
521538 if ttl :
522539 await self ._redis_conn .expire (key , ttl ) # type: ignore
0 commit comments