How to use the ring.func.lru_cache.LruCache function in ring

To help you get started, we’ve selected a few ring 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 youknowone / ring / tests / _test_func_asyncio.py View on Github external
def storage_lru():
    return LruCache(128), ring.lru
github youknowone / ring / tests / test_lru_cache.py View on Github external
def test_lru_object():
    lru = LruCache(3)

    lru.set('a', 10)
    lru.set('b', 20)
    lru.set('c', 30)
    assert 10 == lru.get('a')
    lru.set('d', 40)
    assert SENTINEL is lru.get('b')
    assert 30 == lru.get('c')

    lru.delete('c')
    assert 10 == lru.get('a')
    assert SENTINEL is lru.get('b')
    assert SENTINEL is lru.get('c')
    assert 40 == lru.get('d')

    lru.clear()
github youknowone / ring / tests / test_lru_cache.py View on Github external
def test_overflow_after_clear():
    lru = LruCache(1)

    lru.clear()
    lru.set('a', 10)
    lru.set('b', 20)
    assert lru.get('c') == SENTINEL
github youknowone / ring / tests / test_lru_cache.py View on Github external
def decorating_function(user_function):
            cache = LruCache(maxsize)

            def wrapper(*args, **kwds):
                # Size limited caching that tracks accesses by recency
                key = _make_key(args, kwds, typed)
                result = cache.get(key)
                if result is not SENTINEL:
                    return result
                result = user_function(*args, **kwds)
                cache.set(key, result)
                return result

            return update_wrapper(wrapper, user_function)
github youknowone / ring / tests / test_lru_cache.py View on Github external
def test_expire_object():
    lru = LruCache(3)

    now_mock = MagicMock()
    now_mock.return_value = 0
    lru.now = now_mock

    lru.set('a', 10, expire=1)
    lru.set('b', 20, expire=2)
    lru.set('c', 30, expire=3)  # a - b - c
    # Check if cache works well
    assert lru.get('a') == 10  # b - c - a
    # Check if 'a' key expired
    now_mock.return_value = 1
    assert lru.get('a') == SENTINEL  # b - c
    # Check if lru logic works well
    lru.set('d', 40, expire=4)  # b - c - d
    assert lru.get('b') == 20  # c - d - b
github youknowone / ring / tests / test_func_sync.py View on Github external
def storage_lru():
    storage = LruCache(128)
    storage.ring = ring.lru
    storage.is_binary = False
    storage.has_has = True
    storage.has_touch = True
    storage.has_expire = False
    return storage
github youknowone / ring / ring / func / sync.py View on Github external
>>> def f(...):
        ...     ...

    Note that **ring** basically supports key consistency and manual
    operation features, unlike :func:`functools.lru_cache`.

    :param ring.func.lru_cache.LruCache lru: Cache storage. If the default
        value :data:`None` is given, a new `LruCache`
        object will be created. (Recommended)
    :param int maxsize: The maximum size of the cache storage.

    :see: :func:`functools.lru_cache` for LRU cache basics.
    :see: :func:`ring.func.sync.CacheUserInterface` for sub-functions.
    """
    if lru is None:
        lru = lru_mod.LruCache(maxsize)
        if key_prefix is None:
            key_prefix = ''

    return fbase.factory(
        lru, key_prefix=key_prefix, on_manufactured=None,
        user_interface=user_interface, storage_class=storage_class,
        miss_value=None, expire_default=expire, coder=coder,
        **kwargs)