/usr/share/pyshared/trytond/model/cacheable.py is in tryton-server 2.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from threading import Lock
import time
from trytond.transaction import Transaction
from trytond.cache import Cache
class Cacheable(object):
_name = None
def __init__(self):
super(Cacheable, self).__init__()
self._cache = {}
self._max_len = 1024
self._timeout = 3600
self._lock = Lock()
self._timestamp = None
Cache._cache_instance.append(self)
def add(self, key, value):
dbname = Transaction().cursor.dbname
self._lock.acquire()
try:
self._cache.setdefault(dbname, {})
lower = None
if len(self._cache[dbname]) > self._max_len:
mintime = time.time() - self._timeout
for key2 in self._cache[dbname].keys():
last_time = self._cache[dbname][key2][1]
if mintime > last_time:
del self._cache[dbname][key2]
else:
if not lower or lower[1] > last_time:
lower = (key2, last_time)
if len(self._cache[dbname]) > self._max_len and lower:
del self._cache[dbname][lower[0]]
self._cache[dbname][key] = (value, time.time())
finally:
self._lock.release()
def invalidate(self, key):
dbname = Transaction().cursor.dbname
self._lock.acquire()
try:
del self._cache[dbname][key]
finally:
self._lock.release()
def get(self, key):
dbname = Transaction().cursor.dbname
try:
return self._cache[dbname][key][0]
except KeyError:
return None
def clear(self):
dbname = Transaction().cursor.dbname
self._lock.acquire()
try:
self._cache.setdefault(dbname, {})
self._cache[dbname].clear()
Cache.reset(dbname, self._name)
finally:
self._lock.release()
|