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

Allow to use a datetime.timedelta parameter for Client.set #146

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 8 additions & 2 deletions memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from __future__ import print_function

import binascii
from datetime import timedelta
from io import BytesIO
import re
import socket
Expand All @@ -67,6 +68,8 @@

def cmemcache_hash(key):
return (((binascii.crc32(key) & 0xffffffff) >> 16) & 0x7fff) or 1

tbobm marked this conversation as resolved.
Show resolved Hide resolved

serverHashFunction = cmemcache_hash


Expand Down Expand Up @@ -709,7 +712,7 @@ def set(self, key, val, time=0, min_compress_len=0, noreply=False):
expire, either as a delta number of seconds, or an absolute
unix time-since-the-epoch value. See the memcached protocol
docs section "Storage Commands" for more info on <exptime>. We
default to 0 == cache forever.
default to 0 == cache forever. Optionnaly now accepts a timedelta.

@param min_compress_len: The threshold length to kick in
auto-compression of the value using the compressor
Expand All @@ -724,6 +727,8 @@ def set(self, key, val, time=0, min_compress_len=0, noreply=False):
@param noreply: optional parameter instructs the server to not
send the reply.
'''
if isinstance(time, timedelta):
time = int(time.total_seconds())
return self._set("set", key, val, time, min_compress_len, noreply)

def cas(self, key, val, time=0, min_compress_len=0, noreply=False):
Expand Down Expand Up @@ -1440,7 +1445,8 @@ def readline(self, raise_exception=False):
if self.socket:
recv = self.socket.recv
else:
recv = lambda bufsize: b''
def recv(bufsize):
return b''

while True:
index = buf.find(b'\r\n')
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from setuptools.depends import get_module_constant
from setuptools import setup # noqa

dl_url = "https://github.com/linsomniac/python-memcached/releases/download/{0}/python-memcached-{0}.tar.gz"

version = get_module_constant('memcache', '__version__')
setup(
Expand All @@ -15,7 +16,7 @@
maintainer="Sean Reifschneider",
maintainer_email="[email protected]",
url="https://github.com/linsomniac/python-memcached",
download_url="https://github.com/linsomniac/python-memcached/releases/download/{0}/python-memcached-{0}.tar.gz".format(version),
download_url=dl_url.format(version),
py_modules=["memcache"],
install_requires=open('requirements.txt').read().split(),
classifiers=[
Expand Down