Is there an established memoize on-disk decorator for python?

后端 未结 2 753
借酒劲吻你
借酒劲吻你 2021-02-09 05:43

I have been searching a bit for a python module that offers a memoize decorator with the following capabilities:

  • Stores cache on disk to be reused among subsequent
2条回答
  •  别跟我提以往
    2021-02-09 06:15

    I don't know of any memoize decorator that takes care of all that, but you might want to have a look at ZODB. It's a persistence system built on top of pickle that provides some additional features including being able move objects from memory to disk when they aren't being used and the ability to save only objects that have been modified.

    Edit: As a follow-up for the comment. A memoization decorator isn't supported out of the box by ZODB. However, I think you can:

    • Implement your own persistent class
    • Use a memoization decorator in the methods you need (any standard implementation should work, but it probably needs to be modified to make sure that the dirty bit is set)

    After that, if you create an object of that class and add it to a ZODB database, when you execute one of the memoized methods, the object will be marked as dirty and changes will be saved to the database in the next transaction commit operation.

提交回复
热议问题