NeoEntityX
NeoEntityX6d ago

Caching with Convex

Hi, I am using the action-cache in my app, however there's a problem with cache.fetch. Suppose I have an action summarizePaperInternal that returns a string, and I want to cache it, so I follow the docs at https://github.com/get-convex/action-cache, and create an ActionCache. I would assume that after defining the action, the return of the ActionCache will be same as my action. However that doesn't appear to be the case and ActionCache.fetch returns any.
No description
No description
No description
20 Replies
NeoEntityX
NeoEntityXOP6d ago
Speaking of the docs, there seems to be a bug/error/typo in it at https://github.com/get-convex/action-cache. myFunction just calls cache.fetch but doesn't actually return any results of the fetched value from the cache.
No description
erquhart
erquhart6d ago
Good catches here, thanks for that. PR up: https://github.com/get-convex/action-cache/pull/7
ian
ian6d ago
released in v0.2.7
NeoEntityX
NeoEntityXOP6d ago
Ok but my main problem is still the return type of cache.fetch It's returning any, and I can't deploy my app because of it unless I type cast NVM, I see the PR fixes the return type too. Let me try, one sec. Looks like it's been fixed. Thanks guys, lightspeed. Unrelated to this, but I've pointed out a bug on the landing page two times now but no one seems to respond @erquhart @Ian
ian
ian6d ago
yeah thanks for that! I already saw it posted by Wayne in our internal chat this morning
NeoEntityX
NeoEntityXOP5d ago
Cool Seems like I spoke too soon. This is a really weird bug.
NeoEntityX
NeoEntityXOP5d ago
When I only store the result in a variable and don't return it, the type is correctly inferred
No description
NeoEntityX
NeoEntityXOP5d ago
However
NeoEntityX
NeoEntityXOP5d ago
As soon as I return the result, the type changes to any. What the fuck?
No description
NeoEntityX
NeoEntityXOP5d ago
@erquhart Sorry for the ping, but I see you're online. Could you take a look at this? This entire caching bug has been blocking me for a good 2+ days now.
ian
ian5d ago
This seems like a common issue related to circular types. If you add return types to all the related handler functions that hopefully fixes it essentially when the return type of one function internal.foo.one depends on the return type type of internal.foo.two, unfortunately the type of internal itself depends on both of them. So one's return value ends up depending on itself.
ian
ian5d ago
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment
NeoEntityX
NeoEntityXOP5d ago
Interesting.
NeoEntityX
NeoEntityXOP5d ago
I guess you'll probably need to update the docs again, because this won't work out of the box then:
No description
ian
ian5d ago
good call - it's a good practice to put out there too. Do you have time to cut a PR improving the docs?
NeoEntityX
NeoEntityXOP5d ago
I can do it, but there's a problem
NeoEntityX
NeoEntityXOP5d ago
In the example, the return type of generateLLMResponse isn't known, so inside myFunction, we can't explicitly write out the return type.
No description
ian
ian5d ago
you can take some liberties - that's just as an illustrative example. e.g. { text: string, usage: Usage } or just string even fwiw the example.ts is correct, but b/c it was explicitly typing the return value
NeoEntityX
NeoEntityXOP5d ago
I think ReturnType<generateLLMResponse> might work I'll just do a generic pr with string.
NeoEntityX
NeoEntityXOP5d ago
pr is up https://github.com/get-convex/action-cache/pull/8 --- fyi: doing
result: MyReturnType = myCache.fetch()
return result
result: MyReturnType = myCache.fetch()
return result
won't work. runs into same inference issue. there's only one way to fix it:
handler: async (ctx, args): Promise<MyReturnType> => {
handler: async (ctx, args): Promise<MyReturnType> => {
which is typing the handler
GitHub
explicitly type result by NexWasTaken · Pull Request #8 · get-con...
Addresses issues raised in Discord: https://discord.com/channels/1019350475847499849/1377179000643915808 By submitting this pull request, I confirm that you can use, modify, copy, and redistribute...

Did you find this page helpful?