Skip to content

Commit d836c09

Browse files
kajinamitzzzeek
authored andcommitted
pymemcache: Add support for memcached_expire_time
Added support for an additional pymemcached client parameter :paramref:`.PyMemcacheBackend.memcached_expire_time`. Pull request courtesy Takashi Kajinami. Closes: #254 Pull-request: #254 Pull-request-sha: 1e17398 Change-Id: Ia94127518f44f3061d16579738d29311ea30ff71
1 parent ee9ef57 commit d836c09

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. change::
2+
:tags: usecase, memcached
3+
4+
Added support for an additional pymemcached client parameter
5+
:paramref:`.PyMemcacheBackend.memcached_expire_time`. Pull request
6+
courtesy Takashi Kajinami.

dogpile/cache/backends/memcached.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,6 @@ class GenericMemcachedBackend(CacheBackend):
9595
9696
.. versionadded:: 0.5.7
9797
98-
:param memcached_expire_time: integer, when present will
99-
be passed as the ``time`` parameter to ``pylibmc.Client.set``.
100-
This is used to set the memcached expiry time for a value.
101-
102-
.. note::
103-
104-
This parameter is **different** from Dogpile's own
105-
``expiration_time``, which is the number of seconds after
106-
which Dogpile will consider the value to be expired.
107-
When Dogpile considers a value to be expired,
108-
it **continues to use the value** until generation
109-
of a new value is complete, when using
110-
:meth:`.CacheRegion.get_or_create`.
111-
Therefore, if you are setting ``memcached_expire_time``, you'll
112-
want to make sure it is greater than ``expiration_time``
113-
by at least enough seconds for new values to be generated,
114-
else the value won't be available during a regeneration,
115-
forcing all threads to wait for a regeneration each time
116-
a value expires.
117-
11898
The :class:`.GenericMemachedBackend` uses a ``threading.local()``
11999
object to store individual client objects per thread,
120100
as most modern memcached clients do not appear to be inherently
@@ -147,7 +127,6 @@ def __init__(self, arguments):
147127
self.url = util.to_list(arguments["url"])
148128
self.distributed_lock = arguments.get("distributed_lock", False)
149129
self.lock_timeout = arguments.get("lock_timeout", 0)
150-
self.memcached_expire_time = arguments.get("memcached_expire_time", 0)
151130

152131
def has_lock_timeout(self):
153132
return self.lock_timeout != 0
@@ -222,6 +201,29 @@ def delete_multi(self, keys):
222201
class MemcacheArgs(GenericMemcachedBackend):
223202
"""Mixin which provides support for the 'time' argument to set(),
224203
'min_compress_len' to other methods.
204+
205+
:param memcached_expire_time: integer, when present will
206+
be passed as the ``time`` parameter to the ``set`` method.
207+
This is used to set the memcached expiry time for a value.
208+
209+
.. note::
210+
211+
This parameter is **different** from Dogpile's own
212+
``expiration_time``, which is the number of seconds after
213+
which Dogpile will consider the value to be expired.
214+
When Dogpile considers a value to be expired,
215+
it **continues to use the value** until generation
216+
of a new value is complete, when using
217+
:meth:`.CacheRegion.get_or_create`.
218+
Therefore, if you are setting ``memcached_expire_time``, you'll
219+
want to make sure it is greater than ``expiration_time``
220+
by at least enough seconds for new values to be generated,
221+
else the value won't be available during a regeneration,
222+
forcing all threads to wait for a regeneration each time
223+
a value expires.
224+
225+
:param min_compress_len: Threshold length to kick in auto-compression
226+
of the value using the compressor
225227
"""
226228

227229
def __init__(self, arguments):
@@ -563,6 +565,28 @@ class PyMemcacheBackend(GenericMemcachedBackend):
563565
564566
.. versionadded:: 1.1.5
565567
568+
:param memcached_expire_time: integer, when present will
569+
be passed as the ``time`` parameter to the ``set`` method.
570+
This is used to set the memcached expiry time for a value.
571+
572+
.. note::
573+
574+
This parameter is **different** from Dogpile's own
575+
``expiration_time``, which is the number of seconds after
576+
which Dogpile will consider the value to be expired.
577+
When Dogpile considers a value to be expired,
578+
it **continues to use the value** until generation
579+
of a new value is complete, when using
580+
:meth:`.CacheRegion.get_or_create`.
581+
Therefore, if you are setting ``memcached_expire_time``, you'll
582+
want to make sure it is greater than ``expiration_time``
583+
by at least enough seconds for new values to be generated,
584+
else the value won't be available during a regeneration,
585+
forcing all threads to wait for a regeneration each time
586+
a value expires.
587+
588+
.. versionadded:: 1.3.3
589+
566590
""" # noqa E501
567591

568592
def __init__(self, arguments):
@@ -594,6 +618,9 @@ def __init__(self, arguments):
594618
"enable_retry_client is not set; retry options "
595619
"will be ignored"
596620
)
621+
self.set_arguments = {}
622+
if "memcached_expire_time" in arguments:
623+
self.set_arguments["expire"] = arguments["memcached_expire_time"]
597624

598625
def _imports(self):
599626
global pymemcache

tests/cache/test_memcached_backend.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ def test_pymemcache_enable_retry_client_not_set(self):
345345
),
346346
)
347347

348+
def test_pymemcache_memacached_expire_time(self):
349+
config_args = {"url": "127.0.0.1:11211", "memcached_expire_time": 20}
350+
with self._mock_pymemcache_fixture():
351+
backend = MockPyMemcacheBackend(config_args)
352+
backend.set("foo", "bar")
353+
eq_(
354+
self.hash_client().set.mock_calls,
355+
[mock.call("foo", "bar", expire=20)],
356+
)
357+
348358

349359
class MemcachedTest(_NonDistributedMemcachedTestSuite):
350360
backend = "dogpile.cache.memcached"

0 commit comments

Comments
 (0)