forked from gusdan/django-elasticache
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Depends django-pymemcache #103
Merged
+145
−123
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ Requirements | |
|
||
* pymemcache | ||
* Django>=2.2 | ||
* django-pymemcache>=1.0 | ||
|
||
Installation | ||
------------ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,37 +5,19 @@ | |
import socket | ||
from functools import wraps | ||
|
||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
from django.core.cache import InvalidCacheBackendError | ||
from django.core.cache.backends.memcached import BaseMemcachedCache | ||
|
||
from . import client as pymemcache_client | ||
from djpymemcache import client as djpymemcache_client | ||
from .cluster_utils import get_cluster_info | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def serialize_pickle(key, value): | ||
if isinstance(value, str): | ||
return value, 1 | ||
return pickle.dumps(value), 2 | ||
|
||
|
||
def deserialize_pickle(key, value, flags): | ||
if flags == 1: | ||
return value | ||
if flags == 2: | ||
return pickle.loads(value) | ||
|
||
|
||
def invalidate_cache_after_error(f): | ||
""" | ||
catch any exception and invalidate internal cache with list of nodes | ||
Catch any exception and invalidate internal cache with list of nodes | ||
""" | ||
@wraps(f) | ||
def wrapper(self, *args, **kwds): | ||
|
@@ -49,37 +31,47 @@ def wrapper(self, *args, **kwds): | |
|
||
class ElastiPyMemCache(BaseMemcachedCache): | ||
""" | ||
backend for Amazon ElastiCache (memcached) with auto discovery mode | ||
it used pyMemcache | ||
Backend for Amazon ElastiCache (memcached) with auto discovery mode | ||
it used pymemcache | ||
""" | ||
def __init__(self, server, params): | ||
params['OPTIONS'] = params.get('OPTIONS', {}) | ||
params['OPTIONS'].setdefault('ignore_exc', True) | ||
|
||
self._cluster_timeout = params['OPTIONS'].pop( | ||
'cluster_timeout', | ||
socket._GLOBAL_DEFAULT_TIMEOUT, | ||
) | ||
self._ignore_cluster_errors = params['OPTIONS'].pop( | ||
'ignore_cluster_errors', | ||
False, | ||
) | ||
|
||
super().__init__( | ||
server, | ||
params, | ||
library=pymemcache_client, | ||
value_not_found_exception=ValueError) | ||
library=djpymemcache_client, | ||
value_not_found_exception=ValueError, | ||
) | ||
|
||
if len(self._servers) > 1: | ||
raise InvalidCacheBackendError( | ||
'ElastiCache should be configured with only one server ' | ||
'(Configuration Endpoint)') | ||
'(Configuration Endpoint)', | ||
) | ||
|
||
if len(self._servers[0].split(':')) != 2: | ||
raise InvalidCacheBackendError( | ||
'Server configuration should be in format IP:port') | ||
|
||
self._cluster_timeout = self._options.get( | ||
'cluster_timeout', socket._GLOBAL_DEFAULT_TIMEOUT) | ||
self._ignore_cluster_errors = self._options.get( | ||
'ignore_cluster_errors', False) | ||
'Server configuration should be in format IP:Port', | ||
) | ||
|
||
def clear_cluster_nodes_cache(self): | ||
"""clear internal cache with list of nodes in cluster""" | ||
"""Clear internal cache with list of nodes in cluster""" | ||
if hasattr(self, '_client'): | ||
del self._client | ||
|
||
def get_cluster_nodes(self): | ||
""" | ||
return list with all nodes in cluster | ||
""" | ||
"""Return list with all nodes in cluster""" | ||
server, port = self._servers[0].split(':') | ||
try: | ||
return get_cluster_info( | ||
|
@@ -92,47 +84,49 @@ def get_cluster_nodes(self): | |
logger.debug( | ||
'Cannot connect to cluster %s, err: %s', | ||
self._servers[0], | ||
err | ||
err, | ||
) | ||
return [] | ||
|
||
@property | ||
def _cache(self): | ||
|
||
if getattr(self, '_client', None) is None: | ||
|
||
options = self._options | ||
options['serializer'] = serialize_pickle | ||
options['deserializer'] = deserialize_pickle | ||
options.setdefault('ignore_exc', True) | ||
options.pop('cluster_timeout', None) | ||
options.pop('ignore_cluster_errors', None) | ||
|
||
self._client = self._lib.Client( | ||
self.get_cluster_nodes(), **options) | ||
|
||
self.get_cluster_nodes(), **self._options) | ||
return self._client | ||
|
||
@invalidate_cache_after_error | ||
def get(self, *args, **kwargs): | ||
return super().get(*args, **kwargs) | ||
def add(self, *args, **kwargs): | ||
return super().add(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def get_many(self, *args, **kwargs): | ||
return super().get_many(*args, **kwargs) | ||
def get(self, *args, **kwargs): | ||
return super().get(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def set(self, *args, **kwargs): | ||
return super().set(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def set_many(self, *args, **kwargs): | ||
return super().set_many(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def delete(self, *args, **kwargs): | ||
return super().delete(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Support all methods |
||
def get_many(self, *args, **kwargs): | ||
return super().get_many(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def set_many(self, *args, **kwargs): | ||
return super().set_many(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def delete_many(self, *args, **kwargs): | ||
return super().delete_many(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def incr(self, *args, **kwargs): | ||
return super().incr(*args, **kwargs) | ||
|
||
@invalidate_cache_after_error | ||
def decr(self, *args, **kwargs): | ||
return super().decr(*args, **kwargs) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As same as https://github.com/django-pymemcache/django-pymemcache/blob/master/djpymemcache/client.py