Пример #1
0
    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()
Пример #2
0
    def __init__(self, _, params):
        BaseCache.__init__(self, params)
        self._cache = {}
        self._expire_info = {}

        max_entries = params.get('max_entries', 300)
        try:
            self._max_entries = int(max_entries)
        except (ValueError, TypeError):
            self._max_entries = 300

        cull_frequency = params.get('cull_frequency', 3)
        try:
            self._cull_frequency = int(cull_frequency)
        except (ValueError, TypeError):
            self._cull_frequency = 3

        self._lock = RWLock()
Пример #3
0
    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()
Пример #4
0
    def __init__(self, _, params):
        BaseCache.__init__(self, params)
        self._cache = {}
        self._expire_info = {}

        max_entries = params.get('max_entries', 300)
        try:
            self._max_entries = int(max_entries)
        except (ValueError, TypeError):
            self._max_entries = 300

        cull_frequency = params.get('cull_frequency', 3)
        try:
            self._cull_frequency = int(cull_frequency)
        except (ValueError, TypeError):
            self._cull_frequency = 3

        self._lock = RWLock()
Пример #5
0
    def _set_model(self, model):
        cache_name = '.'.join((
            model._meta.app_label,
            model._meta.model_name,
            self.name,
        ))
        self._cache = CACHES.setdefault(cache_name, OrderedDict())
        self._info = CACHE_INFO.setdefault(cache_name, {'expire_time': 0})
        self._lock = LOCKS.setdefault(cache_name, RWLock())

        self._indexes = {}
        for field_name in self.indexed_fields:
            cache_name = '.'.join((
                model._meta.app_label,
                model._meta.model_name,
                self.name,
                field_name,
            ))
            self._indexes[field_name] = CACHES.setdefault(cache_name, {})

        self._model = model
        if not model._meta.abstract and not model._meta.swapped:
            post_save.connect(self._model_changed, sender=model)
            post_delete.connect(self._model_changed, sender=model)
Пример #6
0
 def __init__(self, host, params):
     SimpleCacheClass.__init__(self, host, params)
     self._lock = RWLock()
Пример #7
0
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()
Пример #8
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()
Пример #9
0
 def __init__(self, _, params):
     BaseStorage.__init__(self, params)
     self._db = {}
     self._lock = RWLock()
Пример #10
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()
Пример #11
0
from django.utils.synch import RWLock
import threading

lock = RWLock()


def r():
    """读进的时候只会被写给等待"""
    with lock.reader():
        import time
        for i in range(0, 3):
            print(i)
            time.sleep(i * 4)


def w():
    with lock.writer():
        import time
        for i in range(0, 3):
            print(i)
            time.sleep(i * 4)


class myThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
Пример #12
0
 def __init__(self, host, params):
     _SimpleCache.__init__(self, host, params)
     self._lock = RWLock()
Пример #13
0
 def __init__(self, _, params, key_prefix='', version=1, key_func=None):
     BaseCache.__init__(self, params, key_prefix, version, key_func)
     self._cache = {}
     self._expire_info = {}
     self._lock = RWLock()
Пример #14
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)
Пример #15
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()
Пример #16
0
 def __init__(self, _, params):
     BaseCache.__init__(self, params)
     self._cache = {}
     self._expire_info = {}
     self._lock = RWLock()
Пример #17
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)