Example #1
0
def get_cache_server_list():
    """Returns configured memcached servers.

    Works with both old-style (CACHE_BACKEND) and new-style (CACHES)
    cache configurations.

    """
    engine = ''

    # Django >= 1.3
    #
    # If somebody uses CACHE_BACKEND instead of CACHES in 1.3, it
    # automatically converts their CACHE_BACKEND configuration to the
    # appropriate CACHES configuration.  So we can safely use
    # parse_backend_conf here and it'll work with both old and new styles.
    try:
        from django.core.cache import parse_backend_conf, DEFAULT_CACHE_ALIAS
        engine, hosts, params = parse_backend_conf(DEFAULT_CACHE_ALIAS)

    # Django < 1.3
    #
    # No parse_backend_conf and DEFAULT_CACHE_ALIAS.
    except ImportError:
        from django.core.cache import parse_backend_uri
        engine, hosts, params = parse_backend_uri(settings.CACHE_BACKEND)

    if 'memcached' not in engine:
        raise ImproperlyConfigured(
            "django-memcached2 only works with memcached.  Currently using '%s'"
            % engine)

    return hosts if isinstance(hosts, list) else hosts.split(';')
Example #2
0
def get_cache_server_list():
    """Returns configured memcached servers.

    Works with both old-style (CACHE_BACKEND) and new-style (CACHES)
    cache configurations.

    """
    engine = ''

    # Django >= 1.3
    #
    # If somebody uses CACHE_BACKEND instead of CACHES in 1.3, it
    # automatically converts their CACHE_BACKEND configuration to the
    # appropriate CACHES configuration.  So we can safely use
    # parse_backend_conf here and it'll work with both old and new styles.
    try:
        from django.core.cache import parse_backend_conf, DEFAULT_CACHE_ALIAS
        engine, hosts, params = parse_backend_conf(DEFAULT_CACHE_ALIAS)

    # Django < 1.3
    #
    # No parse_backend_conf and DEFAULT_CACHE_ALIAS.
    except ImportError:
        from django.core.cache import parse_backend_uri
        engine, hosts, params = parse_backend_uri(settings.CACHE_BACKEND)

    if 'memcached' not in engine:
        raise ImproperlyConfigured("django-memcached2 only works with memcached.  Currently using '%s'" % engine)

    return hosts if isinstance(hosts, list) else hosts.split(';')
Example #3
0
def persist_hits():
    if is_cached_hitcount_enabled() and using_memcache():

        backend, location, params = parse_backend_conf(CACHED_HITCOUNT_CACHE)
        host, port = location.split(':')
        hitcount_cache = get_hitcount_cache()
        lock = hitcount_cache.get(CACHED_HITCOUNT_LOCK_KEY)
        #print  'persist_hits - check %s lock = %s' % (CACHED_HITCOUNT_LOCK_KEY, lock)
        if lock is None or lock != 1:
            try:
                #acquire a lock so no updates will occur while we are persisting the hits to DB
                hitcount_cache.set(CACHED_HITCOUNT_LOCK_KEY, 1, CACHED_HITCOUNT_CACHE_TIMEOUT)
                #print  'acquire %s lock = %s ' % (CACHED_HITCOUNT_LOCK_KEY, hitcount_cache.get(CACHED_HITCOUNT_LOCK_KEY))
                mem = MemcachedStats(host, port)
                keys = mem.keys()

                content_types = {}#used for keeping track of the content types so DB doesn't have to be queried each time
                for cache_key in keys:
                    if "hitcount__" in cache_key and not CACHED_HITCOUNT_IP_CACHE in cache_key:
                        cache_key = cache_key.split(':')[-1]#the key is a combination of key_prefix, version and key all separated by : - all we need is the key
                        count = hitcount_cache.get(cache_key)
                        if count:#only update the hit count if the is not None
                            hitcount, ctype_pk, object_pk  = cache_key.split('__')
                            if ctype_pk in content_types.keys():
                                content_type = content_types[ctype_pk]
                            else:
                                content_type = ContentType.objects.get(id=ctype_pk)
                                content_types[ctype_pk] = content_type

                            with transaction_atomic():
                                #save a new hit or increment this hits on an existing hit
                                hit, created = Hit.objects.select_for_update().get_or_create(added=datetime.utcnow().date(), object_pk=object_pk, content_type=content_type)
                                if hit and created:
                                    hit.hits = long(count)
                                    hit.save()
                                elif hit:
                                    hit.hits = hit.hits + long(count)
                                    hit.save()

                        #reset the hitcount for this object to 0 - even if it was previously None
                        hitcount_cache.set(cache_key, 0, CACHED_HITCOUNT_CACHE_TIMEOUT)
                        #print  'reset key %s to zero = %s ' % (cache_key, hitcount_cache.get(cache_key))
            except Exception, ex:
                logger.error('Unable to persist hits')
                logger.error(ex)

                raise ex
            finally:
Example #4
0
from django.conf import settings
from django.core import cache


_NEXUS_MEM_KEY = 'NEXUS_MEMCACHE_BACKEND'

_NEXUS_IMPORT_ERR_MSG = 'Nexus Memcached default backend improperly configured. ' + \
                        'Try NEXUS_MEMCACHE_BACKEND in settings.py.'


class NexusCacheImproperlyConfigured(Exception):
    '''
    Nexus Memcached backend declaration is somehow improperly configured.
    Try to set NEXUS_MEMCACHE_BACKEND in settings.py manually.
    '''
    pass


if float(django.get_version()) < 1.3: # For all versions prior to 1.3.x
    BACKEND = getattr(settings, _NEXUS_MEM_KEY, settings.CACHE_BACKEND)
else: # For all versions 1.3.x and after
    # Does a defined default exist?
    if not settings.CACHES.has_key(cache.DEFAULT_CACHE_ALIAS):
        raise NexusCacheImproperlyConfigured(_NEXUS_IMPORT_ERR_MSG)
    else: # Grab all the backends
        backend, location, params = cache.parse_backend_conf(cache.DEFAULT_CACHE_ALIAS)
        location_string = ';'.join(location)
        BACKEND = ('memcached://%s/' % location_string)


Example #5
0
from django.db.models.sql import query
from django.utils import encoding, translation

log = logging.getLogger('caching')

FOREVER = 0
NO_CACHE = -1
CACHE_PREFIX = getattr(settings, 'CACHE_PREFIX', '')
FLUSH = CACHE_PREFIX + ':flush:'
CACHE_COUNT_TIMEOUT = getattr(settings, 'CACHE_COUNT_TIMEOUT', None)

CACHE_BACKEND = getattr(settings, 'CACHE_BACKEND', '')
if CACHE_BACKEND:
    scheme, _, _ = parse_backend_uri(CACHE_BACKEND)
else:
    scheme, _, args = parse_backend_conf('default')
    CACHE_COUNT_TIMEOUT = args.get('TIMEOUT', None)
cache.scheme = scheme


class CachingManager(models.Manager):

    # Tell Django to use this manager when resolving foreign keys.
    use_for_related_fields = True

    def get_query_set(self):
        return CachingQuerySet(self.model)

    def contribute_to_class(self, cls, name):
        signals.post_save.connect(self.post_save, sender=cls)
        signals.post_delete.connect(self.post_delete, sender=cls)
Example #6
0
BDD feature: `user_center_cache.feature`

"""

__author__ = 'abael'

import re
import redis
from django.core.cache import parse_backend_conf

redis_loc = None
redis_args = []

# 获取配置中 Redis 访问Client
try:
    cls, location, args = parse_backend_conf('redis')
    if location and location.lower().find('redis') > -1:
        redis_loc = location
        redis_args = args
except:
    try:
        cls, location, args = parse_backend_conf('default')
        if location and location.lower().find('redis') > -1:
            redis_loc = location
            redis_args = args
    except:
        pass
# 没有找到
if redis_loc is None:
    sys.exit(-1)