Python django.core.cache.cache.incr() Examples

The following are 9 code examples of django.core.cache.cache.incr(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.core.cache.cache , or try the search function .
Example #1
Source File: storages.py    From cadasta-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
def _safe_cache_incr(self):
        """ cache.incr that silently handles down cache """
        return self._down_cache_handler_partial()(cache.incr) 
Example #2
Source File: throttle_mail.py    From online-judge with GNU Affero General Public License v3.0 5 votes vote down vote up
def new_email():
    cache.add('error_email_throttle', 0, settings.DMOJ_EMAIL_THROTTLING[1])
    return cache.incr('error_email_throttle') 
Example #3
Source File: consumers.py    From django-rest-framework-reactive with Apache License 2.0 5 votes vote down vote up
def observer_evaluate(self, message):
        """Execute observer evaluation on the worker or throttle."""
        observer_id = message['observer']
        throttle_rate = get_queryobserver_settings()['throttle_rate']
        if throttle_rate <= 0:
            await self._evaluate(observer_id)
            return

        cache_key = throttle_cache_key(observer_id)
        try:
            count = cache.incr(cache_key)
            # Ignore if delayed observer already scheduled.
            if count == 2:
                await self.channel_layer.send(
                    CHANNEL_MAIN,
                    {
                        'type': TYPE_POLL,
                        'observer': observer_id,
                        'interval': throttle_rate,
                    },
                )
        except ValueError:
            count = cache.get_or_set(cache_key, default=1, timeout=throttle_rate)
            # Ignore if cache was set and increased in another thread.
            if count == 1:
                await self._evaluate(observer_id) 
Example #4
Source File: stats.py    From c3nav with Apache License 2.0 5 votes vote down vote up
def increment_cache_key(cache_key):
    try:
        cache.incr(cache_key)
    except ValueError:
        cache.set(cache_key, 1, None) 
Example #5
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_incr(self):
        # Cache values can be incremented
        cache.set("answer", 41)
        assert cache.incr("answer") == 42
        assert cache.get("answer") == 42
        assert cache.incr("answer", 10) == 52
        assert cache.get("answer") == 52
        assert cache.incr("answer", -10) == 42
        with pytest.raises(ValueError):
            cache.incr("does_not_exist") 
Example #6
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cache_versioning_incr_decr(self):
        cache.set("answer1", 37, version=1)
        cache.set("answer1", 42, version=2)
        cache.incr("answer1")
        assert cache.get("answer1", version=1) == 38
        assert cache.get("answer1", version=2) == 42
        cache.decr("answer1")
        assert cache.get("answer1", version=1) == 37
        assert cache.get("answer1", version=2) == 42

        cache.set("answer2", 37, version=1)
        cache.set("answer2", 42, version=2)
        cache.incr("answer2", version=2)
        assert cache.get("answer2", version=1) == 37
        assert cache.get("answer2", version=2) == 43
        cache.decr("answer2", version=2)
        assert cache.get("answer2", version=1) == 37
        assert cache.get("answer2", version=2) == 42

        cache.set("answer3", 37, version=1)
        cache.set("answer3", 42, version=2)
        caches["v2"].incr("answer3")
        assert cache.get("answer3", version=1) == 37
        assert cache.get("answer3", version=2) == 43
        caches["v2"].decr("answer3")
        assert cache.get("answer3", version=1) == 37
        assert cache.get("answer3", version=2) == 42

        cache.set("answer4", 37, version=1)
        cache.set("answer4", 42, version=2)
        caches["v2"].incr("answer4", version=1)
        assert cache.get("answer4", version=1) == 38
        assert cache.get("answer4", version=2) == 42
        caches["v2"].decr("answer4", version=1)
        assert cache.get("answer4", version=1) == 37
        assert cache.get("answer4", version=2) == 42 
Example #7
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_incr_range(self):
        cache.set("overwhelm", BIGINT_SIGNED_MAX - 1)
        cache.incr("overwhelm")
        with pytest.raises(IntegrityError):
            cache.incr("overwhelm") 
Example #8
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cant_incr_decimals(self):
        # Cached values that aren't ints can't be incremented
        cache.set("answer", Decimal("1.1"))
        with pytest.raises(ValueError):
            cache.incr("answer") 
Example #9
Source File: test_cache.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_int_subclass(self):
        # Storing an int subclass should return that int subclass
        cache.set("myint", MyInt(2))
        val = cache.get("myint")
        assert val.times2() == 4

        # Can't increment it since it's a pickle object on the table, not an
        # integer
        with pytest.raises(ValueError):
            cache.incr("myint")