cache-like structure

konstantin konstantin.selivanov at gmail.com
Thu Aug 7 12:21:02 CEST 2008


Hi,

I've write a class that actually is data structure with items that
automatically removed from collection when timeout expires. Interface
is pretty simple. First public method is to make a record. And second
(__contains__) is to define if record is actually in table. I prefer
async way to clean out expired records. Thus I have to create timeout
thread every cycle to wait for the next clean-up.

So my questions are:
- is the implementation thread-safe (or I've missed something)
- is there way to avoid creating new timer every cycle
- are there better ways to do this (or any ready recipes)

---
import threading
import time
import collections

class CacheTable(object):
    def __init__(self, ttl):
        self.records = collections.deque()
        self.ttl = ttl
        self.timer = None
        self.mutex = threading.Lock()

    def record(self, item):
        self.mutex.acquire()
        try:
            self.records.append((time.time(), item))
            if not self.timer:
                self.__settimer(self.ttl)
        finally:
            self.mutex.release()

    def __cleanup(self):
        self.mutex.acquire()
        try:
            current = time.time()
            self.timer = None
            while self.records:
                timestamp, item = self.records.popleft()
                if timestamp + self.ttl > current:
                    self.records.appendleft((timestamp, item))
                    time_to_wait = timestamp + self.ttl - current
                    self.__settimer(time_to_wait)
                    break
        finally:
            self.mutex.release()

    def __settimer(self, timeout):
        self.timer = threading.Timer(timeout, self.__cleanup)
        self.timer.start()

    def __contains__(self, value):
        self.mutex.acquire()
        try:
            items = [item for ts, item in self.records]
            return items.__contains__(value)
        finally:
            self.mutex.release()
---

Thanks!



More information about the Python-list mailing list