How to use the cachier.mongo_core.RecalculationNeeded function in cachier

To help you get started, we’ve selected a few cachier examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github shaypal5 / cachier / tests / test_mongo_core.py View on Github external
def test_stalled_mongo_db_cache():
    @cachier(mongetter=_test_mongetter)
    def _stalled_func():
        return 1
    core = _MongoCore(_test_mongetter, None, False)
    core.set_func(_stalled_func)
    core.clear_cache()
    with pytest.raises(RecalculationNeeded):
        core.wait_on_entry_calc(key=None)
github shaypal5 / cachier / cachier / core.py View on Github external
if entry is not None:  # pylint: disable=R0101
                _print('Entry found.')
                if entry.get('value', None) is not None:
                    _print('Cached result found.')
                    if stale_after:
                        now = datetime.datetime.now()
                        if now - entry['time'] > stale_after:
                            _print('But it is stale... :(')
                            if entry['being_calculated']:
                                if next_time:
                                    _print('Returning stale.')
                                    return entry['value']  # return stale val
                                _print('Already calc. Waiting on change.')
                                try:
                                    return core.wait_on_entry_calc(key)
                                except RecalculationNeeded:
                                    return _calc_entry(core, key, func, args, kwds)
                            if next_time:
                                _print('Async calc and return stale')
                                try:
                                    core.mark_entry_being_calculated(key)
                                    _get_executor().submit(
                                        _function_thread, core, key, func,
                                        args, kwds)
                                finally:
                                    core.mark_entry_not_calculated(key)
                                return entry['value']
                            _print('Calling decorated function and waiting')
                            return _calc_entry(core, key, func, args, kwds)
                    _print('And it is fresh!')
                    return entry['value']
                if entry['being_calculated']:
github shaypal5 / cachier / cachier / mongo_core.py View on Github external
def wait_on_entry_calc(self, key):
        while True:
            time.sleep(MONGO_SLEEP_DURATION_IN_SEC)
            key, entry = self.get_entry_by_key(key)
            if entry is None:
                raise RecalculationNeeded()
            if entry is not None and not entry['being_calculated']:
                return entry['value']