Logo Search packages:      
Sourcecode: zope-ldapuserfolder version File versions

SimpleCache.py

#####################################################################
#
# SimpleCache     A simple non-persistent cache
#
# This software is governed by a license. See
# LICENSE.txt for the terms of this license.
#
#####################################################################

import time

00012 class SimpleCache:
    """ A simple non-persistent cache """

00015     def __init__(self):
        """ Initialize a new instance """
        self.cache = {}
        self.timeout = 600


00021     def set(self, id, object):
        """ Cache an object under the given id """
        id = id.lower()
        self.cache[id] = object


00027     def get(self, id, password=None):
        """ Retrieve a cached object if it is valid """
        try:
            id = id.lower()
        except AttributeError:
            return None

        user = self.cache.get(id, None)

        if ( password is not None and 
             user is not None and 
             password != user._getPassword() ):
            user = None

        if ( user and 
             (time.time() < user.getCreationTime().timeTime() + self.timeout) ):
            return user
        else:
            return None


00048     def getCache(self):
        """ Get valid cache records """
        valid = []
        cached = self.cache.values()
        now = time.time()

        for object in cached:
            created = object.getCreationTime().timeTime()
            if object and now < (created + self.timeout):
                valid.append(object)

        return valid


00062     def remove(self, id):
        """ Purge a record out of the cache """
        id = id.lower()

        if self.cache.has_key(id):
            del self.cache[id]


00070     def clear(self):
        """ Clear the internal caches """
        self.cache = {}


00075     def setTimeout(self, timeout):
        """ Set the timeout (in seconds) for cached entries """
        self.timeout = timeout



Generated by  Doxygen 1.6.0   Back to index