from __future__ import absolute_import, division
from dimagi.utils.couch.cache.cache_core import get_redis_client


class MessagingRuleProgressHelper(object):

    def __init__(self, rule_id):
        self.rule_id = rule_id
        self.client = get_redis_client()

    @property
    def in_progress_key(self):
        return 'messaging-rule-processing-in-progress-%s' % self.rule_id

    @property
    def current_key(self):
        return 'messaging-rule-case-count-current-%s' % self.rule_id

    @property
    def total_key(self):
        return 'messaging-rule-case-count-total-%s' % self.rule_id

    def set_initial_progress(self):
        self.client.set(self.current_key, 0)
        self.client.set(self.total_key, 0)
        self.client.set(self.in_progress_key, 1)
        self.client.expire(self.current_key, 48 * 60 * 60)
        self.client.expire(self.total_key, 48 * 60 * 60)
        self.client.expire(self.in_progress_key, 48 * 60 * 60)

    def set_rule_complete(self):
        self.client.set(self.in_progress_key, 0)

    def increment_current_case_count(self):
        self.client.incr(self.current_key)

    def set_total_case_count(self, value):
        self.client.set(self.total_key, value)

    @staticmethod
    def _int_or_zero(value):
        try:
            return int(value)
        except (TypeError, ValueError):
            return 0

    def get_progress_pct(self):
        current = self._int_or_zero(self.client.get(self.current_key))
        total = self._int_or_zero(self.client.get(self.total_key))

        if not current or not total:
            return 0

        if current >= total:
            return 100

        return int(round(100.0 * current / total, 0))

@@ -259,16 +259,85 @@
 ient()%0A%0A
+    @property%0A    def key_expiry(self):%0A        return 48 * 60 * 60%0A%0A
     @pro
@@ -879,36 +879,39 @@
 urrent_key, 
-48 * 60 * 60
+self.key_expiry
 )%0A        se
@@ -943,28 +943,31 @@
 al_key, 
-48 * 60 * 60
+self.key_expiry
 )%0A      
@@ -1009,28 +1009,31 @@
 ss_key, 
-48 * 60 * 60
+self.key_expiry
 )%0A%0A    d
@@ -1287,16 +1287,76 @@
 , value)
+%0A        self.client.expire(self.total_key, self.key_expiry)
 %0A%0A    @s

Set key expiry every time the value is set