Completed
Push — master ( 07086e...069f57 )
by Fox
01:32
created

recycle()   B

Complexity

Conditions 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
c 2
b 0
f 0
dl 0
loc 23
rs 8.2508
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
15
def recycle():
16
    """
17
        the purpose of this tasks is to recycle the data from the cache
18
        with version=2 in the main cache
19
    """
20
    logger = getLogger('django_th.trigger_happy')
21
    all_packages = MyService.all_packages()
22
    for package in all_packages:
23
        if package == 'th_instapush':
24
            continue
25
        cache = caches[package]
26
        # http://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk
27
        for service in cache.iter_keys('th_*'):
28
            try:
29
                # get the value from the cache version=2
30
                service_value = cache.get(service, version=2)
31
                # put it in the version=1
32
                cache.set(service, service_value)
33
                # remote version=2
34
                cache.delete_pattern(service, version=2)
35
            except ValueError:
36
                pass
37
    logger.info('recycle of cache done!')
38