N
Nuxt2mo ago
RicharDVD

Clear nitro route cache programatically

Hi! I would like to clear some cache programatically in a Nitro handler so another service can call this route to clear a certain part of the cache. I currently have the following code:
export default defineEventHandler(async () => {
try {
const storage = useStorage();
// Clear the brands cache
await storage.clear('cache:nitro:handlers:car-brands');
const keys = await storage.keys('cache:nitro:handlers:car-brands');

return {
keys,
};
} catch (error) {
console.error('Failed to clear cache:', error);
throw createError({
statusCode: 500,
message: 'Failed to clear cache',
});
}
});
export default defineEventHandler(async () => {
try {
const storage = useStorage();
// Clear the brands cache
await storage.clear('cache:nitro:handlers:car-brands');
const keys = await storage.keys('cache:nitro:handlers:car-brands');

return {
keys,
};
} catch (error) {
console.error('Failed to clear cache:', error);
throw createError({
statusCode: 500,
message: 'Failed to clear cache',
});
}
});
I fetch the keys for debugging purposes. When fetching the keys, I do receive the correct keys that are cached but the clear method doesn't seem to do much? When I look in the nitro cache folder locally, the files are still there.
{
"keys": [
"cache:nitro:handlers:car-brands:carbrands.json",
"cache:nitro:handlers:car-brands:carmodelsMn2yMn29.json",
"cache:nitro:handlers:car-brands:carmodelsOJopAqe7.json"
]
}
{
"keys": [
"cache:nitro:handlers:car-brands:carbrands.json",
"cache:nitro:handlers:car-brands:carmodelsMn2yMn29.json",
"cache:nitro:handlers:car-brands:carmodelsOJopAqe7.json"
]
}
What am I doing wrong?
3 Replies
kapa.ai
kapa.ai2mo ago
Hi @RicharDVD. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To clear the Nitro route cache programmatically, you're on the right track, but there are a few things to consider and adjust in your approach. Let's break it down: 1. The useStorage() method is correct for accessing Nitro's storage layer. 2. The clear() method might not be the best choice for selectively clearing cache entries. Instead, you should use removeItem() for each key you want to remove. 3. The cache keys you're trying to clear might not exactly match the format you're using. Here's an improved version of your code that should work better:
export default defineEventHandler(async () => {
try {
const storage = useStorage();
const cachePrefix = 'cache:nitro:handlers:car-brands';

// Get all keys with the specified prefix
const keys = await storage.getKeys(cachePrefix);

// Remove each item individually
for (const key of keys) {
await storage.removeItem(key);
}

// Verify removal
const remainingKeys = await storage.getKeys(cachePrefix);

return {
clearedKeys: keys,
remainingKeys,
};
} catch (error) {
console.error('Failed to clear cache:', error);
throw createError({
statusCode: 500,
message: 'Failed to clear cache',
});
}
});
export default defineEventHandler(async () => {
try {
const storage = useStorage();
const cachePrefix = 'cache:nitro:handlers:car-brands';

// Get all keys with the specified prefix
const keys = await storage.getKeys(cachePrefix);

// Remove each item individually
for (const key of keys) {
await storage.removeItem(key);
}

// Verify removal
const remainingKeys = await storage.getKeys(cachePrefix);

return {
clearedKeys: keys,
remainingKeys,
};
} catch (error) {
console.error('Failed to clear cache:', error);
throw createError({
statusCode: 500,
message: 'Failed to clear cache',
});
}
});
This approach does the following: 1. It uses getKeys() to find all keys with the specified prefix. 2. It then iterates over these keys and removes each item individually using removeItem(). 3. Finally, it checks for any remaining keys to verify the removal. A few additional points to note: - The cache files you see in the local Nitro cache folder might not immediately disappear. Nitro's storage layer abstracts the actual storage mechanism, which can vary depending on the deployment environment. - If you're using a custom storage driver or a specific cache configuration, you might need to adjust your approach accordingly. - Make sure your route has the necessary permissions to modify the cache. For more information on Nitro's storage capabilities and caching, you can refer to the following resources: Nuxt 3 Documentation - Server Storage Nitro Documentation - Storage Layer If you're still experiencing issues after these changes, you might want to check your Nitro and Nuxt configurations to ensure there are no conflicting cache settings. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@268039364590829568> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
RicharDVD
RicharDVDOP2mo ago
Odd, I tried this earlier but it didn't work. Maybe I made a typo or something😅

Did you find this page helpful?