Web method caching significantly enhances your site's performance by temporarily storing frequently-accessed data—specifically, the return values of your backend API calls. This allows subsequent requests for the same data to be served quickly from the cache stored on Wix's infrastructure, reducing the need to re-execute the logic each time the request is made.
You can set your backend methods to cache return values for a specific duration or Time To Live (TTL). As a result, the next time a visitor makes a request that triggers your backend methods, the cached data is served immediately until the TTL expires or the cache is invalidated. Caching improves overall performance by reducing server load, decreasing response times, and minimizing resource consumption. Additionally, it provides reliability, as caches can serve as a backups if the original data source is temporarily unavailable.
Note: The web methods in your code aren't cached automatically.
You can implement web method caching in your web modules using:
Web method caching is most beneficial in the following scenarios:
Conversely, web method caching may not always be the best solution in situations where your backend methods:
In cases where real-time updates are essential, consider implementing caching selectively. For example, if a backend method retrieves foreign exchange rates, it may be valuable to cache that data for a 24-hour period, allowing it to refresh on a daily basis.
For data that changes less frequently, you can set a longer TTL period; otherwise, web method caches have a default TTL of 1 week.
Cache invalidation is a vital process that ensures the accuracy and relevance of the data being presented to site visitors. It involves clearing cached return values when the underlying data changes. Cached return values are cleared when:
You can implement cache invalidation in your code using the invalidateCache()
method from wix-cache-backend
. This should be done when there are significant changes to your site's content, such as updates in product availability or the creation of a new blog post.
By invalidating caches, you maintain data integrity, prevent site visitors from accessing outdated information, and optimize your site's performance and responsiveness.