示例#1
0
def cache_get_batch_data(key_prefix, index_list):
    logger.debug('do cache_get_batch_data, key_prefix: %s, index_list: %s' % (key_prefix, index_list))
    keys = []
    for index in index_list:
        key = key_prefix + '_' + index
        if isinstance(key, unicode):
            key = key.encode('utf8')
        if key != key.strip():
            logger.warning('key has Illegal character space. key:%s' % key)
        keys.append(key.strip())
    #redis = model_connect.get_redis()
    redis_py = model_connect.get_redis_py()
    if None == redis_py:
        logger.error('get redist connect error.')
        return (False, {})
    try:
        logger.debug('cache_get_batch_data, keys: %s' % keys)
        #datas = redis.MGet( keys )
        datas = redis_py.mget( keys )
        res = {}
        for i, data in enumerate(datas):
            if None != data and '' != data:
                res[index_list[i]] = data
        return (True, res)
    except:
        logger.error('cache_get_batch_data failed. error:%s' % str(sys.exc_info()))
        return (False, {})
示例#2
0
def clear_redis():
    redis_py = model_connect.get_redis_py() 
    if None == redis_py:
        print 'ERROR, get redis py failed.'
    del_keys = redis_py.keys()
    pipe = redis_py.pipeline()
    for key in del_keys:
        pipe.delete(key)
    if False == pipe.execute():
        print 'ERROR pipeline execute failed.'
def cache_clear_key(key):
    logger.debug("cache delete key: %s" % key)
    redis_py = model_connect.get_redis_py()
    if None == redis_py:
        logger.error("get redis py connect failed.")
        return False
    try:
        redis_py.delete(key)
        logger.debug("cache delete data, redis delete success.")
        return True
    except:
        logger.error("cache_clear_prefix_index failed. error: %s" % str(sys.exc_info()))
        return False
示例#4
0
def cache_get_data(key):
    #redis = model_connect.get_redis()
    redis_py = model_connect.get_redis_py()
    if None == redis_py:
        logger.error('get redist connect error.')
        return (False, None)
    #data = redis.Get(key)
    if isinstance(key, unicode):
        key = key.encode('utf8')
    if key != key.strip():
        logger.warning('key has Illegal character space. key:%s' % key)
    try:
        data = redis_py.get(key.strip())
        return (True, data)
    except:
        logger.error('cache_get_data failed. error: %s' % str(sys.exc_info()))
        return (False, None)
def cache_update_batch(kv_dct):
    redis_py = model_connect.get_redis_py()
    if None == redis_py:
        logger.error("get redis py connect failed.")
        return False
    try:
        pipe = redis_py.pipeline()
        for k, v in kv_dct.items():
            if k != k.strip():
                logger.warning("key with illegal character space. key:%s" % k)
            pipe.set(k.strip(), v)
        res = pipe.execute()
        logger.debug("cache batch set data, pipeline execute success.")
        return True
    except:
        logger.error("cache_update_batch. error: %s" % str(sys.exc_info()))
        return False
def cache_clear_prefix_index(redis_index):
    redis_py = model_connect.get_redis_py()
    if None == redis_py:
        logger.error("get redis py connect failed.")
        return False
    # print 'key_pattern: %s' % key_pattern
    try:
        del_keys = redis_py.keys(redis_index + "_*")
        pipe = redis_py.pipeline()
        logger.debug("del_keys: %s" % del_keys)
        for key in del_keys:
            logger.debug("cache delete key : %s" % key)
            pipe.delete(key)
        res = pipe.execute()
        if False == res:
            logger.error("cache batch delete data, pipeline execute failed.")
            return False
        logger.debug("cache batch delete data, pipeline execute success.")
        return True
    except:
        logger.error("cache_clear_prefix_index failed. error: %s" % str(sys.exc_info()))
        return False
def cache_clear_update_batch(redis_index, kv_dct):
    redis_py = model_connect.get_redis_py()
    if None == redis_py:
        logger.error("get redis py connect failed.")
        return False
    try:
        del_keys = redis_py.keys(redis_index + "_*")
        pipe = redis_py.pipeline()
        for key in del_keys:
            logger.debug("cache delete key : %s" % key)
            pipe.delete(key)
        for k, v in kv_dct.items():
            # logger.debug('set cache key: %s, value: %s' % (k, v))
            logger.debug("set cache key: %s" % k)
            if k != k.strip():
                logger.warning("key with illegal character space. key:%s" % k)
            pipe.set(k.strip(), v)
        res = pipe.execute()
        logger.debug("cache clear update batch, pipeline execute success.")
        return True
    except:
        logger.error("cache_clear_update_batch failed. error: %s" % str(sys.exc_info()))
        return False
示例#8
0
def cache_set_data(key_prefix, key_tail, value, expire_time = None):
    if None == value:
        logger.error('value is none. key: %s' % (key_prefix + '_' + key_tail))
        return False
    #redis = model_connect.get_redis()
    redis_py = model_connect.get_redis_py()
    if None == redis_py:
        logger.error('get redist connect error.')
        return False
    key = key_prefix + '_' + key_tail
    #if not redis.Set(key, value):
    if key != key.strip():
        logger.warning('key has Illegal character space. key:%s' % key)
        key = key.strip()
    try:
        if not redis_py.set(key, value):
            logger.error('redist set error. key: %s' % key)
            return False
        if None != expire_time and False == redis_py.expire(key, expire_time):
            return False
        return True
    except:
        logger.error('cache_set_data failed. error: %s' % str(sys.exc_info()))
        return False