Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhadani committed Sep 18, 2019
2 parents a27380b + bee5621 commit 997d397
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.0
current_version = 0.2.0
commit = False
tag = False

Expand Down
8 changes: 7 additions & 1 deletion microcosm_caching/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ def get(self, key):
pass

@abstractmethod
def set(self, key, value):
def set(self, key, value, ttl=None):
"""
Set a key, value pair to the cache.
Optional ttl (time-to-live) value should be in seconds.
"""
pass
7 changes: 5 additions & 2 deletions microcosm_caching/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@ def __init__(
def get(self, key):
return self.client.get(key)

def set(self, key, value):
return self.client.set(key, value)
def set(self, key, value, ttl=None):
if ttl is None:
# pymemcache interprets 0 as no expiration
ttl = 0
return self.client.set(key, value, expire=ttl)
22 changes: 20 additions & 2 deletions microcosm_caching/tests/test_memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Unit-tests for memcached cache backend.
"""
from time import sleep

from hamcrest import assert_that, equal_to, is_
from parameterized import parameterized

Expand All @@ -24,9 +26,25 @@ def setup(self):
)),
])
def test_set_and_get_value(self, key, value):
self.cache.set("key", value)
self.cache.set(key, value)

assert_that(
self.cache.get("key"),
self.cache.get(key),
is_(equal_to(value)),
)

def test_set_with_ttl_works(self):
self.cache.set("key", "value", ttl=1)

assert_that(
self.cache.get("key"),
is_(equal_to("value")),
)

# retrieving key again should fail once ttl expired
# Nb. using sleep in unit-tests is not a best practice generally.
sleep(1)
assert_that(
self.cache.get("key"),
is_(equal_to(None)),
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


project = "microcosm-caching"
version = "0.1.0"
version = "0.2.0"

setup(
name=project,
Expand Down

0 comments on commit 997d397

Please sign in to comment.