About Web Method Caching

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.

Supported IDEs

You can implement web method caching in your web modules using:

  • The editor (Wix Studio and Wix Editor)
  • The Wix IDE
  • Your local IDE

When to use web method caching

Web method caching is most beneficial in the following scenarios:

  • When there are numerous requests for the same data, such as a product catalog or popular blog posts.
  • When the data is mostly static or seldom changes, like company profiles, fixed schedules, or images.
  • When your backend methods involve heavy computations or extensive database queries that can strain server resources.

Conversely, web method caching may not always be the best solution in situations where your backend methods:

  • Return real-time data that requires immediate accuracy, such as live stock prices or sports scores.
  • Return sensitive or personalized data, like user account details and preferences.
  • Require fresh data for each operation, as in the case of processing payments.

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.

Understanding cache invalidation

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:

  • The cache's TTL expires.
  • You republish your site after updating code.
  • You manually implement cache invalidation in your code.

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.

See also

Did this help?