remove logging, add config-settings for caching, add docs
This commit is contained in:
parent
03324d5944
commit
78f160bf0b
6 changed files with 47 additions and 96 deletions
48
model.py
48
model.py
|
@ -7,19 +7,25 @@ from trytond.model import MultiValueMixin, ValueMixin, fields, Unique, Model
|
|||
from trytond.transaction import Transaction
|
||||
from trytond.pool import Pool
|
||||
from trytond.cache import MemoryCache
|
||||
from trytond.config import config
|
||||
from datetime import timedelta, datetime
|
||||
from decimal import Decimal
|
||||
from sql import With, Literal
|
||||
from sql.functions import Function
|
||||
from sql.conditionals import Coalesce
|
||||
import copy, logging
|
||||
import copy
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
if config.get('cashbook', 'memcache', default='yes').lower() in ['yes', '1', 'true']:
|
||||
ENABLE_CACHE = True
|
||||
else:
|
||||
ENABLE_CACHE = False
|
||||
|
||||
if config.get('cashbook', 'sync', default='yes').lower() in ['yes', '1', 'true']:
|
||||
ENABLE_CACHESYNC = True
|
||||
else:
|
||||
ENABLE_CACHESYNC = False
|
||||
|
||||
ENABLE_CACHE = True
|
||||
CACHEKEY_CURRENCY = 'currency-%s'
|
||||
CACHEKEY_CASHBOOK = 'cashbook-%s'
|
||||
|
||||
|
||||
class ArrayAgg(Function):
|
||||
|
@ -105,14 +111,8 @@ class MemCache(Model):
|
|||
for x in values.keys()
|
||||
if record.id in values[x].keys()}
|
||||
cls._cashbook_value_cache.set(cache_keys[record.id], copy.deepcopy(data))
|
||||
# ~ logger.info('memcache-VALUE-SET %(time)s key=%(key)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ 'key': cache_keys[record.id],
|
||||
# ~ })
|
||||
cls._cashbook_value_cache.sync(Transaction())
|
||||
# ~ logger.info('memcache-VALUE-SYNC %(time)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ })
|
||||
if ENABLE_CACHESYNC == True:
|
||||
cls._cashbook_value_cache.sync(Transaction())
|
||||
|
||||
@classmethod
|
||||
def store_value(cls, cache_key, values):
|
||||
|
@ -121,10 +121,6 @@ class MemCache(Model):
|
|||
if ENABLE_CACHE == False:
|
||||
return
|
||||
cls._cashbook_value_cache.set(cache_key, copy.deepcopy(values))
|
||||
# ~ logger.info('memcache-VALUE-SET %(time)s key=%(key)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ 'key': cache_key,
|
||||
# ~ })
|
||||
|
||||
@classmethod
|
||||
def read_from_cache(cls, records, cache_keys, names, result):
|
||||
|
@ -145,15 +141,7 @@ class MemCache(Model):
|
|||
if result[name][record.id] is None:
|
||||
result[name][record.id] = Decimal('0.0')
|
||||
result[name][record.id] += values[name]
|
||||
# ~ logger.info('memcache-VALUE-READ-HIT %(time)s key=%(key)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ 'key': cache_keys[record.id],
|
||||
# ~ })
|
||||
else :
|
||||
# ~ logger.info('memcache-VALUE-READ-FAIL %(time)s key=%(key)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ 'key': cache_keys[record.id],
|
||||
# ~ })
|
||||
todo_records.append(record)
|
||||
return (todo_records, result)
|
||||
|
||||
|
@ -167,11 +155,6 @@ class MemCache(Model):
|
|||
if ENABLE_CACHE == False:
|
||||
return '-'
|
||||
|
||||
# ~ logger.info('memcache-KEY %(time)s name=%(name)s record=%(record)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ 'name': name,
|
||||
# ~ 'record': str(record),
|
||||
# ~ })
|
||||
fname = [name, str(record.id)]
|
||||
fname.extend(addkeys)
|
||||
|
||||
|
@ -215,11 +198,6 @@ class MemCache(Model):
|
|||
|
||||
if 'cachekey' in line.keys():
|
||||
key = cls.store_value(line['cachekey'], fname[-1])
|
||||
# ~ logger.info('memcache-KEY-RESULT %(time)s name=%(name)s result=%(result)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ 'name': name,
|
||||
# ~ 'result': '-'.join(fname),
|
||||
# ~ })
|
||||
return '-'.join(fname)
|
||||
|
||||
@classmethod
|
||||
|
@ -238,6 +216,8 @@ class MemCache(Model):
|
|||
def record_update(cls, cache_key, record):
|
||||
""" update cache-value
|
||||
"""
|
||||
if ENABLE_CACHE == False:
|
||||
return
|
||||
cls.store_value(cache_key,
|
||||
cls.genkey(record.id, record.write_date, record.create_date)
|
||||
if record is not None else None)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue