# -*- coding: utf-8 -*- from optparse import make_option from django.core.management.base import BaseCommand from django.core.cache import cache from stats_collector.models import ObjectStats, SectionStats from stats_collector.utils import unique_items_iterator class Command(BaseCommand): def handle(self, *args, **options): pipe = cache._client.pipeline() for model in [ObjectStats, SectionStats]: key = model.make_key() # pop items from redis l = cache._client.lrange(key, 0, -1) # cache._client.ltrim(key, len(l), -1) pipe.ltrim(key, len(l), -1) unique_items = {} for k, v in unique_items_iterator(l): _val = unique_items.get(k, 0) unique_items[k] = _val + v for k, v in unique_items.items(): pipe.rpush(key, model.dump_item(k + (v,))) pipe.execute()