Esempio n. 1
0
class GlobalSettings(object):
    __metaclass__ = Singleton

    def __init__(self, prefix=None):
        if not prefix:
            self.prefix = "SetTtiNg"
        else:
            assert isinstance(prefix, str)
            self.prefix = prefix
        # cache level L1
        self.local = LocalCacheManager(default_expire_mean=30)
        # self.local.clear()
        # cache level L2
        self.redis_connection_pool = \
            redis.ConnectionPool(max_connections=128, **redis_server_settings["RemoteSetting"])
        self.redis = redis.Redis(connection_pool=self.redis_connection_pool)
        # cache level L3
        self.my_conn = MySQLdb.connect(**(mysqldb_connect_strings["Setting"]))
        self.my_conn.autocommit(False)
        self.my_cur = None

        self._is_production = None

    def clear_cache(self, l1=True, l2=False):
        if l1:
            self.local.clear()
        if l2:
            self.redis.flushdb()

    def reset_to_default(self):
        # clear L1, L2 Caches
        self.clear_cache(l1=True, l2=True)
        # reset L3
        try:
            self.my_conn.ping(True)
            with closing(self.my_conn.cursor()) as self.my_cur:
                self.my_cur.execute("""DELETE FROM `Coock`.`settings` WHERE 1""")
                for key in vars:
                    self.my_cur.execute("""INSERT INTO `Coock`.`settings`(rkey, value) VALUES(%s, %s)""", \
                        (self._convert_to_real_key(key), self._encode_value(key, vars[key]["default"])))
                self.my_conn.commit()
        except MySQLdb.Error, e:
            self.my_conn.rollback()
            print(e.message, str(e.args))
            return False
        # reset L2
        try:
            p = self.redis.pipeline(transaction=True)
            for key in vars:
                p.setex(self._convert_to_real_key(key), self._encode_value(key, vars[key]["default"]), \
                        86400 * 365 * 10)
            p.execute()
        except redis.RedisError, e:
            print(e.message, str(e.args))
            return False
Esempio n. 2
0
    def __init__(self, prefix=None):
        if not prefix:
            self.prefix = "SetTtiNg"
        else:
            assert isinstance(prefix, str)
            self.prefix = prefix
        # cache level L1
        self.local = LocalCacheManager(default_expire_mean=30)
        # self.local.clear()
        # cache level L2
        self.redis_connection_pool = \
            redis.ConnectionPool(max_connections=128, **redis_server_settings["RemoteSetting"])
        self.redis = redis.Redis(connection_pool=self.redis_connection_pool)
        # cache level L3
        self.my_conn = MySQLdb.connect(**(mysqldb_connect_strings["Setting"]))
        self.my_conn.autocommit(False)
        self.my_cur = None

        self._is_production = None