-
Notifications
You must be signed in to change notification settings - Fork 9
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
Ensure put returns key #35
Conversation
key = store.put(key, value) | ||
assert isinstance(key, str) | ||
new_key = store.put(key, value) | ||
assert key == new_key |
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.
I think I have seen some cases (in the redis store?) where you can have new_key != key
is key is None
. Should we handle that here, too?
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.
Here we return the key
:
minimalkv/minimalkv/memory/redisstore.py
Lines 75 to 97 in 54cc308
def _put( | |
self, key: str, value: bytes, ttl_secs: Optional[Union[str, int, float]] = None | |
) -> str: | |
assert ttl_secs is not None | |
if ttl_secs in (NOT_SET, FOREVER): | |
# if we do not care about ttl, just use set | |
# in redis, using SET will also clear the timeout | |
# note that this assumes that there is no way in redis | |
# to set a default timeout on keys | |
self.redis.set(key, value) | |
else: | |
ittl = None | |
try: | |
ittl = int(ttl_secs) | |
except ValueError: | |
pass # let it blow up further down | |
if ittl == ttl_secs: | |
self.redis.setex(key, ittl, value) | |
else: | |
self.redis.psetex(key, int(ttl_secs * 1000), value) | |
return key |
I guess there it the KeyTransformingDecorator
(undocumented):
minimalkv/minimalkv/decorator.py
Lines 101 to 102 in 54cc308
def put(self, key: str, *args, **kwargs): # noqa D | |
return self._unmap_key(self._dstore.put(self._map_key(key), *args, **kwargs)) |
and the HashDecorator
:
Lines 49 to 75 in 54cc308
def put(self, key: Optional[str], data: bytes, *args, **kwargs): | |
"""Store bytestring data at key. | |
Parameters | |
---------- | |
key : str or None | |
The key under which the data is to be stored. If None, the hash of data is | |
used. | |
data : bytes | |
Data to be stored at key, must be of type ``bytes``. | |
Returns | |
------- | |
str | |
The key under which data was stored. | |
Raises | |
------ | |
ValueError | |
If the key is not valid. | |
IOError | |
If storing failed or the file could not be read. | |
""" | |
if key is None: | |
key = self._template.format(self.hashfunc(data).hexdigest()) | |
return self._dstore.put(key, data, *args, **kwargs) # type: ignore |
The former is surely untested (else tests would be failing).
Let's merge this and then worry about other methods later. |
First step towards #28.