Beispiel #1
0
class _LocMemCache(_SimpleCache):
    "Thread-safe in-memory cache."

    def __init__(self, host, params):
        _SimpleCache.__init__(self, host, params)
        self._lock = RWLock()

    def get(self, key, default=None):
        should_delete = False
        self._lock.reader_enters()
        try:
            now = time.time()
            exp = self._expire_info.get(key)
            if exp is None:
                return default
            elif exp < now:
                should_delete = True
            else:
                return copy.deepcopy(self._cache[key])
        finally:
            self._lock.reader_leaves()
        if should_delete:
            self._lock.writer_enters()
            try:
                del self._cache[key]
                del self._expire_info[key]
                return default
            finally:
                self._lock.writer_leaves()

    def set(self, key, value, timeout=None):
        self._lock.writer_enters()
        try:
            _SimpleCache.set(self, key, value, timeout)
        finally:
            self._lock.writer_leaves()

    def delete(self, key):
        self._lock.writer_enters()
        try:
            _SimpleCache.delete(self, key)
        finally:
            self._lock.writer_leaves()
class CacheClass(SimpleCacheClass):
    def __init__(self, host, params):
        SimpleCacheClass.__init__(self, host, params)
        self._lock = RWLock()

    def get(self, key, default=None):
        should_delete = False
        self._lock.reader_enters()
        try:
            now = time.time()
            exp = self._expire_info.get(key)
            if exp is None:
                return default
            elif exp < now:
                should_delete = True
            else:
                return copy.deepcopy(self._cache[key])
        finally:
            self._lock.reader_leaves()
        if should_delete:
            self._lock.writer_enters()
            try:
                del self._cache[key]
                del self._expire_info[key]
                return default
            finally:
                self._lock.writer_leaves()

    def set(self, key, value, timeout=None):
        self._lock.writer_enters()
        try:
            SimpleCacheClass.set(self, key, value, timeout)
        finally:
            self._lock.writer_leaves()

    def delete(self, key):
        self._lock.writer_enters()
        try:
            SimpleCacheClass.delete(self, key)
        finally:
            self._lock.writer_leaves()
Beispiel #3
0
class StorageClass(BaseStorage):
    def __init__(self, _, params):
        BaseStorage.__init__(self, params)
        self._db = {}
        self._lock = RWLock()

    def set(self, key, value):
        self._lock.writer_enters()
        try:
            self._db[key] = pickle.dumps(value)
        finally:
            self._lock.writer_leaves()

    def get(self, key):
        self._lock.reader_enters()
        # Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
        try:
            try:
                return pickle.loads(self._db[key])
            except KeyError:
                return None
        finally:
            self._lock.reader_leaves()

    def delete(self, key):
        self._lock.write_enters()
        # Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
        try:
            try:
                del self._db[key]
            except KeyError:
                pass
        finally:
            self._lock.writer_leaves()

    def has_key(self, key):
        self._lock.reader_enters()
        try:
            return key in self._db
        finally:
            self._lcok.reader_leaves()
Beispiel #4
0
class StorageClass(BaseStorage):
    def __init__(self, _, params):
        BaseStorage.__init__(self, params)
        self._db = {}
        self._lock = RWLock()

    def set(self, key, value):
        self._lock.writer_enters()
        try:
            self._db[key] = pickle.dumps(value)
        finally:
            self._lock.writer_leaves()

    def get(self, key):
        self._lock.reader_enters()
        # Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
        try:
            try:
                return pickle.loads(self._db[key])
            except KeyError:
                return None
        finally:
            self._lock.reader_leaves()

    def delete(self, key):
        self._lock.write_enters()
        # Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
        try:
            try:
                del self._db[key]
            except KeyError:
                pass
        finally:
            self._lock.writer_leaves()

    def has_key(self, key):
        self._lock.reader_enters()
        try:
            return key in self._db
        finally:
            self._lcok.reader_leaves()
Beispiel #5
0
class CacheClass(BaseCache):
    def __init__(self, _, params):
        BaseCache.__init__(self, params)
        self._cache = {}
        self._expire_info = {}
        self._lock = RWLock()

    def add(self, key, value, timeout=None):
        self.validate_key(key)
        self._lock.writer_enters()
        try:
            exp = self._expire_info.get(key)
            if exp is None or exp <= time.time():
                try:
                    self._set(key, pickle.dumps(value), timeout)
                    return True
                except pickle.PickleError:
                    pass
            return False
        finally:
            self._lock.writer_leaves()

    def get(self, key, default=None):
        self.validate_key(key)
        self._lock.reader_enters()
        try:
            exp = self._expire_info.get(key)
            if exp is None:
                return default
            elif exp > time.time():
                try:
                    return pickle.loads(self._cache[key])
                except pickle.PickleError:
                    return default
        finally:
            self._lock.reader_leaves()
        self._lock.writer_enters()
        try:
            try:
                del self._cache[key]
                del self._expire_info[key]
            except KeyError:
                pass
            return default
        finally:
            self._lock.writer_leaves()

    def _set(self, key, value, timeout=None):
        if len(self._cache) >= self._max_entries:
            self._cull()
        if timeout is None:
            timeout = self.default_timeout
        self._cache[key] = value
        self._expire_info[key] = time.time() + timeout

    def set(self, key, value, timeout=None):
        self.validate_key(key)
        self._lock.writer_enters()
        # Python 2.4 doesn't allow combined try-except-finally blocks.
        try:
            try:
                self._set(key, pickle.dumps(value), timeout)
            except pickle.PickleError:
                pass
        finally:
            self._lock.writer_leaves()

    def has_key(self, key):
        self.validate_key(key)
        self._lock.reader_enters()
        try:
            exp = self._expire_info.get(key)
            if exp is None:
                return False
            elif exp > time.time():
                return True
        finally:
            self._lock.reader_leaves()

        self._lock.writer_enters()
        try:
            try:
                del self._cache[key]
                del self._expire_info[key]
            except KeyError:
                pass
            return False
        finally:
            self._lock.writer_leaves()

    def _cull(self):
        if self._cull_frequency == 0:
            self.clear()
        else:
            doomed = [k for (i, k) in enumerate(self._cache) if i % self._cull_frequency == 0]
            for k in doomed:
                self._delete(k)

    def _delete(self, key):
        try:
            del self._cache[key]
        except KeyError:
            pass
        try:
            del self._expire_info[key]
        except KeyError:
            pass

    def delete(self, key):
        self.validate_key(key)
        self._lock.writer_enters()
        try:
            self._delete(key)
        finally:
            self._lock.writer_leaves()

    def clear(self):
        self._cache.clear()
        self._expire_info.clear()
class CacheClass(BaseCache):
    def __init__(self, _, params):
        BaseCache.__init__(self, params)
        self._cache = {}
        self._expire_info = {}
        self._lock = RWLock()

    def add(self, key, value, timeout=None):
        self.validate_key(key)
        self._lock.writer_enters()
        try:
            exp = self._expire_info.get(key)
            if exp is None or exp <= time.time():
                try:
                    self._set(key, pickle.dumps(value), timeout)
                    return True
                except pickle.PickleError:
                    pass
            return False
        finally:
            self._lock.writer_leaves()

    def get(self, key, default=None):
        self.validate_key(key)
        self._lock.reader_enters()
        try:
            exp = self._expire_info.get(key)
            if exp is None:
                return default
            elif exp > time.time():
                try:
                    return pickle.loads(self._cache[key])
                except pickle.PickleError:
                    return default
        finally:
            self._lock.reader_leaves()
        self._lock.writer_enters()
        try:
            try:
                del self._cache[key]
                del self._expire_info[key]
            except KeyError:
                pass
            return default
        finally:
            self._lock.writer_leaves()

    def _set(self, key, value, timeout=None):
        if len(self._cache) >= self._max_entries:
            self._cull()
        if timeout is None:
            timeout = self.default_timeout
        self._cache[key] = value
        self._expire_info[key] = time.time() + timeout

    def set(self, key, value, timeout=None):
        self.validate_key(key)
        self._lock.writer_enters()
        # Python 2.4 doesn't allow combined try-except-finally blocks.
        try:
            try:
                self._set(key, pickle.dumps(value), timeout)
            except pickle.PickleError:
                pass
        finally:
            self._lock.writer_leaves()

    def has_key(self, key):
        self.validate_key(key)
        self._lock.reader_enters()
        try:
            exp = self._expire_info.get(key)
            if exp is None:
                return False
            elif exp > time.time():
                return True
        finally:
            self._lock.reader_leaves()

        self._lock.writer_enters()
        try:
            try:
                del self._cache[key]
                del self._expire_info[key]
            except KeyError:
                pass
            return False
        finally:
            self._lock.writer_leaves()

    def _cull(self):
        if self._cull_frequency == 0:
            self.clear()
        else:
            doomed = [
                k for (i, k) in enumerate(self._cache)
                if i % self._cull_frequency == 0
            ]
            for k in doomed:
                self._delete(k)

    def _delete(self, key):
        try:
            del self._cache[key]
        except KeyError:
            pass
        try:
            del self._expire_info[key]
        except KeyError:
            pass

    def delete(self, key):
        self.validate_key(key)
        self._lock.writer_enters()
        try:
            self._delete(key)
        finally:
            self._lock.writer_leaves()

    def clear(self):
        self._cache.clear()
        self._expire_info.clear()
Beispiel #7
0
class LocLRUCache(BaseCache):
    """
    Django LRU Threading-safe locmem cache
    """
    def __init__(self, _, params):
        BaseCache.__init__(self, params)
        self._params = params
        self._cache = {}  # entry:(val,expire_time)
        try:
            self._max_entries = int(params.get('max_entries'))
        except:
            self._max_entries = MAX_KEYS

        self._call_seq = {}

        self._call_list = []
        self._lock = RWLock()

    def _lru_purge(self):
        if self._cached_num > self._max_entries:
            # always 1 more entry, therefor we just pop one
            key, val = self._call_seq.popitem()
            self.delete(key)

    def add(self, key, val, timeout=3600):
        if not self.has_key(key):
            self.set(key, val, timeout)

    def set(self, key, val, timeout=3600):
        self._lock.writer_enters()
        try:
            self._cache[key] = (val, time.time() + timeout)
            self._cached_num = len(self._cache)
            self._refresh(key)
            self._lock.writer_leaves()
            self._lru_purge()
        except TypeError:
            pass

    def _refresh(self, key):
        try:
            del self._call_seq[key]
        except:
            pass
        try:
            self._call_seq.update({key: None})
        except:
            pass

    def get(self, key, default=None):

        self._lock.reader_enters()
        try:
            val, exp_time = self._cache.get(key, (default, 0))
            self._lock.reader_leaves()
            if exp_time < time.time():
                self.delete(key)
                val = default
            else:  # still valided
                self._refresh(key)
        except:
            pass
        finally:
            return val

    def delete(self, key):

        self._lock.writer_enters()
        try:
            del self._cache[key]
        except KeyError:
            pass

        try:
            del self._call_seq[key]
        except:
            pass
        self._cached_num = len(self._cache)
        self._lock.writer_leaves()

    def has_key(self, key):
        return self._cache.has_key(key)

    def clear(self):
        [self.delete(key) for key, val in self._cache.iteritems()]

    def __str__(self):
        return u"LRU Cache:{0._params} cached={0._cached_num}".format(self)
Beispiel #8
0
class LocLRUCache(BaseCache):
    """
    Django LRU Threading-safe locmem cache
    """
    def __init__(self, _, params):
        BaseCache.__init__(self, params)
        self._params = params
        self._cache = {}  # entry:(val,expire_time)
        try:
            self._max_entries = int(params.get('max_entries'))
        except:
            self._max_entries = MAX_KEYS

        self._call_seq = {}

        self._call_list = []
        self._lock = RWLock()

    def _lru_purge(self):
        if self._cached_num > self._max_entries:
            # always 1 more entry, therefor we just pop one
            key, val = self._call_seq.popitem()
            self.delete(key)

    def add(self, key, val, timeout=3600):
        if not self.has_key(key):
            self.set(key, val, timeout)

    def set(self, key, val, timeout=3600):
        self._lock.writer_enters()
        try:
            self._cache[key] = (val, time.time() + timeout)
            self._cached_num = len(self._cache)
            self._refresh(key)
            self._lock.writer_leaves()
            self._lru_purge()
        except TypeError:
            pass

    def _refresh(self, key):
        try:
            del self._call_seq[key]
        except:
            pass
        try:
            self._call_seq.update({key: None})
        except:
            pass

    def get(self, key, default=None):

        self._lock.reader_enters()
        try:
            val, exp_time = self._cache.get(key, (default, 0))
            self._lock.reader_leaves()
            if exp_time < time.time():
                self.delete(key)
                val = default
            else:  # still valided
                self._refresh(key)
        except:
            pass
        finally:
            return val

    def delete(self, key):

        self._lock.writer_enters()
        try:
            del self._cache[key]
        except KeyError:
            pass

        try:
            del self._call_seq[key]
        except:
            pass
        self._cached_num = len(self._cache)
        self._lock.writer_leaves()

    def has_key(self, key):
        return self._cache.has_key(key)

    def clear(self):
        [self.delete(key) for key, val in self._cache.iteritems()]

    def __str__(self):
        return u"LRU Cache:{0._params} cached={0._cached_num}".format(self)