Author rhettinger
Recipients jcea, rhettinger, serhiy.storchaka, steven.daprano
Date 2017-04-25.02:44:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1493088274.61.0.392161865929.issue30153@psf.upfronthosting.co.za>
In-reply-to
Content
If you're interested, here is a starting point for experimenting with any variations you want (invalidate a specific entry, changeable maxsize, pickle/unpickle, expiration of entries after a specific time, inspection of the internal contents, ability to inject known values, testing whether a specific argument tuple is in the cache, logging of cache access, or just about anything you could do with a regular dictionary):

    class LRU(OrderedDict):

        def __init__(self, func, maxsize=128):
            self.maxsize = 128
            self.func = func

        def __call__(self, *args):
            if args in self:
                value = self[args]
                self.move_to_end(args)
                return value
            value = self.func(*args)
            if len(self) >= self.maxsize:
                self.popitem(False)
            self[args] = value
            return value
History
Date User Action Args
2017-04-25 02:44:34rhettingersetrecipients: + rhettinger, jcea, steven.daprano, serhiy.storchaka
2017-04-25 02:44:34rhettingersetmessageid: <1493088274.61.0.392161865929.issue30153@psf.upfronthosting.co.za>
2017-04-25 02:44:34rhettingerlinkissue30153 messages
2017-04-25 02:44:34rhettingercreate