optimize(various): optimize with frappe.get_cached_value

This commit is contained in:
Rushabh Mehta 2018-08-08 16:33:40 +05:30
parent b8f91f36fc
commit e7c8b0c138
4 changed files with 25 additions and 12 deletions

View file

@ -658,12 +658,12 @@ def get_cached_doc(*args, **kwargs):
# redis cache
doc = cache().hget('document_cache', key)
if doc:
doc = frappe.get_doc(doc)
doc = get_doc(doc)
local.document_cache[key] = doc
return doc
# database
doc = frappe.get_doc(*args, **kwargs)
doc = get_doc(*args, **kwargs)
return doc
@ -677,6 +677,17 @@ def clear_document_cache(doctype, name):
del local.document_cache[key]
cache().hdel('document_cache', key)
def get_cached_value(doctype, name, fieldname, as_dict=False):
doc = get_cached_doc(doctype, name)
if isinstance(fieldname, text_type):
if as_dict:
throw('Cannot make dict for single fieldname')
return doc.get(fieldname)
values = [doc.get(f) for f in fieldname]
if as_dict:
return _dict(zip(fieldname, values))
return values
def get_doc(*args, **kwargs):
"""Return a `frappe.model.document.Document` object of the given type and name.

View file

@ -300,9 +300,10 @@ class Database:
result = self._cursor.fetchall()
ret = []
needs_formatting = self.needs_formatting(result, formatted)
keys = [column[0] for column in self._cursor.description]
for r in result:
row_dict = frappe._dict({})
values = []
for i in range(len(r)):
if needs_formatting:
val = self.convert_to_simple_type(r[i], formatted)
@ -311,8 +312,9 @@ class Database:
if as_utf8 and type(val) is text_type:
val = val.encode('utf-8')
row_dict[self._cursor.description[i][0]] = val
ret.append(row_dict)
values.append(val)
ret.append(frappe._dict(zip(keys, values)))
return ret
def needs_formatting(self, result, formatted):
@ -740,7 +742,7 @@ class Database:
del self.value_cache[dt]
frappe.clear_document_cache(dt, dn)
def set(self, doc, field, val):
"""Set value in document. **Avoid**"""
doc.db_set(field, val)

View file

@ -9,7 +9,7 @@ def execute():
frappe.reload_doc('core', 'doctype', 'system_settings', force=True)
if not frappe.db.get_value("System Settings", None, "currency_precision"):
default_currency = frappe.db.get_default("currency")
number_format = frappe.db.get_value("Currency", default_currency, "number_format") \
number_format = frappe.db.get_value("Currency", default_currency, "number_format", cache=True) \
or frappe.db.get_default("number_format")
if number_format:
precision = get_number_format_info(number_format)[2]

View file

@ -354,7 +354,7 @@ def remainder(numerator, denominator, precision=2):
def round_based_on_smallest_currency_fraction(value, currency, precision=2):
smallest_currency_fraction_value = flt(frappe.db.get_value("Currency",
currency, "smallest_currency_fraction_value"))
currency, "smallest_currency_fraction_value", cache=True))
if smallest_currency_fraction_value:
remainder_val = remainder(value, smallest_currency_fraction_value, precision)
@ -419,7 +419,7 @@ def fmt_money(amount, precision=None, currency=None):
if precision > 2:
if len(decimals) < 3:
if currency:
fraction = frappe.db.get_value("Currency", currency, "fraction_units") or 100
fraction = frappe.db.get_value("Currency", currency, "fraction_units", cache=True) or 100
precision = len(cstr(fraction)) - 1
else:
precision = number_format_precision
@ -459,7 +459,7 @@ def fmt_money(amount, precision=None, currency=None):
amount = minus + amount
if currency and frappe.defaults.get_global_default("hide_currency_symbol") != "Yes":
symbol = frappe.db.get_value("Currency", currency, "symbol") or currency
symbol = frappe.db.get_value("Currency", currency, "symbol", cache=True) or currency
amount = symbol + " " + amount
return amount
@ -504,7 +504,7 @@ def money_in_words(number, main_currency = None, fraction_currency=None):
if not main_currency:
main_currency = d.get('currency', 'INR')
if not fraction_currency:
fraction_currency = frappe.db.get_value("Currency", main_currency, "fraction") or _("Cent")
fraction_currency = frappe.db.get_value("Currency", main_currency, "fraction", cache=True) or _("Cent")
number_format = frappe.db.get_value("Currency", main_currency, "number_format", cache=True) or \
frappe.db.get_default("number_format") or "#,###.##"