Skip to content
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

Add support to log a full buffer and TTL expiry to syslog. #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions expiringdict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Expiration happens on any access, object is locked during cleanup from expired
values. Can not store more than max_len elements - the oldest will be deleted.

>>> ExpiringDict(max_len=100, max_age_seconds=10)
>>> ExpiringDict(max_len=100, max_age_seconds=10, logging=False)

The values stored in the following way:
{
Expand All @@ -15,6 +15,7 @@
NOTE: iteration over dict and also keys() do not remove expired values!
'''

import syslog
import time
from threading import RLock

Expand All @@ -26,14 +27,17 @@


class ExpiringDict(OrderedDict):
def __init__(self, max_len, max_age_seconds):
def __init__(self, max_len, max_age_seconds, logging=False):
assert max_age_seconds >= 0
assert max_len >= 1

OrderedDict.__init__(self)
self.max_len = max_len
self.max_age = max_age_seconds
self.lock = RLock()
self.logging = logging
if logging:
self.setup_syslog()

def __contains__(self, key):
""" Return True if the dict has a key, else return False. """
Expand Down Expand Up @@ -62,6 +66,7 @@ def __getitem__(self, key, with_age=False):
else:
return item[0]
else:
self.log_expired(key, item[0])
del self[key]
raise KeyError(key)

Expand All @@ -80,6 +85,7 @@ def pop(self, key, default=None):
with self.lock:
try:
item = OrderedDict.__getitem__(self, key)
self.log_full(key, item[0])
del self[key]
return item[0]
except KeyError:
Expand Down Expand Up @@ -151,3 +157,19 @@ def viewkeys(self):
def viewvalues(self):
""" Return a new view of the dictionary's values. """
raise NotImplementedError()

def setup_syslog(self):
""" Set options for syslog. """
syslog.openlog('expiringdict')

def log_expired(self, key, value):
""" Log that we deleted an expired key. """
if self.logging:
syslog.syslog("Deleting expired entry. "
"Key: %s, value: %s" % (key, value))

def log_full(self, key, value):
""" Log that we deleted the oldest key. """
if self.logging:
syslog.syslog("Deleting entry due to full cache. "
"Key: %s, value: %s" % (key, value))