Set stores a cursor and returns a short numeric ID
(ctx context.Context, cursor string)
| 64 | |
| 65 | // Set stores a cursor and returns a short numeric ID |
| 66 | func (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 |
| 106 | func (c *RedisCursorCache) Get(ctx context.Context, id uint64) (string, error) { |
nothing calls this directly
no test coverage detected