book: add cache for asset-rates

This commit is contained in:
Frederik Jaeckel 2023-02-27 10:20:23 +01:00
parent 009f44816e
commit 2b9a2ba07a
3 changed files with 53 additions and 0 deletions

View file

@ -10,10 +10,12 @@ from .reconciliation import Reconciliation
from .line import Line from .line import Line
from .splitline import SplitLine from .splitline import SplitLine
from .assetsetting import AssetSetting from .assetsetting import AssetSetting
from .asset import AssetRate
def register(): def register():
Pool.register( Pool.register(
AssetRate,
Type, Type,
Book, Book,
Line, Line,

48
asset.py Normal file
View file

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# This file is part of the cashbook-module from m-ds.de for Tryton.
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from trytond.pool import Pool, PoolMeta
CACHEKEY_ASSETRATE = 'assetrate-%s'
class AssetRate(metaclass=PoolMeta):
__name__ = 'investment.rate'
@classmethod
def create(cls, vlist):
""" update cache-value
"""
MemCache = Pool().get('cashbook.memcache')
records = super(AssetRate, cls).create(vlist)
for rate in records:
MemCache.record_update(CACHEKEY_ASSETRATE % rate.asset.id, rate)
return records
@classmethod
def write(cls, *args):
""" update cache-value
"""
MemCache = Pool().get('cashbook.memcache')
super(AssetRate, cls).write(*args)
actions = iter(args)
for rates, values in zip(actions, actions):
for rate in rates:
MemCache.record_update(CACHEKEY_ASSETRATE % rate.asset.id, rate)
@classmethod
def delete(cls, records):
""" set cache to None
"""
MemCache = Pool().get('cashbook.memcache')
for record in records:
MemCache.record_update(CACHEKEY_ASSETRATE % record.asset.id, None)
super(AssetRate, cls).delete(records)
# end

View file

@ -16,6 +16,7 @@ from sql.functions import CurrentDate
from sql.aggregate import Sum from sql.aggregate import Sum
from sql.conditionals import Case, Coalesce from sql.conditionals import Case, Coalesce
from trytond.modules.cashbook.model import CACHEKEY_CURRENCY from trytond.modules.cashbook.model import CACHEKEY_CURRENCY
from .asset import CACHEKEY_ASSETRATE
class Book(SymbolMixin, metaclass=PoolMeta): class Book(SymbolMixin, metaclass=PoolMeta):
@ -288,6 +289,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
}, { }, {
'model': 'investment.rate', 'model': 'investment.rate',
'query': [('asset.id', '=', x.asset.id)], 'query': [('asset.id', '=', x.asset.id)],
'cachekey': CACHEKEY_ASSETRATE % x.asset.id,
} if x.asset is not None else {}], } if x.asset is not None else {}],
addkeys = [query_date.isoformat()]) addkeys = [query_date.isoformat()])
for x in cashbooks for x in cashbooks
@ -426,6 +428,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
}, { }, {
'model': 'investment.rate', 'model': 'investment.rate',
'query': [('asset.id', '=', x.asset.id)], 'query': [('asset.id', '=', x.asset.id)],
'cachekey': CACHEKEY_ASSETRATE % x.asset.id,
} if x.asset is not None else {}], } if x.asset is not None else {}],
addkeys=[ addkeys=[
str(company_currency), str(company_currency),