From 175641be8a7f9f307d1513abe690c21db6df0f33 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Tue, 9 Jul 2019 16:33:02 +0300 Subject: [PATCH 01/16] Updated readme and setup.cfg for PyPI. --- README.md | 3 +++ setup.cfg | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 setup.cfg diff --git a/README.md b/README.md index 068c9e4..1e48a93 100644 --- a/README.md +++ b/README.md @@ -88,4 +88,7 @@ Robert Marshall Ben Hayden + +Python 3 support added by Omer Hanetz + LICENSE: BSD diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b88034e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md From 0cbd87b2f1cee9dd0752cd8caef1c592eb8ea233 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Tue, 9 Jul 2019 16:38:40 +0300 Subject: [PATCH 02/16] Cosmetics and inconsistent tabs. --- redis_cache/test_rediscache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/redis_cache/test_rediscache.py b/redis_cache/test_rediscache.py index f961d8c..6cdbf05 100644 --- a/redis_cache/test_rediscache.py +++ b/redis_cache/test_rediscache.py @@ -5,6 +5,7 @@ from unittest import TestCase, main import time + class ComplexNumber(object): # used in pickle test def __init__(self, real, imag): self.real = real @@ -189,7 +190,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") @@ -295,4 +296,5 @@ def test_invalidate_key(self): def tearDown(self): self.c.flush() + main() From 69e8e6cafd03abbb760d4247ebbb724d8b4ea2a6 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Tue, 9 Jul 2019 16:39:45 +0300 Subject: [PATCH 03/16] Cosmetics. --- redis_cache/test_rediscache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redis_cache/test_rediscache.py b/redis_cache/test_rediscache.py index 6cdbf05..427ac9d 100644 --- a/redis_cache/test_rediscache.py +++ b/redis_cache/test_rediscache.py @@ -1,5 +1,5 @@ -#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 From 64e1da224903a689f14d8321d39fad19c69b6f05 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 12:59:30 +0300 Subject: [PATCH 04/16] Fixed imports for python 3 + some cosmetics. --- redis_cache/test_rediscache.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/redis_cache/test_rediscache.py b/redis_cache/test_rediscache.py index 427ac9d..c4276af 100644 --- a/redis_cache/test_rediscache.py +++ b/redis_cache/test_rediscache.py @@ -1,7 +1,7 @@ # SimpleCache Tests # ~~~~~~~~~~~~~~~~~~~ from datetime import timedelta -from rediscache import SimpleCache, RedisConnect, cache_it, cache_it_json, CacheMissException, ExpiredKeyException, DoNotCache +from redis_cache.rediscache import SimpleCache, RedisConnect, cache_it, cache_it_json, CacheMissException, ExpiredKeyException, DoNotCache from unittest import TestCase, main import time @@ -21,6 +21,7 @@ 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() @@ -295,6 +296,3 @@ def test_invalidate_key(self): def tearDown(self): self.c.flush() - - -main() From 574dfed96ef77f3d2026260b77356821be18294c Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 12:59:46 +0300 Subject: [PATCH 05/16] Fixed imports for python 3. --- redis_cache/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis_cache/__init__.py b/redis_cache/__init__.py index a46e94e..ce5d31b 100644 --- a/redis_cache/__init__.py +++ b/redis_cache/__init__.py @@ -1 +1 @@ -from rediscache import * \ No newline at end of file +from .rediscache import * From 14f2784ab8d8b3c308ac3312a016f7ca06fe7a3c Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 13:00:39 +0300 Subject: [PATCH 06/16] Fixed imports for python 3 and added encoding parameters to support both binary and non-binary data. --- redis_cache/rediscache.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/redis_cache/rediscache.py b/redis_cache/rediscache.py index f951bd8..a3aa63b 100644 --- a/redis_cache/rediscache.py +++ b/redis_cache/rediscache.py @@ -6,6 +6,7 @@ import json import hashlib import redis +from redis._compat import unicode, basestring import logging DEFAULT_EXPIRY = 60 * 60 * 24 @@ -17,11 +18,13 @@ class RedisConnect(object): This makes the Simple Cache class a little more flexible, for cases where redis connection configuration needs customizing. """ - def __init__(self, host=None, port=None, db=None, password=None): + def __init__(self, host=None, port=None, db=None, password=None, decode_responses=True, encoding='iso-8859-1'): self.host = host if host else 'localhost' self.port = port if port else 6379 self.db = db if db else 0 self.password = password + self.decode_responses = decode_responses + self.encoding = encoding def connect(self): """ @@ -40,7 +43,9 @@ def connect(self): return redis.StrictRedis(host=self.host, port=self.port, db=self.db, - password=self.password) + password=self.password, + decode_responses=self.decode_responses, + encoding=self.encoding) class CacheMissException(Exception): @@ -76,7 +81,9 @@ def __init__(self, port=None, db=None, password=None, - namespace="SimpleCache"): + namespace="SimpleCache", + decode_responses=True, + encoding='iso-8859-1'): self.limit = limit # No of json encoded strings to cache self.expire = expire # Time to keys to expire in seconds @@ -89,8 +96,10 @@ def __init__(self, self.connection = RedisConnect(host=self.host, port=self.port, db=self.db, - password=password).connect() - except RedisNoConnException, e: + password=password, + decode_responses=decode_responses, + encoding=encoding).connect() + except RedisNoConnException: self.connection = None pass @@ -232,8 +241,8 @@ def mget(self, keys): def get_json(self, key): return json.loads(self.get(key)) - def get_pickle(self, key): - return pickle.loads(self.get(key)) + def get_pickle(self, key, encoding='iso-8859-1'): + return pickle.loads(self.get(key).encode(encoding)) def mget_json(self, keys): """ @@ -276,7 +285,6 @@ def __len__(self): def keys(self): return self.connection.smembers(self.get_set_name()) - def flush(self): keys = list(self.keys()) keys.append(self.get_set_name()) @@ -365,7 +373,6 @@ def func(*args, **kwargs): return decorator - def cache_it_json(limit=10000, expire=DEFAULT_EXPIRY, cache=None, namespace=None): """ Arguments and function result must be able to convert to JSON. From 967e79f33ce43ab693bb2bcc45db61ab17f32f0a Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 13:02:58 +0300 Subject: [PATCH 07/16] Fixed a bug in flush_namespace that caused flushed items not to be removed from the keys set. --- redis_cache/rediscache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis_cache/rediscache.py b/redis_cache/rediscache.py index a3aa63b..50ee4b1 100644 --- a/redis_cache/rediscache.py +++ b/redis_cache/rediscache.py @@ -298,7 +298,7 @@ def flush_namespace(self, space): keys = list(self.connection.keys(namespace)) with self.connection.pipeline() as pipe: pipe.delete(*keys) - pipe.srem(setname, *space) + pipe.srem(setname, *[key for key in self.keys() if key[:key.find(':')] == space]) pipe.execute() def get_hash(self, args): From 932fe16a34c0646892d4edb76a5ec2b24ac2fd89 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 14:11:12 +0300 Subject: [PATCH 08/16] Updated documentation and versioning before packaging. --- requirements.txt | 2 +- setup.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 61a87b2..b8c3d51 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -redis>=2.7.1 +redis>=3.2.1 diff --git a/setup.py b/setup.py index ac2a2ea..b6cf1f8 100644 --- a/setup.py +++ b/setup.py @@ -6,17 +6,18 @@ def openf(fname): return open(os.path.join(os.path.dirname(__file__), fname)) setup( - name="redis-simple-cache", + name="redis-simple-cache-3k", version="0.0.6", - author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel", + author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Ported to Python 3 by Omer Hanetz", author_email="flaviojuvenal@gmail.com", 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. ", license="3-clause BSD", keywords="decorator decorators redis cache", - url="https://github.com/vivekn/redis-simple-cache", + url="https://github.com/ohanetz/redis-simple-cache-3k", packages=['redis_cache'], long_description=openf("README.md").read(), + long_description_content_type='text/markdown', install_requires=[line.strip() for line in openf("requirements.txt") if line.strip()], classifiers=[ "Development Status :: 3 - Alpha", From fd4a88d1a66a78e2a5a2540aca6fd4fb0e32b371 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 14:18:27 +0300 Subject: [PATCH 09/16] Fixed documentation. --- README.md | 10 ++++++---- setup.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1e48a93..da867c2 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,21 @@ 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. +Forked package with python 3 support. +Original package is located here: https://github.com/vivekn/redis-simple-cache + Requirements: ------------- -redis 2.6.2 -redis-py 2.7.1 (see requirements.txt file) +redis 3.2.1 Installation: ------------- - pip install redis-simple-cache + pip install redis-simple-cache-3k or to get the latest version - git clone git://github.com/vivekn/redis-simple-cache.git + git clone git://github.com/ohanetz/redis-simple-cache.git cd redis-simple-cache python setup.py install diff --git a/setup.py b/setup.py index b6cf1f8..7f54cd9 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def openf(fname): setup( name="redis-simple-cache-3k", version="0.0.6", - author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Ported to Python 3 by Omer Hanetz", + author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Python 3 support by Omer Hanetz", author_email="flaviojuvenal@gmail.com", 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. ", From 57287dd0afdfe37c02753ed227c94da5378932e7 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 14:20:15 +0300 Subject: [PATCH 10/16] Updated version. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7f54cd9..d0c6eb2 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ def openf(fname): setup( name="redis-simple-cache-3k", - version="0.0.6", + version="0.0.6.3", author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Python 3 support by Omer Hanetz", author_email="flaviojuvenal@gmail.com", description="redis-simple-cache is a pythonic interface for creating a cache over redis. " From 3c1914a65cc9f225cb077c16acf2cb75fbb5cc9b Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Wed, 10 Jul 2019 14:25:22 +0300 Subject: [PATCH 11/16] Updated version. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d0c6eb2..2a8aeea 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ def openf(fname): setup( name="redis-simple-cache-3k", - version="0.0.6.3", + version="0.0.6.post1", author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Python 3 support by Omer Hanetz", author_email="flaviojuvenal@gmail.com", description="redis-simple-cache is a pythonic interface for creating a cache over redis. " From 01d1d72964f4a8991e8d1b41d26b90859d86718a Mon Sep 17 00:00:00 2001 From: Ryan El Kochta Date: Fri, 16 Apr 2021 22:50:03 -0400 Subject: [PATCH 12/16] Encode function arguments before hashing them Hashlib requires that any string passed to the md5 hash function be properly encoded beforehand. This patch does that, fixing the "TypeError: Unicode-objects must be encoded before hashing" that arises currently when decorating a function with @cache_it_json(). --- redis_cache/rediscache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis_cache/rediscache.py b/redis_cache/rediscache.py index 50ee4b1..14c3bc3 100644 --- a/redis_cache/rediscache.py +++ b/redis_cache/rediscache.py @@ -303,7 +303,7 @@ def flush_namespace(self, space): def get_hash(self, args): if self.hashkeys: - key = hashlib.md5(args).hexdigest() + key = hashlib.md5(args.encode('utf-8')).hexdigest() else: key = pickle.dumps(args) return key From 457550f7bae1c0157d3abe1f377b9cdb60504b65 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Mon, 29 Nov 2021 00:36:18 +0200 Subject: [PATCH 13/16] Updated requirements.txt for redis 4 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b8c3d51..d487736 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -redis>=3.2.1 +redis>=4.0.0 From 8cfd3953bc889b544dbda80d5c7411a1ebf435a9 Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Mon, 29 Nov 2021 10:47:00 +0200 Subject: [PATCH 14/16] docs --- README.md | 8 ++++---- setup.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index da867c2..ad822ff 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Original package is located here: https://github.com/vivekn/redis-simple-cache Requirements: ------------- -redis 3.2.1 +redis 4.0.0 Installation: ------------- @@ -16,8 +16,8 @@ Installation: or to get the latest version - git clone git://github.com/ohanetz/redis-simple-cache.git - cd redis-simple-cache + git clone git://github.com/ohanetz/redis-simple-cache-3k.git + cd redis-simple-cache-3k python setup.py install Usage: @@ -91,6 +91,6 @@ Robert Marshall Ben Hayden -Python 3 support added by Omer Hanetz +Python 3 and Redis 4 support added by Omer Hanetz LICENSE: BSD diff --git a/setup.py b/setup.py index 2a8aeea..002a954 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def openf(fname): setup( name="redis-simple-cache-3k", version="0.0.6.post1", - author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Python 3 support by Omer Hanetz", + author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Python 3 and Redis 4 support by Omer Hanetz", author_email="flaviojuvenal@gmail.com", 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. ", From de548928a2da2343644cf99e40d5665ee7889aaa Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Mon, 29 Nov 2021 11:17:16 +0200 Subject: [PATCH 15/16] Removed redis 4 incompatible imports --- redis_cache/rediscache.py | 6 +++++- redis_cache/test_rediscache.py | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/redis_cache/rediscache.py b/redis_cache/rediscache.py index 50ee4b1..93f1faf 100644 --- a/redis_cache/rediscache.py +++ b/redis_cache/rediscache.py @@ -6,8 +6,12 @@ import json import hashlib import redis -from redis._compat import unicode, basestring import logging +import sys + +if sys.version_info[0] >= 3: # Maintain support for python 2.x + basestring = str + unicode = str DEFAULT_EXPIRY = 60 * 60 * 24 diff --git a/redis_cache/test_rediscache.py b/redis_cache/test_rediscache.py index c4276af..9587fc7 100644 --- a/redis_cache/test_rediscache.py +++ b/redis_cache/test_rediscache.py @@ -296,3 +296,7 @@ def test_invalidate_key(self): def tearDown(self): self.c.flush() + + +main() + From 926c40445cb421b9d4e2be79229a34c9d75dad8c Mon Sep 17 00:00:00 2001 From: Omer Hanetz Date: Mon, 29 Nov 2021 11:28:19 +0200 Subject: [PATCH 16/16] Updated version info --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 002a954..9c748a4 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ def openf(fname): setup( name="redis-simple-cache-3k", - version="0.0.6.post1", + version="0.0.7", author="Vivek Narayanan, Flávio Juvenal, Sam Zaydel. Python 3 and Redis 4 support by Omer Hanetz", author_email="flaviojuvenal@gmail.com", description="redis-simple-cache is a pythonic interface for creating a cache over redis. "