Skip to content

Commit

Permalink
renaming project (#10)
Browse files Browse the repository at this point in the history
* renaming project

* Fixing travis yml

* Back to full coverage
  • Loading branch information
Alex Kahan committed Jan 12, 2017
1 parent b172c7b commit 6a3ba46
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -4,5 +4,5 @@ python:
# command to install dependencies
install: "pip install -r requirements.txt"
# command to run tests
script: nosetests --with-coverage --cover-package redis_cache
script: nosetests --with-coverage --cover-package backends
after_success: coveralls
14 changes: 8 additions & 6 deletions README.md
Expand Up @@ -9,14 +9,16 @@ Implements high level function caching to Redis with a decorator

## Setup
```python
from redis_cache import RedisCache
r = RedisCache('localhost', 6379)
from cache_deco import Cache
from backends.redis.redis_backend import RedisBackend
redis = RedisBackend('localhost', 6379)
c = Cache(redis)
```

## Cache

```python
@r.cache()
@c.cache()
def my_method(a, b, c):
return a ** b ** c
```
Expand All @@ -26,7 +28,7 @@ def my_method(a, b, c):

e.g.
```python
@r.cache(expiration=100)
@c.cache(expiration=100)
def my_method():
...
```
Expand All @@ -39,7 +41,7 @@ e.g.
def sig_gen(*args, **kwargs):
return "?".join(args)

r.cache(signature_generator=sig_gen)
@c.cache(signature_generator=sig_gen)
def my_method():
...
```
Expand All @@ -48,7 +50,7 @@ def my_method():

e.g.
```python
@r.cache(invalidator=True)
@c.cache(invalidator=True)
def my_method():
...
```
Expand Down
22 changes: 22 additions & 0 deletions backends/test_backend_base.py
@@ -0,0 +1,22 @@
from backends.backend_base import Backend
from unittest import TestCase


class BackendTestCase(TestCase):

def setUp(self):
self.backend = Backend()

def test_not_implemented(self):

with self.assertRaises(NotImplementedError):
self.backend.get_cache('somekey')

with self.assertRaises(NotImplementedError):
self.backend.set_cache('somekey', 'somevalue')

with self.assertRaises(NotImplementedError):
self.backend.invalidate_key('somekey')

with self.assertRaises(NotImplementedError):
self.backend.set_cache_and_expire('somekey', 'somevalue', 'time')
7 changes: 4 additions & 3 deletions redis_cache/__init__.py → cache_deco/__init__.py
@@ -1,13 +1,13 @@
import functools
import pickle

from backends.redis.redis_backend import BackendException
from backends.backend_base import BackendException

# Default expiration time for a cached object if not given in the decorator
DEFAULT_EXPIRATION = 60


class RedisCache(object):
class Cache(object):
def __init__(self, client):
self.backend = client

Expand All @@ -23,6 +23,7 @@ def wrapper(*args, **kwargs):
fn_hash = self._generate_cache_key(fn, args, kwargs, **options)
try:
cache_request = self.backend.get_cache(fn_hash)
# TODO
if cache_request is '':
# Cache miss
ret = fn(*args, **kwargs)
Expand All @@ -40,7 +41,7 @@ def wrapper(*args, **kwargs):
else:
return cache_hit
except BackendException:
# If Redis fails, just execute the function as normal
# If the backend fails, just execute the function as normal
if return_invalidator:
return fn(*args, **kwargs), None
else:
Expand Down
24 changes: 12 additions & 12 deletions tests/test_redis_cache.py → tests/test_cache_deco.py
@@ -1,4 +1,4 @@
from redis_cache import RedisCache, DEFAULT_EXPIRATION
from cache_deco import Cache, DEFAULT_EXPIRATION
from backends.backend_base import Backend, BackendException
from unittest import TestCase
from mock import Mock
Expand All @@ -22,7 +22,7 @@ def test_cache_miss(self):
"""
mock_client = Mock()
# Simulates a cache miss
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = ''

Expand All @@ -49,7 +49,7 @@ def test_cache_miss_kwargs(self):
"""
mock_client = Mock()
# Simulates a cache miss
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = ''

Expand Down Expand Up @@ -86,7 +86,7 @@ def test_cache_miss_expiration(self):
ttl = 100
mock_client = Mock()
# Simulates a cache hit
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = ''

Expand All @@ -113,7 +113,7 @@ def test_cache_hit(self):
test_param = 'cache hit test'
mock_client = Mock()
# Simulates a cache hit
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = pickle.dumps(test_param)

Expand All @@ -139,7 +139,7 @@ def test_cache_hit_invalidate(self):
test_param = 'cache hit test'
mock_client = Mock()
# Simulates a cache hit
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = pickle.dumps(test_param)

Expand Down Expand Up @@ -173,7 +173,7 @@ def test_redis_failure(self):
test_param = 'cache hit test'
mock_client = Mock()
# Simulates a cache hit
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.side_effect = BackendException

Expand Down Expand Up @@ -206,7 +206,7 @@ def test_cache_custom_signature(self):
test_param = 'cache hit test'
mock_client = Mock()
# Simulates a cache hit
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = pickle.dumps(test_param)

Expand Down Expand Up @@ -244,7 +244,7 @@ def test_simple_object_pickle(self):
"""
mock_client = Mock()
# Simulates a cache miss
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = ''

Expand Down Expand Up @@ -280,7 +280,7 @@ def test_simple_object(self):
"""
mock_client = Mock()
# Simulates a cache miss
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client
redis_cache.backend.get_cache.return_value = ''

Expand Down Expand Up @@ -314,7 +314,7 @@ def test_cache_on_class_without_str(self):
"""

mock_client = Mock()
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client

class TestClass(object):
Expand Down Expand Up @@ -365,7 +365,7 @@ def test_cache_on_stateful_class_without_str(self):
values for the previous state of the object.
"""
mock_client = Mock()
redis_cache = RedisCache(Backend())
redis_cache = Cache(Backend())
redis_cache.backend = mock_client

class TestClass(object):
Expand Down

0 comments on commit 6a3ba46

Please sign in to comment.