Skip to content

Commit

Permalink
Update to the new redis version
Browse files Browse the repository at this point in the history
  • Loading branch information
Rydra committed Mar 5, 2019
1 parent c6bffe2 commit 4577d48
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 15 deletions.
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[dev-packages]
pytest = "*"

[packages]
redis = "*"

[requires]
python_version = "3.6"
81 changes: 81 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions redis_cache/rediscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def connect(self):
return redis.StrictRedis(host=self.host,
port=self.port,
db=self.db,
password=self.password)
password=self.password,
charset="iso-8859-1",
decode_responses=True)


class CacheMissException(Exception):
Expand Down Expand Up @@ -235,7 +237,7 @@ def get_json(self, key):
return json.loads(self.get(key))

def get_pickle(self, key):
return pickle.loads(self.get(key))
return pickle.loads(self.get(key).encode("iso-8859-1"))

def mget_json(self, keys):
"""
Expand Down
34 changes: 22 additions & 12 deletions redis_cache/test_rediscache.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#SimpleCache Tests
#~~~~~~~~~~~~~~~~~~~
# SimpleCache Tests
# ~~~~~~~~~~~~~~~~~~~
from datetime import timedelta
from rediscache import SimpleCache, RedisConnect, cache_it, cache_it_json, CacheMissException, ExpiredKeyException, DoNotCache
from unittest import TestCase, main

import pytest

from rediscache import SimpleCache, RedisConnect, cache_it, cache_it_json, CacheMissException, ExpiredKeyException, \
DoNotCache
from unittest import TestCase
import time


class ComplexNumber(object): # used in pickle test
def __init__(self, real, imag):
self.real = real
Expand All @@ -14,12 +19,13 @@ def __eq__(self, other):
return self.real == other.real and self.imag == other.imag


class SimpleCacheTest(TestCase):
class TestSimpleCache(TestCase):

def setUp(self):
self.c = SimpleCache(10) # Cache that has a maximum limit of 10 keys
self.assertIsNotNone(self.c.connection)
self.redis = RedisConnect().connect()

def test_expire(self):
quick_c = SimpleCache()

Expand All @@ -40,6 +46,7 @@ def test_kwargs_decorator(self):
@cache_it_json(cache=self.c)
def add_it(a, b=10, c=5):
return a + b + c

add_it(3)
self.assertEqual(add_it(3), 18)
add_it(5, b=7)
Expand All @@ -58,17 +65,19 @@ def test_json(self):
self.assertEqual(self.c.get_json("json"), payload)

def test_pickle(self):
payload = ComplexNumber(3,4)
payload = ComplexNumber(3, 4)
self.c.store_pickle("pickle", payload)
self.assertEqual(self.c.get_pickle("pickle"), payload)

def test_decorator(self):
self.redis.flushall()
mutable = []

@cache_it(cache=self.c)
def append(n):
mutable.append(n)
return mutable

append(1)
len_before = len(mutable)
mutable_cached = append(1)
Expand Down Expand Up @@ -142,10 +151,12 @@ def test_decorator_json(self):
import random

mutable = {}

@cache_it_json(cache=self.c)
def set_key(n):
mutable[str(random.random())] = n
return mutable

set_key('a')
len_before = len(mutable)
mutable_cached = set_key('a')
Expand All @@ -160,11 +171,12 @@ def test_decorator_complex_type(self):
@cache_it(cache=self.c)
def add(x, y):
return ComplexNumber(x.real + y.real, x.imag + y.imag)
result = add(ComplexNumber(3,4), ComplexNumber(4,5))
result_cached = add(ComplexNumber(3,4), ComplexNumber(4,5))

result = add(ComplexNumber(3, 4), ComplexNumber(4, 5))
result_cached = add(ComplexNumber(3, 4), ComplexNumber(4, 5))
self.assertNotEqual(id(result), id(result_cached))
self.assertEqual(result, result_cached)
self.assertEqual(result, complex(3,4) + complex(4,5))
self.assertEqual(result, complex(3, 4) + complex(4, 5))

def test_cache_limit(self):
for i in range(100):
Expand All @@ -189,7 +201,7 @@ def test_flush(self):
connection.delete("will_not_be_deleted")

def test_flush_namespace(self):
self.redis.flushall()
self.redis.flushall()
self.c.store("foo:one", "bir")
self.c.store("foo:two", "bor")
self.c.store("fii", "bur")
Expand Down Expand Up @@ -294,5 +306,3 @@ def test_invalidate_key(self):

def tearDown(self):
self.c.flush()

main()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
redis==2.10.5
redis==3.2.0

0 comments on commit 4577d48

Please sign in to comment.