Skip to content

Commit

Permalink
Added django memoize to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anx-ckreuzberger committed May 9, 2017
1 parent b202608 commit bd9c40f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ pip install -r requirements_test.txt
python setup.py install
cd tests
python manage.py test
```
```

## Cache Backend
If you want to use a cache for the session store, you can install [django-memoize](https://pythonhosted.org/django-memoize/) and add `'memoize'` to `INSTALLED_APPS`.

Then you need to use ``CachedMultiTokenAuthentication`` instead of ``MultiTokenAuthentication``.

```bash
pip install django-memoize
```

22 changes: 22 additions & 0 deletions django_rest_multitokenauth/coreauthentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

from django_rest_multitokenauth.models import MultiToken

# try to import memoize
memoize = None

try:
from memoize import memoize
except ImportError:
pass


class MultiTokenAuthentication(TokenAuthentication):
"""
Expand All @@ -23,3 +31,17 @@ def get_model(self):
if self.model is not None:
return self.model
return MultiToken


# if memoize is available, create a cached multi token authentication class, which uses redis as a cache
if memoize:
class CachedMultiTokenAuthentication(MultiTokenAuthentication):
"""
Cached MultiTokenAuthentication, using django-memoize
"""
@memoize(timeout=60)
def authenticate_credentials(self, key):
return super(CachedMultiTokenAuthentication, self).authenticate_credentials(key)

def __repr__(self):
return self.__class__.__name__
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='django-rest-multitokenauth',
version='1.0.0',
version='1.1.0',
packages=find_packages(),
include_package_data=True,
license='BSD License',
Expand Down

0 comments on commit bd9c40f

Please sign in to comment.