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 logic each time the function is called. By implementing caching in your backend code, you can reduce the need to repeatedly execute your backend function calls, enhancing your site's performance.

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

What is function caching?

When a site visitor requests dynamic content on your site, your backend functions are called to fetch and display that content. The return values to these API function 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 function 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. The function re-executes upon the next request and the return value is re-cached.

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 properties:

  • tags: An identifier to the cached return value of the function.
  • ttl: The cache's time-to-live in seconds.
Copy

When should I use function caching?

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

  • There is frequent request for the same data, for example, a product catalog or popular blog posts.
  • You have static or rarely changing data, such as company information or fixed schedules.
  • Your backend functions involve heavy computations or database queries, such as generating reports from extensive datasets.

However, backend function caching can be less efficient in situations where your backend function:

  • Returns certain real-time data, such as stock prices or live sports scores.
  • Returns user-specific data, such as user account details.
  • Involves sensitive data that must remain private.
  • Requires fresh data for each operation, such as processing payments.

In situations where real-time accuracy is crucial and data updates frequently, you might selectively implement caching while ensuring your site stays up-to-date. For example, if you have a backend function that retrieves foreign exchange rates, you could cache the functions return value for a 24-hour period, since exchange rates change daily. This means that after each 24-hour period, the function's return value will be refreshed and cached upon the next call. 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 604800 seconds (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: You can assign multiple tags to your caches. Any cache containing at least one tag specified in the invalidationMethods object will be invalidated using the invalidateCache() function.

Copy

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 product 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 invalidate:

To invalidate the server side rendering (SSR) cache of your site, see the invalidateCache() function in the wix-site-backend module.

Did this help?