Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit 1cd4384

Browse files
authored
client: add support for v1/namespaces/:namespace/hint_cache_warm (#15)
1 parent 76ce419 commit 1cd4384

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

warm.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package tpuf
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
type WarmCacheResult struct {
10+
Status string `json:"status"`
11+
Message string `json:"message"`
12+
}
13+
14+
// Hints to turbopuffer that it should warm the hint cache for the given namespace.
15+
// See https://turbopuffer.com/docs/warm-cache for more details.
16+
func (c *Client) WarmCache(ctx context.Context, namespace string) (*WarmCacheResult, error) {
17+
path := fmt.Sprintf("/v1/namespaces/%s/hint_cache_warm", namespace)
18+
19+
respData, err := c.get(ctx, path, nil)
20+
if err != nil {
21+
return nil, fmt.Errorf("failed to warm cache: %w", err)
22+
}
23+
24+
var warmCacheResult WarmCacheResult
25+
if err := json.Unmarshal(respData, &warmCacheResult); err != nil {
26+
return nil, fmt.Errorf("failed to decode response: %w", err)
27+
}
28+
29+
return &warmCacheResult, nil
30+
}

warm_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package tpuf_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"io"
7+
"net/http"
8+
"testing"
9+
10+
"github.com/bamo/tpuf-go"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestWarmCache(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
namespace string
18+
httpResponse *http.Response
19+
httpError error
20+
expectedError string
21+
expectedMethod string
22+
expectedURL string
23+
expectedResult *tpuf.WarmCacheResult
24+
}{
25+
{
26+
name: "successful warm cache",
27+
namespace: "test-namespace",
28+
httpResponse: &http.Response{
29+
StatusCode: http.StatusOK,
30+
Body: io.NopCloser(bytes.NewBufferString(`{
31+
"status": "ACCEPTED",
32+
"message": "cache starting to warm"
33+
}`)),
34+
},
35+
expectedMethod: http.MethodGet,
36+
expectedURL: "https://api.turbopuffer.com/v1/namespaces/test-namespace/hint_cache_warm",
37+
expectedResult: &tpuf.WarmCacheResult{
38+
Status: "ACCEPTED",
39+
Message: "cache starting to warm",
40+
},
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
client := &tpuf.Client{
47+
ApiToken: "test-token",
48+
HttpClient: &fakeHttpClient{
49+
doFunc: func(req *http.Request) (*http.Response, error) {
50+
assert.Equal(t, tt.expectedMethod, req.Method, "unexpected request method")
51+
assert.Equal(t, tt.expectedURL, req.URL.String(), "unexpected request URL")
52+
53+
return tt.httpResponse, tt.httpError
54+
},
55+
},
56+
}
57+
58+
result, err := client.WarmCache(context.Background(), tt.namespace)
59+
if tt.expectedError == "" {
60+
assert.NoError(t, err)
61+
assert.Equal(t, tt.expectedResult, result, "unexpected warm cache result")
62+
} else {
63+
assert.EqualError(t, err, tt.expectedError)
64+
assert.Nil(t, result)
65+
}
66+
})
67+
}
68+
}

0 commit comments

Comments
 (0)