The Point of Sale (POS) system in Odoo 18 leverages an efficient caching mechanism to store frequently accessed data in memory, enhancing performance and responsiveness. By reducing database queries and optimizing data retrieval, caching ensures smoother operations in high-traffic retail environments.
Key Benefits of Using POS Cache in Odoo 18
Faster Data Access
How to Store and Retrieve Values in Odoo 18 POS Cache
Odoo 18 provides multiple ways to manage cached data in the POS module. Below are different methods to store and retrieve values using the pos object and cache utilities.
Storing and Retrieving Order-Specific Data
You can store temporary data related to an order using the pos object.
Storing a Value in the Current Order
import { usePos } from "@point_of_sale/app/store/pos_hook"; // import usePos
this.pos = usePos();
this.pos.get_order().order_note = 'Special instructions';Retrieving the Stored Value
var note = this.pos.get_order().order_note;
console.log(note);
Storing Cashier-Specific Data
If you need to store temporary data related to the logged-in cashier, you can use:
Storing a Value for the Cashier
this.pos.get_cashier().preferences = { discount_access: true };Retrieving the Cashier’s Data
var cashierPrefs = this.pos.get_cashier().preferences;
console.log(cashierPrefs.discount_access);
Using the Cache Utility for Advanced Caching
Odoo 18 introduces a dedicated Cache utility for more structured caching.
Importing and Initializing the Cache
import { Cache } from "@web/core/utils/cache";
setup() {
super.setup();
this.pos.cache = new Cache((key) => null);
},Storing a Value in Cache
setCacheValue(key, value) {
this.pos.cache.clear(key);
this.pos.cache.read(key);
const { cache } = this.pos.cache._getCacheAndKey(key);
cache[key] = value;
},
// Example usage:
this.setCacheValue('loyalty_points', 500);Retrieving a Value from Cache
getCacheValue(key) {
return this.pos.cache.read(key);
},
// Example usage:
var points = this.getCacheValue('loyalty_points');
console.log(points);Clearing Cache Data
The POS cache is temporary and automatically clears when the session ends. However, you can manually remove cached data if needed.
this.pos.cache.invalidate(); // Removes all
this.pos.cache.clear('loyalty_points'); // Removes a specific key
Using the POS cache in Odoo 18 significantly improves performance by reducing database load and accelerating data access. Whether storing order details, cashier preferences, or custom key-value pairs, the caching mechanisms ensure a smoother and more efficient POS experience.
By leveraging these techniques, businesses can enhance transaction speed, reduce hardware strain, and provide a seamless checkout process for customers.
To read more about How to Store Values in POS Cache in Odoo 17, refer to our blog How to Store Values in POS Cache in Odoo 17.