예제 #1
0
    def process(self, q_item):
        if q_item.method == 'post':
            json_resp = self.post(url=q_item.url,
                                  data=json.loads(q_item.payload))
        else:
            json_resp = self.get(url=q_item.url)

        if 'error' in session:
            q_item.status = "error"
            error = session.pop('error')
            q_item.response = json.dumps(error)
            if q_item.retry_count is None:
                q_item.retry_count = 0
            else:
                q_item.retry_count += 1
            Queue.update(q_item)
            return error
        else:
            if q_item.cache_name:
                Cache.set_data(q_item.cache_name,
                               json_resp,
                               is_unique=q_item.cache_is_unique)

            q_item.status = "ok"
            q_item.response = json.dumps(json_resp)

        Queue.update(q_item)
        return json_resp
예제 #2
0
    def get_user(self, email):
        users = Cache.get_data('get_users', default=[])

        for user in users:
            if user['email'] == email:
                return user

        user = self.get(url='user/{}'.format(email))
        users.append(user)

        Cache.set_data('get_users', users, is_unique=True)

        return user
예제 #3
0
        def decorated(*args, **kwargs):
            if current_app.config['TESTING']:
                if 'db_call' in dkwargs:
                    data = dkwargs['db_call'](*args, **kwargs)
                else:
                    data = f(*args, **kwargs)
                return data

            data = None
            if 'from_cache' in dkwargs:
                for cache_name in dkwargs['from_cache'].split(','):
                    data_cache = Cache.get_data(cache_name)
                    if data_cache:
                        if len(args) == 2:
                            data = [
                                d for d in data_cache
                                if d[dkwargs['key']] == str(args[1])
                            ]
                            if len(data) > 0:
                                data = data[0]
                                break
                        else:
                            current_app.logger.info("from_cache not 2 args")
            else:
                data = Cache.get_data(f.__name__)

                if data and dkwargs.get('update_daily'):
                    updated_on = Cache.get_updated_on(f.__name__)
                    kwargs['func'] = f
                    if 'decorator' in dkwargs:
                        kwargs['decorator'] = dkwargs['decorator']
                    if 'sort_by' in dkwargs:
                        kwargs['sort_by'] = dkwargs['sort_by']
                    if (datetime.utcnow() - updated_on
                        ).seconds > 60 * 60 * 24:  # update pages once a day
                        update_cache(*args, **kwargs)

            if not data:
                if 'db_call' in dkwargs:
                    data = dkwargs['db_call'](*args, **kwargs)
                else:
                    data = f(*args, **kwargs)
                if 'from_cache' not in dkwargs:
                    Cache.set_data(f.__name__, data)
            return data
예제 #4
0
def update_cache(*args, **kwargs):
    func = kwargs.pop('func')
    cache___name__ = func.__name__.replace("_from_db", "")
    cached_data = Cache.get_data(cache___name__)
    if 'decorator' in kwargs:
        func = kwargs['decorator'](func)

    data = func(*args, **kwargs)

    review_data = Cache.get_review_entities(func.__name__)

    if review_data and 'sort_by' in kwargs:
        sort_by = kwargs.pop('sort_by')
        data = sort_by(data.extend(review_data))

    if cached_data != data:
        current_app.logger.info('Cache updated from db')
        Cache.set_data(cache___name__, data)
    else:
        current_app.logger.info('Cache does not need updating for {}'.format(
            func.__name__))

    Cache.purge_older_versions(func.__name__)