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>
diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py
index 9fb3be5..c8ca8a8 100644
--- a/erpnext/stock/utils.py
+++ b/erpnext/stock/utils.py
@@ -13,6 +13,8 @@
 import erpnext
 from erpnext.stock.valuation import FIFOValuation, LIFOValuation
 
+BarcodeScanResult = Dict[str, Optional[str]]
+
 
 class InvalidWarehouseCompany(frappe.ValidationError):
 	pass
@@ -552,7 +554,16 @@
 
 
 @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 @@
 		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 @@
 		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 @@
 		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 {}