How to make "defineCachedFunction" cache the return data forever? #3118
fandyajpo
started this conversation in
Show and tell
Replies: 2 comments 1 reply
|
i have try to add maxAge = Infinity, remove maxAge, remove all key it doesnt cache the data forever |
0 replies
|
Hi! Are you trying to cache the mongodb connexion itself? You won't be able to do so because it's not serializable (nitro uses https://github.com/unjs/unstorage under the hood to store cached data). Instead, you should use something like: // server/utils/mongodb/client.ts
let cachedMongoConnection;
export async function getMongoConnection() {
if (!cachedMongoConnection) {
cachedMongoConnection = await connect("");
}
return cachedMongoConnection;
}Then you can use it like: // server/utils/mongodb/my-entity.ts
import { getMongoConnection } from './client'
export const cachedMongoEntity = defineCachedFunction(
async (name: string) => {
const client = await getMongoConnection()
const db = client.db('my-db');
const collection = db.collection('my-entity');
return await collection.find({}).toArray(); // return serializable data
}, {
maxAge: 31536000, // one year
name: 'mongoEntity',
getKey: (name: string) => name
},
)As per your question for returning same data "forever", nitro tries to follow IETF standards in which one year is advised as a standard max value. See RFC 2616 Hope this helps! |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
i mean how to make it cached forever?
All reactions