perf: cache barcode scan result (backport #32629) (#32672)

perf: cache barcode scan result (#32629)

* perf: cache barcode scan result

* feat: BarcodeScanResult type

* fix: use safe `get_value` `set_value`

Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
(cherry picked from commit b88e850d55)

Co-authored-by: Devin Slauenwhite <devin.slauenwhite@gmail.com>
This commit is contained in:
mergify[bot]
2022-10-20 15:57:21 +05:30
committed by GitHub
parent 2c9845f8cb
commit 3300856fb4

View File

@@ -13,6 +13,8 @@ from frappe.utils import cstr, flt, get_link_to_form, nowdate, nowtime
import erpnext
from erpnext.stock.valuation import FIFOValuation, LIFOValuation
BarcodeScanResult = Dict[str, Optional[str]]
class InvalidWarehouseCompany(frappe.ValidationError):
pass
@@ -552,7 +554,16 @@ def check_pending_reposting(posting_date: str, throw_error: bool = True) -> bool
@frappe.whitelist()
def scan_barcode(search_value: str) -> Dict[str, Optional[str]]:
def scan_barcode(search_value: str) -> BarcodeScanResult:
def set_cache(data: BarcodeScanResult):
frappe.cache().set_value(f"erpnext:barcode_scan:{search_value}", data, expires_in_sec=120)
def get_cache() -> Optional[BarcodeScanResult]:
if data := frappe.cache().get_value(f"erpnext:barcode_scan:{search_value}"):
return data
if scan_data := get_cache():
return scan_data
# search barcode no
barcode_data = frappe.db.get_value(
@@ -562,7 +573,9 @@ def scan_barcode(search_value: str) -> Dict[str, Optional[str]]:
as_dict=True,
)
if barcode_data:
return _update_item_info(barcode_data)
_update_item_info(barcode_data)
set_cache(barcode_data)
return barcode_data
# search serial no
serial_no_data = frappe.db.get_value(
@@ -572,7 +585,9 @@ def scan_barcode(search_value: str) -> Dict[str, Optional[str]]:
as_dict=True,
)
if serial_no_data:
return _update_item_info(serial_no_data)
_update_item_info(serial_no_data)
set_cache(serial_no_data)
return serial_no_data
# search batch no
batch_no_data = frappe.db.get_value(
@@ -582,6 +597,8 @@ def scan_barcode(search_value: str) -> Dict[str, Optional[str]]:
as_dict=True,
)
if batch_no_data:
_update_item_info(batch_no_data)
set_cache(batch_no_data)
return _update_item_info(batch_no_data)
return {}