#query prevent cache

4 messages · Page 1 of 1 (latest)

bitter shell
#

query prevent cache

teal pewterBOT
#

Hi @bitter shell. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I understand you're asking about preventing cache in Convex queries. Let me explain how caching works in Convex and how you might control it.

Understanding Convex Query Caching

Convex automatically caches query results, which is one of its key features. When multiple clients request the same query with the same arguments, they receive a cached response, improving performance and reducing database load The Zen of Convex.

When Cache Gets Invalidated

The cache is automatically invalidated when:

  1. The underlying data referenced in the query changes
  2. The query arguments change

Strategies to Control Caching

If you need to prevent caching or control when the cache is invalidated, here are some approaches:

1. Separate Frequently Changing Data

If you have data that changes frequently and invalidates your cache too often:

// OPTIMIZED: invalidates a "heartbeat" document referenced in fewer queries.
// When the user reports a heartbeat, update their related heartbeat document.
await ctx.db.patch(user.heartbeatId, { lastSeen: Date.now() });
// In any query that cares about online status:
const heartbeat = await ctx.db.get(user.heartbeatId);
const tooOld = Date.now() - HEARTBEAT_TOO_OLD;
const online = heartbeat ? tooOld < heartbeat.lastSeen : false; 

This approach separates frequently changing data into dedicated documents that are only referenced by queries that need that specific data Queries that scale.

2. Avoid Sprawling Session Documents

Don't store unrelated data in the same document if different parts update at different frequencies:

// BAD: Storing everything in one session document
// If any part changes, all queries reading this document get invalidated
#

// GOOD: Separate documents for different features
// heartbeats table for online status
// shopping_carts table for cart data
// presence table for cursor position


This prevents cache invalidation for unrelated features [Track sessions without cookies](<https://stack.convex.dev/track-sessions-without-cookies#avoid-sprawling-session-documents>).

### 3. Using TanStack Query's gcTime

If you're using TanStack Query with Convex, you can control how long queries stay subscribed with the `gcTime` option:

```javascript
// Default is 5 minutes
useQuery(api.myQuery, args, { gcTime: 30000 }); // 30 seconds

This controls how long the query remains subscribed after it's no longer being used Discord support thread.

Remember that Convex's caching is a feature, not a bug. It's designed to make your application more efficient and responsive. In most cases, you should work with the caching system rather than against it.

I hope this helps! Let me know if you have more specific questions about query caching in Convex.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: