book/line: logging, model: cache-write skip existing values
This commit is contained in:
parent
179543bcf0
commit
03324d5944
4 changed files with 81 additions and 35 deletions
44
model.py
44
model.py
|
@ -7,12 +7,14 @@ 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 datetime import timedelta
|
||||
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
|
||||
import copy, logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
ENABLE_CACHE = True
|
||||
|
@ -91,19 +93,26 @@ class MemCache(Model):
|
|||
return copy.deepcopy(cls._cashbook_value_cache.get(cache_key))
|
||||
|
||||
@classmethod
|
||||
def store_result(cls, records, cache_keys, values):
|
||||
def store_result(cls, records, cache_keys, values, skip_records=[]):
|
||||
""" store result to cache
|
||||
"""
|
||||
if ENABLE_CACHE == False:
|
||||
return
|
||||
for record in records:
|
||||
if record not in skip_records:
|
||||
continue
|
||||
data = {x:values[x][record.id]
|
||||
for x in values.keys()
|
||||
if record.id in values[x].keys()}
|
||||
cls._cashbook_value_cache.set(cache_keys[record.id], copy.deepcopy(data))
|
||||
print('\n## SYNC-GO-1')
|
||||
# ~ 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())
|
||||
print('-- SYNC-FERTIG-1')
|
||||
# ~ logger.info('memcache-VALUE-SYNC %(time)s' % {
|
||||
# ~ 'time': datetime.now().isoformat(),
|
||||
# ~ })
|
||||
|
||||
@classmethod
|
||||
def store_value(cls, cache_key, values):
|
||||
|
@ -112,9 +121,10 @@ class MemCache(Model):
|
|||
if ENABLE_CACHE == False:
|
||||
return
|
||||
cls._cashbook_value_cache.set(cache_key, copy.deepcopy(values))
|
||||
print('\n## SYNC-GO-2')
|
||||
cls._cashbook_value_cache.sync(Transaction())
|
||||
print('-- SYNC-FERTIG-2')
|
||||
# ~ 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):
|
||||
|
@ -135,7 +145,15 @@ 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)
|
||||
|
||||
|
@ -149,6 +167,11 @@ 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)
|
||||
|
||||
|
@ -192,6 +215,11 @@ 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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue