Completed
Push — master ( f6ab33...ae067c )
by Fox
01:45
created

recycle()   A

Complexity

Conditions 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
c 2
b 0
f 0
dl 0
loc 21
rs 9.0534
1
# coding: utf-8
2
from __future__ import unicode_literals
3
from __future__ import absolute_import
4
5
# django
6
from django.core.cache import caches
7
from django.utils.log import getLogger
8
9
# trigger happy
10
from django_th.my_services import MyService
11
12
logger = getLogger('django_th.trigger_happy')
13
14
def recycle():
15
    """
16
        the purpose of this tasks is to recycle the data from the cache
17
        with version=2 in the main cache
18
    """
19
    logger = getLogger('django_th.trigger_happy')
20
    all_packages = MyService.all_packages()
21
    for package in all_packages:
22
        cache = caches[package]
23
        # http://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk
24
        for service in cache.iter_keys('th_*'):
25
            try:
26
                # get the value from the cache version=2
27
                service_value = cache.get(service, version=2)
28
                # put it in the version=1
29
                cache.set(service, service_value)
30
                # remote version=2
31
                cache.delete_pattern(service, version=2)
32
            except ValueError:
33
                pass
34
    logger.info('recycle of cache done!')
35