Skip to content

Commit

Permalink
Add method to decorated function to allow precaching of results.
Browse files Browse the repository at this point in the history
  • Loading branch information
lordjabez committed Mar 26, 2023
1 parent d1578e6 commit dffc746
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The Cachier wrapper adds a ``clear_cache()`` function to each wrapped function.
foo.clear_cache()
Genereal Configuration
General Configuration
----------------------

Threads Limit
Expand Down Expand Up @@ -148,6 +148,24 @@ See here for an example:
`Question: How to work with unhashable arguments <https://github.com/python-cachier/cachier/issues/91>`_


Precaching values
---------------------------------

If you want to load a value into the cache without calling the underlying function, this can be done with the `precache_value` function.

.. code-block:: python
@cachier()
def add(arg1, arg2):
return arg1 + arg2
foo.precache_value(2, 2, value_to_cache=5)
result = add(2, 2)
print(result) # prints 5
Per-function call arguments
---------------------------

Expand Down Expand Up @@ -381,5 +399,3 @@ Notable bugfixers:
.. links:
.. _pymongo: https://api.mongodb.com/python/current/
.. _watchdog: https://github.com/gorakhargosh/watchdog


6 changes: 6 additions & 0 deletions cachier/base_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def get_entry(self, args, kwds):
key = self.hash_func(args, kwds)
return self.get_entry_by_key(key)

def precache_value(self, args, kwds, value_to_cache):
"""Writes a precomputed value into the cache."""
key = self.hash_func(args, kwds)
self.set_entry(key, value_to_cache)
return value_to_cache

@abc.abstractmethod
def get_entry_by_key(self, key):
"""Returns the result mapped to the given key in this core's cache,
Expand Down
11 changes: 11 additions & 0 deletions cachier/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,20 @@ def cache_dpath():
except AttributeError:
return None

def precache_value(*args, value_to_cache, **kwds):
"""Add an initial value to the cache.
Arguments
---------
value : any
entry to be written into the cache
"""
return core.precache_value(args, kwds, value_to_cache)

func_wrapper.clear_cache = clear_cache
func_wrapper.clear_being_calculated = clear_being_calculated
func_wrapper.cache_dpath = cache_dpath
func_wrapper.precache_value = precache_value
return func_wrapper

return _cachier_decorator
28 changes: 28 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,31 @@ def _calls_wait_for_calc_timeout_slow(res_queue):
res4 = _wait_for_calc_timeout_slow(1, 2)
# One of the cached values is returned
assert res1 == res4 or res2 == res4 or res3 == res4


@pytest.mark.parametrize(
'mongetter,backend',
[
(_test_mongetter, 'mongo'),
(None, 'memory'),
(None, 'pickle'),
]
)
def test_precache_value(mongetter, backend):

@cachier(backend=backend, mongetter=mongetter)
def func(arg_1, arg_2):
"""Some function."""
return arg_1 + arg_2

result = func.precache_value(2, 2, value_to_cache=5)
assert result == 5
result = func(2, 2)
assert result == 5
func.clear_cache()
result = func(2, 2)
assert result == 4
result = func.precache_value(2, arg_2=2, value_to_cache=5)
assert result == 5
result = func(2, arg_2=2)
assert result == 5

0 comments on commit dffc746

Please sign in to comment.