Skip to content

Commit

Permalink
Merge pull request #17 from globality-corp/release/0.9.0
Browse files Browse the repository at this point in the history
Release/0.9.0
  • Loading branch information
albsadowski authored Jan 3, 2020
2 parents 1f1d730 + d783f57 commit 359154d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 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.8.0
current_version = 0.9.0
commit = False
tag = False

Expand Down
38 changes: 15 additions & 23 deletions microcosm_caching/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,30 @@ class SerializationFlag(IntEnum):
JSON = 2


def json_serializer(key, value):
class JsonSerializerDeserializer:
"""
Simple JSON serializer for use with caching backends
that only support string/bytes value storage.
Memcached is the primary use case.
"""
if isinstance(value, str) or isinstance(value, bytes):
return value, SerializationFlag.STRING.value

return dumps(value), SerializationFlag.JSON.value
def serialize(self, key, value):
if isinstance(value, str) or isinstance(value, bytes):
return value, SerializationFlag.STRING.value

return dumps(value), SerializationFlag.JSON.value

def json_deserializer(key, value, flags):
"""
Simple JSON deserializer for use with caching backends
that only support string/bytes value storage.
Memcached is the primary use case.
"""
if flags == SerializationFlag.STRING:
if isinstance(value, bytes):
value = value.decode("utf-8")
return value
elif flags == SerializationFlag.JSON:
return loads(value)
def deserialize(self, key, value, flags):
if flags == SerializationFlag.STRING:
if isinstance(value, bytes):
value = value.decode("utf-8")
return value
elif flags == SerializationFlag.JSON:
return loads(value)

raise ValueError(f"Unknown serialization format flags: {flags}")
raise ValueError(f"Unknown serialization format flags: {flags}")


class MemcachedCache(CacheBase):
Expand All @@ -69,15 +63,13 @@ def __init__(
servers: Optional[Tuple[str, int]] = None,
connect_timeout=None,
read_timeout=None,
serializer=json_serializer,
deserializer=json_deserializer,
serde=None,
testing=False,
):
client_kwargs = dict(
connect_timeout=connect_timeout,
timeout=read_timeout,
serializer=serializer,
deserializer=deserializer,
serde=serde or JsonSerializerDeserializer(),
)

if testing:
Expand Down
6 changes: 3 additions & 3 deletions microcosm_caching/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
from hamcrest import assert_that, is_
from parameterized import parameterized

from microcosm_caching.memcached import SerializationFlag, json_deserializer, json_serializer
from microcosm_caching.memcached import JsonSerializerDeserializer, SerializationFlag


@parameterized([
("key", "string-value", ("string-value", SerializationFlag.STRING.value)),
("key", dict(foo="bar"), (dumps(dict(foo="bar")), SerializationFlag.JSON.value)),
])
def test_serializer(key, value, result):
assert_that(json_serializer(key, value), is_(result))
assert_that(JsonSerializerDeserializer().serialize(key, value), is_(result))


@parameterized([
("key", "string-value", SerializationFlag.STRING.value, "string-value"),
("key", dumps(dict(foo="bar")), SerializationFlag.JSON.value, dict(foo="bar")),
])
def test_deserializer(key, value, flag, expected_value):
assert_that(json_deserializer(key, value, flag), is_(expected_value))
assert_that(JsonSerializerDeserializer().deserialize(key, value, flag), is_(expected_value))
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


project = "microcosm-caching"
version = "0.8.0"
version = "0.9.0"

setup(
name=project,
Expand All @@ -21,7 +21,7 @@
"marshmallow>=3.0.0",
"microcosm>=2.12.0",
"microcosm-logging>=1.3.0",
"pymemcache>=2.2.2",
"pymemcache>=3.0.0",
"simplejson>=3.16.0",
],
extras_require={
Expand Down

0 comments on commit 359154d

Please sign in to comment.