Caching is a process that allows you to temporarily store data on Wix's infrastructure so it can be accessed faster, improving site performance. You can cache the return values of backend functions using the webMethod()
function from wix-web-module
, and for routers using the ok()
function from wix-router
. The Cache API provides functionality for invalidating your caches.
With the Cache API, you can invalidate the cached return values from your function calls, ensuring that your site displays the most current information. For instance, let's say you use the queryProducts()
function and cache the response to display your products on your e-commerce site. If a product is discontinued, you should invalidate the cache to reflect this change. Once the cache is invalidated, it will be re-cached the next time the queryProducts()
function is called. This prevents customers from attempting to purchase an item that is no longer available.
Learn more about cache invalidation for backend functions and routers.
To use the Cache API, import cache
from the wix-cache-backend
module:
You can invalidate your cached return values that were previously defined with tags when cached. Your cache is automatically invalidated when:
Functions in the Cache API are restricted and only run if you elevate permissions using the wix-auth
elevate()
function.
Warning: Elevating a function allows it to be called by any site visitor. Exercise caution to prevent security vulnerabilities.