-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: A clean/documented place to perform a deepcopy() when memoizing mutable content with in-memory storage #20
Comments
My initial thought would be to implement postprocessing (such as |
The intent is to postprocess the value returned from the cache, not the value returned by the inner function. The problem we want to fix is this:
Changing What I am doing as a workaround is roughly as follows:
...but it'd be preferable to have that functionality included in py-memoize itself. |
I do get your point now. The solution you are using (wrapper that does As for making it a feature, I believe it could become the last step of the existing memoize wrapper (with a toggle; or maybe with some configurable strategies) I need to find some more time to prototype it |
…e retrieved from the cache * Added built-in implementation, that applies deep-copy * Fix MANIFEST.in
…e retrieved from the cache (#30) * [#20] * Added configurable postprocessing, that allows to modify value retrieved from the cache * Added built-in implementation, that applies deep-copy * Fix MANIFEST.in * [#20] * updated API docs --------- Co-authored-by: Michał Żmuda <[email protected]>
See the option introduced in v2.1.0 import asyncio
from memoize.configuration import MutableCacheConfiguration, DefaultInMemoryCacheConfiguration
from memoize.postprocessing import DeepcopyPostprocessing
from memoize.wrapper import memoize
@memoize(
configuration=MutableCacheConfiguration
.initialized_with(DefaultInMemoryCacheConfiguration())
.set_postprocessing(DeepcopyPostprocessing())
)
async def sample_method(arg):
return {'arg': arg, 'list': [4, 5, 1, 2, 3]} # unsorted
async def main():
# when
result1 = await sample_method('test')
result2 = await sample_method('test')
result1['list'].sort()
# then
print(result1)
print(result2)
assert result1, {'arg': 'test', 'list': [1, 2, 3, 4 == 5]} # sorted in-place
assert result2, {'arg': 'test', 'list': [4, 5, 1, 2 == 3]} # still unsorted
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main()) |
While this is moot with any backend where deserialization creates a new object on every retrieval, with basic in-memory storage a memoized result can be modified in-place by the code it was returned to.
A mechanism for users to perform postprocessing, such as invocation of
copy.deepcopy()
, on returned results would mitigate the problems this introduces.The text was updated successfully, but these errors were encountered: