Velo: About Function Caching in the Backend

Caching helps to significantly speed up response times of your backend function calls by temporarily storing your function's return values. This enables subsequent function calls for the same data to be served more quickly from the cache on Wix's infrastructure, rather than re-executing the function's logic each time it's called. By implementing caching in your backend code, you can reduce the need to repeatedly execute backend functions, enhancing your site's performance.

Note: The backend functions in your code are not cached automatically.

What is function caching?

When a site visitor accesses dynamic content on your site, your backend functions are called to generate that content. The return values to these API calls are then temporarily stored, or cached, for a period of time. As a result, the next time those functions are called, the cached data is returned immediately. Caching improves overall performance by reducing server load, decreasing response times, and minimizing resource consumption. It also provides reliability, as a cached return value can serve as a backup if the original data source is temporarily unavailable.

With backend caching, you can assign identifiers to cached return values using tags, and determine a cache's Time To Live (TTL). Tags allow you to easily locate and invalidate specific caches when needed. After the TTL expires, the cache is considered stale, and the function re-executes upon the next request.

To implement caching in your backend code, use the webMethod() function from wix-web-module. Inside the webMethod() function, use the options parameter that contains the cache object. The cache object has the following 2 properties:

  • tags: Gives an identifier to the cached API call's return value.
  • ttl: Specifies the cache's time-to-live.
Copy
1

When should I use function caching?

Caching should be used to enhance performance and efficiency in scenarios such as:

  • When the same data is frequently requested.
  • When you have static or rarely changing data.
  • When your backend functions involve intensive computations or database queries.

In situations where real-time accuracy is crucial and data updates frequently, selectively applying caching can help ensure your site displays the most current information. For example, if you have a backend function that retrieves foreign exchange rates to display on your site, you might cache the function's return value for only 24 hours. After this period, the function's return value is cached again the next time the function is called. To cache a return value for 24 hours, set the ttl property to 86400.

For data that changes less frequently, you can set a longer TTL period. If no TTL is specified, the default is 604000 seconds (approximately 1 week).

How to invalidate a function cache?

While caching data is beneficial, it's crucial to manage it carefully to ensure your site visitors always see the most up-to-date information. Cache invalidation strategies are essential to maintaining data accuracy and relevance for your site.

You can invalidate your function caches when:

  • You use the invalidateCache() function.
  • The TTL expires.
  • You republish your site with code changes.

Invalidating cached return values causes your backend functions to re-execute and retrieve the freshest data. To invalidate a cached return value, you must specify which return value to invalidate using tags. By invalidating cached return values based on specified tags, you maintain data integrity, prevent site visitors from accessing outdated information, and optimize your site's performance and responsiveness.

To implement function cache invalidation, use the invalidateCache() function from the wix-cache-backend module. Use the invalidationMethods parameter to specify the tag identifiers for the cached return values you want to invalidate.

Important:

If you plan to invalidate your caches, you must first cache them using the tags property of the cache object to identify them.

Copy
1

When should I invalidate my cache?

Whenever your site's data changes significantly, even if the data is relatively static, you'll want to invalidate the cache to ensure your site displays the latest information. For example, when you add or update a product on your e-commerce site, you should invalidate the relevant cache. Once invalidated, the next time a site visitor accesses your products page, the backend function to retrieve your products is called again, and the return value is then re-cached with the new or updated product, enhancing your site's performance.

Note: With the wix-cache-backend API, you can only invalidate your backend function caches. To invalidate the server side rendering (SSR) cache of your site, see the invalidateCache() function in the wix-site-backend module.

Was this helpful?
Yes
No