コード例 #1
0
ファイル: cache.py プロジェクト: adreyer/django-monocle
    def get(self, key):
        """
        Retrieves an object from cache ensuring the specified key is properly formatted.
        Calls to this method will send either ``cache_miss`` or ``cache_hit`` signals
        depeding on whether the result of django ``cache.get()`` is None or not respectively.``

        :param string key: Cache key to retrieve
        :returns: Result of Django ``cache.get()``
        """
        key = self.make_key(key)
        val = _cache.get(key)

        # Django cache backend explicitly returns `None` on a miss
        if val is None:
            cache_miss.send(sender=self, key=key)

        return val
コード例 #2
0
ファイル: cache.py プロジェクト: adreyer/django-monocle
    def get_or_prime(self, key, primer=''):
        """
        Gets an object from cache or primes the cache with a specified primer
        if the specified key does not exist in cache.

        :param string key: Cache key to retrieve
        :param primer: Specific value to prime the cache with if no key exists
        :returns: Two-tuple (value, primed) where primed indicates if cache was primed
        """
        key = self.make_key(key)

        if _cache.add(key, primer, timeout=settings.CACHE_AGE):
            logger.debug('Primed cache key %s with %s for age %s' % (key, primer, settings.CACHE_AGE))
            cache_miss.send(sender=self, key=key)
            return primer, True
        else:
            cache_hit.send(sender=self, key=key)
            return _cache.get(key), False