MCPcopy
hub / github.com/dosco/graphjin / Set

Method Set

serv/mcp_cursor_cache.go:66–103  ·  view source on GitHub ↗

Set stores a cursor and returns a short numeric ID

(ctx context.Context, cursor string)

Source from the content-addressed store, hash-verified

64
65// Set stores a cursor and returns a short numeric ID
66func (c *RedisCursorCache) Set(ctx context.Context, cursor string) (uint64, error) {
67 ctx, cancel := context.WithTimeout(ctx, cursorRedisTimeout)
68 defer cancel()
69
70 // Check if cursor already exists (deduplication)
71 hash := hashCursor(cursor)
72 revKey := cursorRevKey + hash
73
74 // Try to get existing ID
75 existingID, err := c.client.Get(ctx, revKey).Uint64()
76 if err == nil {
77 // Refresh TTL on existing entries
78 pipe := c.client.Pipeline()
79 pipe.Expire(ctx, revKey, c.ttl)
80 pipe.Expire(ctx, cursorIDKey+fmt.Sprintf("%d", existingID), c.ttl)
81 pipe.Exec(ctx) //nolint:errcheck
82 return existingID, nil
83 }
84
85 // Generate new ID atomically
86 id, err := c.client.Incr(ctx, cursorNextIDKey).Uint64()
87 if err != nil {
88 return 0, fmt.Errorf("failed to generate cursor ID: %w", err)
89 }
90
91 idKey := cursorIDKey + fmt.Sprintf("%d", id)
92
93 // Store both mappings
94 pipe := c.client.Pipeline()
95 pipe.Set(ctx, idKey, cursor, c.ttl)
96 pipe.Set(ctx, revKey, id, c.ttl)
97 _, err = pipe.Exec(ctx)
98 if err != nil {
99 return 0, fmt.Errorf("failed to store cursor: %w", err)
100 }
101
102 return id, nil
103}
104
105// Get retrieves a cursor by its numeric ID
106func (c *RedisCursorCache) Get(ctx context.Context, id uint64) (string, error) {

Callers

nothing calls this directly

Calls 5

hashCursorFunction · 0.85
GetMethod · 0.65
ExpireMethod · 0.65
ExecMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected