> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt ## Resource: Implement Web Method Caching ## Article: Implement Web Method Caching ## Article Link: https://dev.wix.com/docs/develop-websites/articles/best-practices/caching/implement-web-method-caching.md ## Article Content: # Implement Web Method Caching [Web method caching](https://dev.wix.com/docs/develop-websites/articles/best-practices/caching/about-function-caching-in-the-backend.md) is an effective way to improve the performance of your site by temporarily storing the return values of web methods in a cache. This article guides you through the process of implementing web method caching and how to invalidate it when necessary. ## Step 1 | Set up caches You must assign identifiers to cached return values of your web methods and can optionally define a cache's Time To Live (TTL). These required identifiers, called tags, allow you to easily identify caches that may need invalidation. For example, you can invalidate all caches that have the tag "math." To set up caching for your web methods: 1. Open your [web modules](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/backend-code/web-modules/about-web-modules.md) and find the methods you want to cache. 1. For each web method that you want to cache, update the code so that the [`webMethod()`](https://dev.wix.com/docs/velo/api-reference/wix-web-module/web-method.md) is called with the following `options`: - `cache`: An object containing: - `tags`: One or more identifiers of the cached return value. - `ttl`: The cache's time-to-live in seconds. If omitted, defaults to 1 week (`604800` seconds). ```js import { Permissions, webMethod } from "wix-web-module"; export const cachedMultiply = webMethod(Permissions.Anyone, (a, b) => a * b, { cache: { tags: ["multiply", "math"], ttl: 604800, }, }); ```
__Important:__ The `tags` field is required for caching. If omitted, nothing is cached.## Step 2 | Invalidate caches Invalidating or clearing your caches causes your backend methods to re-execute upon the following request, ensuring that fresh data is retrieved and cached. To invalidate caches, use the `invalidateCache()` method from the [`wix-cache-backend`](https://dev.wix.com/docs/velo/api-reference/wix-cache-backend/cache/invalidate-cache.md) module. This method accepts the `invalidationMethods` parameter, which specifies the tags previously assigned to the caches you want to invalidate.
__Important:__ - You must specify `invalidationMethods` when invalidating your cache. - A cache can be assigned multiple tags. Any cache with at least one tag specified in the `invalidationMethods` object is invalidated when `invalidateCache()` is called. - The `invalidateCache()` method must be [elevated](https://dev.wix.com/docs/velo/api-reference/wix-auth/elevate.md) and wrapped in a [web method](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/backend-code/web-modules/about-web-modules.md#web-methods).To invalidate caches: 1. Add the necessary imports in your web modules (backend files). ```js import { Permissions, webMethod } from "wix-web-module"; import { elevate } from "wix-auth"; import { cache } from "wix-cache-backend"; ``` 1. Create a variable to store the tags assigned to the cache. Then call `elevate()` and store its returned value in another variable. ```js const invalidationMethods = [{ tag: "math" }, { tag: "multiply" }]; const elevatedInvalidateCache = elevate(cache.invalidateCache); ``` 1. Call `webMethod()` and store its return value in an exported variable. Pass a permissions value and the elevated invalidation method. ```js export const invalidateCache = webMethod(Permissions.Admin, async () => { try { await elevatedInvalidateCache(invalidationMethods); console.log("Successfully invalidated cache."); } catch (error) { console.error(error); } }); ``` > **Notes:** > - The `wix-cache-backend` API, allows you to invalidate web method caches as well as [router caches](https://dev.wix.com/docs/velo/api-reference/wix-router/ok.md). > - To invalidate the server side rendering (SSR) cache of your site, refer to the [`invalidateCache()`](https://dev.wix.com/docs/velo/api-reference/wix-site-backend/invalidate-cache.md) method in the `wix-site-backend` module. ## See also - [About Caching](https://dev.wix.com/docs/develop-websites/articles/best-practices/caching/about-caching.md) - [About Web Method Caching](https://dev.wix.com/docs/develop-websites/articles/best-practices/caching/about-web-method-caching.md) - [About Web Modules](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/backend-code/web-modules/about-web-modules.md) - [wix-web-module API](https://dev.wix.com/docs/velo/api-reference/wix-web-module/introduction.md) - [wix-cache-backend API](https://dev.wix.com/docs/velo/api-reference/wix-cache-backend/cache/introduction.md)