Skip to main content
Version: v3

Storage Adapter

alova provides a comprehensive multi-level cache function, and the following caches are used by default

  • L1 cache: both the client and the server save in the form of object key-value

  • L2 cache: the client uses localStorage to store, and the server does not provide an L2 adapter

In certain situations, you may need to customize different storage adapters. Customizing storage adapters is also very simple. You only need to specify functions for saving data, getting data, and removing data.

const customStorageAdapter = {
set(key, value) {
// Save data, value is structured data, you can call JSON.stringify to convert it to a string
},
get(key) {
// Get data, you need to return structured data, you can call JSON.parse to convert it to an object
},
remove(key) {
// Remove data
},
clear() {
// Clear data
}
};

Use a custom adapter.

const alovaInstance = createAlova({
// ...
l1Cache: customStorageAdapter, // l1 cache
l2Cache: customStorageAdapter // l2 cache
});

For more information about response caching, please refer to Detailed of Cache.

SessionStorage Storage Adapter Example

const sessionStorageAdapter = {
set(key, value) {
sessionStorage.setItem(key, JSON.stringify(value));
},
get(key) {
const data = sessionStorage.getItem(key);
return data ? JSON.parse(data) : data;
},
remove(key) {
sessionStorage.removeItem(key);
},
clear() {
sessionStorage.clear();
}
};