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>
This commit is contained in:
Devin Slauenwhite 2022-10-20 06:26:07 -04:00 committed by GitHub
parent adeb1f92c8
commit b88e850d55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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 {}