问题描述:

I'm looking for a dictionary like object in which each key has a ttl (or countdown) that is decremented each time .pop() is called. When the counter hits 0 .pop() actually removes it. Something analogous to the Time-to-Live of network packets, which is measured in hops rather than seconds.

I'm guessing something like this exists, but I don't know the name.

>>> d = X(); d.set('k', 'v', ttl=2); d.set('k2', 'v2', ttl=7)

>>> d.pop('k')

(1, 'v')

>>> d.pop('k')

(0, 'v')

>>> d.pop('k')

...

KeyError: 'k'

>>> d.pop('k2')

(6, 'v2')

What wheel am I reinventing?

相关阅读:
Top