Unipile improves your application's performance and prevent hits of rate limiting by caching methods requests. This page provides an in-depth look at Unipile caching mechanisms and the how to configure them.
Control cache on requests
Unipile support the Cache-Control standard request header and few of its directives to control the caching when you make a request on Methods API.
no-store
no-storeBy default, every request is cached. The no-store request directive allows you to request that caches refrain from storing the request and corresponding response. This is useful if you don't want to store any data on Unipile's servers.
Cache-Control: no-storeimport { UnipileMessaging } from "unipile";
const key = process.env.UNIPILE_API_KEY ?? "";
const messagingApi = new UnipileMessaging({ key });
// Per request
await messagingApi.getChatsList({
path: { account_id: "acc_123456789" },
headers: {
"X-API-KEY": key,
"Cache-Control": "no-store",
},
});
// Reuse this header object in your app if you want this behavior everywhere
const NO_STORE_HEADERS = {
"X-API-KEY": key,
"Cache-Control": "no-store",
};import os
import unipile
ACCOUNT_ID = "acc_123456789"
configuration = unipile.Configuration()
configuration.api_key["apiKey"] = os.getenv("UNIPILE_API_KEY", "")
with unipile.ApiClient(configuration) as api_client:
messaging_api = unipile.MessagingApi(api_client)
response = messaging_api.get_chats_list(
ACCOUNT_ID,
_headers={"Cache-Control": "no-store"},
)
# Reuse this header object in your app if you want this behavior everywhere
NO_STORE_HEADERS = {"Cache-Control": "no-store"}no-cache
no-cacheThe no-cache request directive asks caches to validate the response with the origin server before reuse. This allow you yo request the most up-to-date data from the provider even if the cache has a fresh response.
Cache-Control: no-cacheimport { UnipileMessaging } from "unipile";
const key = process.env.UNIPILE_API_KEY ?? "";
const messagingApi = new UnipileMessaging({ key });
await messagingApi.getChatsList({
path: { account_id: "acc_123456789" },
headers: {
"X-API-KEY": key,
"Cache-Control": "no-cache",
},
});import os
import unipile
ACCOUNT_ID = "acc_123456789"
configuration = unipile.Configuration()
configuration.api_key["apiKey"] = os.getenv("UNIPILE_API_KEY", "")
with unipile.ApiClient(configuration) as api_client:
messaging_api = unipile.MessagingApi(api_client)
response = messaging_api.get_chats_list(
ACCOUNT_ID,
_headers={"Cache-Control": "no-cache"},
)max-age
max-ageThe max-age=N request directive indicates that the you allows a stored response that is generated on the origin server within N seconds — where N may be any non-negative integer (including 0).
Cache-Control: max-age=10800import { UnipileMessaging } from "unipile";
const key = process.env.UNIPILE_API_KEY ?? "";
const messagingApi = new UnipileMessaging({ key });
await messagingApi.getChatsList({
path: { account_id: "acc_123456789" },
headers: {
"X-API-KEY": key,
"Cache-Control": "max-age=10800",
},
});import os
import unipile
ACCOUNT_ID = "acc_123456789"
configuration = unipile.Configuration()
configuration.api_key["apiKey"] = os.getenv("UNIPILE_API_KEY", "")
with unipile.ApiClient(configuration) as api_client:
messaging_api = unipile.MessagingApi(api_client)
response = messaging_api.get_chats_list(
ACCOUNT_ID,
_headers={"Cache-Control": "max-age=10800"},
)In the case above, if the response with Cache-Control: max-age=10800 was generated more than 3 hours ago (calculated from max-age), the cache couldn't reuse that response.
Note: This is set to 1 hour by default if not specified in the request.
Caching status of responses
Unipile support standard headers to give you informations about the caching status of responses.
X-Cache
X-CacheThe X-Cache response header tells you if the cache was used to produce the response payload. If the response if coming from the cache, the value of the header is HIT, if the response is coming from the Provider's API, the value of the header is MISS.
X-Cache: HITimport { UnipileMessaging } from "unipile";
const key = process.env.UNIPILE_API_KEY ?? "";
const messagingApi = new UnipileMessaging({ key });
const { response } = await messagingApi.getChatsList({
path: { account_id: "acc_123456789" },
headers: { "X-API-KEY": key },
});
console.log(response.headers.get("X-Cache")); // HIT or MISSimport os
import unipile
ACCOUNT_ID = "acc_123456789"
configuration = unipile.Configuration()
configuration.api_key["apiKey"] = os.getenv("UNIPILE_API_KEY", "")
with unipile.ApiClient(configuration) as api_client:
messaging_api = unipile.MessagingApi(api_client)
response = messaging_api.get_chats_list_with_http_info(ACCOUNT_ID)
print(response.headers.get("X-Cache")) # HIT or MISSThis header is not present if no-store directive is used in the request.
Age
AgeThe Age response header tells you how aged is the response when it's coming from the cache.
Age: 10000import { UnipileMessaging } from "unipile";
const key = process.env.UNIPILE_API_KEY ?? "";
const messagingApi = new UnipileMessaging({ key });
const { response } = await messagingApi.getChatsList({
path: { account_id: "acc_123456789" },
headers: { "X-API-KEY": key },
});
console.log(response.headers.get("Age")); // Age in secondsimport os
import unipile
ACCOUNT_ID = "acc_123456789"
configuration = unipile.Configuration()
configuration.api_key["apiKey"] = os.getenv("UNIPILE_API_KEY", "")
with unipile.ApiClient(configuration) as api_client:
messaging_api = unipile.MessagingApi(api_client)
response = messaging_api.get_chats_list_with_http_info(ACCOUNT_ID)
print(response.headers.get("Age")) # Age in seconds