mailgun / expiringdict

Dictionary with auto-expiring values for caching purposes.
Apache License 2.0
347 stars 76 forks source link

Option to add trigger on delete #50

Open jzr-supove opened 2 years ago

jzr-supove commented 2 years ago

Trigger on delete

Add option to pass custom function that is executed when element being deleted, so that we can process elements before they gone

Example usage:

def process(key, value):
    print(key, value)

cache = ExpiringDict(max_len=2, max_age_seconds=1, on_delete=process)

cache[0] = "A"
cache[1] = "B"
cache[2] = "C"
cache[3] = "D"

Outputs:

0 A
1 B

Hotfix

Fixed issue when key gets deleted even though its updated recently

Example:

cache = ExpiringDict(max_len=2, max_age_seconds=1)

cache['r'] = "Red"
cache['g'] = "Green"
cache['r'] = "LightRed"
cache['b'] = "Blue"

print(cache)

Output before:

ExpiringDict([('g', 'Green'), ('b', 'Blue')])

Output after fix:

ExpiringDict([('r', 'LightRed'), ('b', 'Blue')])