def archive_cat_counts(self, start_time, preserve=True):
        ts_key = 'ts_%s' % (self.cat_id,)
        counter_key = 'counter_%s' % (self.cat_id,)
        check_mark = calendar.timegm(start_time.timetuple())
        now  = calendar.timegm(time.gmtime())
        counters_to_delete = []
        count_dict = self.redis_conn.hgetall(counter_key)

        interesting_ts = [
            x for x in count_dict if int(x) < check_mark
        ]

        for ts in interesting_ts:
            if ts == now:
                continue
            new_value = count_dict[ts]
            if preserve:
                existing_entry = self.range(ts, ts)
                if existing_entry:
                    # If there is an existing entry then don't overwrite it.
                    # If we do find something it should be the only entry for
                    # that second timeslot.
                    new_value += existing_entry[0].count
            self.redis_conn.zadd(
                ts_key, self.generate_ts_value(ts, new_value), ts
            )
            counters_to_delete.append(ts)

        # Remove the counters from the 'counter' key.
        # We store longterm counters in the timeseries key (ts).
        for counter in counters_to_delete:
            self.redis_conn.hdel(counter_key, counter)
Move everything before start_time into a Sorted Set.
dataset/ETHPy150Open gmcquillan/firetower/firetower/category.py/TimeSeries.archive_cat_counts