remove caching

This commit is contained in:
Frederik Jaeckel 2023-12-23 10:44:55 +01:00
parent 82c390caae
commit fb062243d6
3 changed files with 9 additions and 119 deletions

View file

@ -3,8 +3,7 @@
# The COPYRIGHT file at the top level of this repository contains the # The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms. # full copyright notices and license terms.
from trytond.pool import Pool, PoolMeta from trytond.pool import PoolMeta
CACHEKEY_ASSETRATE = 'assetrate-%s'
class AssetRate(metaclass=PoolMeta): class AssetRate(metaclass=PoolMeta):
@ -14,34 +13,22 @@ class AssetRate(metaclass=PoolMeta):
def create(cls, vlist): def create(cls, vlist):
""" update cache-value """ update cache-value
""" """
MemCache = Pool().get('cashbook.memcache')
records = super(AssetRate, cls).create(vlist) records = super(AssetRate, cls).create(vlist)
for rate in records: # TODO: update cashbooks using this asset
MemCache.record_update(CACHEKEY_ASSETRATE % rate.asset.id, rate)
return records return records
@classmethod @classmethod
def write(cls, *args): def write(cls, *args):
""" update cache-value """ update cache-value
""" """
MemCache = Pool().get('cashbook.memcache')
super(AssetRate, cls).write(*args) super(AssetRate, cls).write(*args)
# TODO: update cashbooks using this asset
actions = iter(args)
for rates, values in zip(actions, actions):
for rate in rates:
MemCache.record_update(CACHEKEY_ASSETRATE % rate.asset.id, rate)
@classmethod @classmethod
def delete(cls, records): def delete(cls, records):
""" set cache to None """ 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) super(AssetRate, cls).delete(records)
# TODO: update cashbooks using this asset
# end # end

View file

@ -5,7 +5,6 @@
from trytond.model import ModelSingleton, ModelView, ModelSQL, fields from trytond.model import ModelSingleton, ModelView, ModelSQL, fields
from trytond.pool import Pool
class AssetSetting(ModelSingleton, ModelSQL, ModelView): class AssetSetting(ModelSingleton, ModelSQL, ModelView):
@ -25,13 +24,4 @@ class AssetSetting(ModelSingleton, ModelSQL, ModelView):
model_name='cashbook.book', ondelete='RESTRICT', model_name='cashbook.book', ondelete='RESTRICT',
help='Profit and loss on sale of assets are recorded in the cash book.') help='Profit and loss on sale of assets are recorded in the cash book.')
@classmethod
def write(cls, *args):
""" clear cache-values
"""
MemCache = Pool().get('cashbook.memcache')
MemCache._cashbook_value_cache.clear_all()
super(AssetSetting, cls).write(*args)
# end AssetSetting # end AssetSetting

97
book.py
View file

@ -9,33 +9,14 @@ from trytond.pyson import Eval, Or, Len, Bool, If
from trytond.modules.cashbook.book import STATES2, DEPENDS2 from trytond.modules.cashbook.book import STATES2, DEPENDS2
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.report import Report from trytond.report import Report
from trytond.config import config
from decimal import Decimal from decimal import Decimal
from datetime import timedelta from datetime import timedelta
from sql import Literal from sql import Literal
from sql.functions import CurrentDate 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 sub_ids_hierarchical,\ from trytond.modules.cashbook.model import (
CACHEKEY_CURRENCY, AnyInArray sub_ids_hierarchical, AnyInArray)
from .asset import CACHEKEY_ASSETRATE
# enable/disable caching of cachekey for 'currency.rate'
if config.get(
'cashbook', 'cache_currency',
default='yes').lower() in ['yes', '1', 'true']:
ENA_CURRKEY = True
else:
ENA_CURRKEY = False
# enable/disable caching of cachekey for 'currency.rate'
if config.get(
'cashbook', 'cache_asset',
default='yes').lower() in ['yes', '1', 'true']:
ENA_ASSETKEY = True
else:
ENA_ASSETKEY = False
class Book(SymbolMixin, metaclass=PoolMeta): class Book(SymbolMixin, metaclass=PoolMeta):
@ -291,7 +272,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
""" """
pool = Pool() pool = Pool()
IrDate = pool.get('ir.date') IrDate = pool.get('ir.date')
MemCache = pool.get('cashbook.memcache')
cursor = Transaction().connection.cursor() cursor = Transaction().connection.cursor()
context = Transaction().context context = Transaction().context
result = { result = {
@ -309,40 +289,13 @@ class Book(SymbolMixin, metaclass=PoolMeta):
).quantize(Decimal(str(1/10 ** digits))) ).quantize(Decimal(str(1/10 ** digits)))
query_date = context.get('date', IrDate.today()) query_date = context.get('date', IrDate.today())
cache_keys = {
x.id: MemCache.get_key_by_record(
name='get_yield_data',
record=x,
query=[{
'model': 'cashbook.line',
'query': [('cashbook.parent', 'child_of', [x.id])],
}, {
'model': 'currency.currency.rate',
'query': [('currency.id', '=', x.currency.id)],
'cachekey' if ENA_CURRKEY else 'disabled':
CACHEKEY_CURRENCY % x.currency.id,
}, {
'model': 'investment.rate',
'query': [('asset.id', '=', x.asset.id)],
'cachekey' if ENA_ASSETKEY else 'disabled':
CACHEKEY_ASSETRATE % x.asset.id,
} if x.asset is not None else {}],
addkeys=[query_date.isoformat()])
for x in cashbooks
}
# read from cache
(todo_cashbook, result) = MemCache.read_from_cache(
cashbooks, cache_keys, names, result)
if len(todo_cashbook) == 0:
return result
# results for 'total' # results for 'total'
records_total = [] records_total = []
records_12m = [] records_12m = []
if len(todo_cashbook) > 0: if cashbooks:
(tab_book1, query_total) = cls.get_yield_data_sql() (tab_book1, query_total) = cls.get_yield_data_sql()
query_total.where &= tab_book1.id.in_([x.id for x in todo_cashbook]) query_total.where &= tab_book1.id.in_([x.id for x in cashbooks])
cursor.execute(*query_total) cursor.execute(*query_total)
records_total = cursor.fetchall() records_total = cursor.fetchall()
@ -351,7 +304,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
date_to=query_date, date_to=query_date,
date_from=query_date - timedelta(days=365), date_from=query_date - timedelta(days=365),
) )
query_12m.where &= tab_book2.id.in_([x.id for x in todo_cashbook]) query_12m.where &= tab_book2.id.in_([x.id for x in cashbooks])
cursor.execute(*query_12m) cursor.execute(*query_12m)
records_12m = cursor.fetchall() records_12m = cursor.fetchall()
@ -370,9 +323,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
record[2], record[4]) record[2], record[4])
result['yield_sales_12m'][record[0]] = quantize_val( result['yield_sales_12m'][record[0]] = quantize_val(
record[3], record[4]) record[3], record[4])
# store to cache
MemCache.store_result(cashbooks, cache_keys, result, todo_cashbook)
return {x: result[x] for x in names} return {x: result[x] for x in names}
@classmethod @classmethod
@ -457,10 +407,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
CBook = pool.get('cashbook.book') CBook = pool.get('cashbook.book')
Uom = pool.get('product.uom') Uom = pool.get('product.uom')
Currency = pool.get('currency.currency') Currency = pool.get('currency.currency')
IrDate = pool.get('ir.date')
MemCache = pool.get('cashbook.memcache')
cursor = Transaction().connection.cursor() cursor = Transaction().connection.cursor()
context = Transaction().context
(query, tab_book) = cls.get_asset_quantity_sql() (query, tab_book) = cls.get_asset_quantity_sql()
company_currency = CBook.default_currency() company_currency = CBook.default_currency()
@ -473,37 +420,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
'digits'] 'digits']
} }
cache_keys = {
x.id: MemCache.get_key_by_record(
name='get_asset_quantity',
record=x,
query=[{
'model': 'cashbook.line',
'query': [('cashbook.parent', 'child_of', [x.id])],
}, {
'model': 'currency.currency.rate',
'query': [('currency.id', '=', x.currency.id)],
'cachekey' if ENA_CURRKEY else 'disabled':
CACHEKEY_CURRENCY % x.currency.id,
}, {
'model': 'investment.rate',
'query': [('asset.id', '=', x.asset.id)],
'cachekey' if ENA_ASSETKEY else 'disabled':
CACHEKEY_ASSETRATE % x.asset.id,
} if x.asset is not None else {}],
addkeys=[
str(company_currency),
str(context.get('qdate', IrDate.today()).toordinal()),
])
for x in cashbooks
}
# read from cache
(todo_cashbook, result) = MemCache.read_from_cache(
cashbooks, cache_keys, names, result)
if len(todo_cashbook) == 0:
return result
def values_from_record(rdata): def values_from_record(rdata):
""" compute values for record """ compute values for record
""" """
@ -644,9 +560,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
Decimal('100.0') * c_val / p_amount - Decimal('100.0') Decimal('100.0') * c_val / p_amount - Decimal('100.0')
).quantize(Decimal(str(1/10 ** digits))) ).quantize(Decimal(str(1/10 ** digits)))
result['digits'][id_book] = None result['digits'][id_book] = None
# store to cache
MemCache.store_result(cashbooks, cache_keys, result, todo_cashbook)
return {x: result[x] for x in names} return {x: result[x] for x in names}
@fields.depends('id') @fields.depends('id')