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

Add Python 3 compatibility #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.idea
.DS_Store
*.rdb

dist/
93 changes: 93 additions & 0 deletions poetry.lock

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

19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "redis-simple-cache-py3"
version = "0.0.7"
description = "redis-simple-cache is a pythonic interface for creating a cache over redis. It provides simple decorators that can be added to any function to cache its return values."
authors = ["Vivek Narayanan, Flávio Juvenal, Sam Zaydel"]
packages = [
{ include = "redis_cache" }
]

[tool.poetry.dependencies]
python = ">=3.6"
redis_cache = "^0.1.5"
redis = "^3.3.11"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
2 changes: 1 addition & 1 deletion redis_cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from rediscache import *
from .rediscache import *
10 changes: 7 additions & 3 deletions redis_cache/rediscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import redis
import logging

from redis._compat import basestring, unicode

DEFAULT_EXPIRY = 60 * 60 * 24


Expand Down Expand Up @@ -40,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 @@ -90,7 +94,7 @@ def __init__(self,
port=self.port,
db=self.db,
password=password).connect()
except RedisNoConnException, e:
except RedisNoConnException as e:
self.connection = None
pass

Expand Down Expand Up @@ -233,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()
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

26 changes: 0 additions & 26 deletions setup.py

This file was deleted.