Skip to content

Commit a5bd9f5

Browse files
committed
allow cache injection again
1 parent 6f1f64a commit a5bd9f5

File tree

21 files changed

+73
-61
lines changed

21 files changed

+73
-61
lines changed

pkg/bilibili/bilibili.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ func AuthRequired() bool { return false }
3030
// Client handles Bilibili requests.
3131
type Client struct {
3232
httpClient *http.Client
33-
cache *httpcache.Cache
33+
cache httpcache.Cacher
3434
logger *slog.Logger
3535
}
3636

3737
// Option configures a Client.
3838
type Option func(*config)
3939

4040
type config struct {
41-
cache *httpcache.Cache
41+
cache httpcache.Cacher
4242
logger *slog.Logger
4343
}
4444

4545
// WithHTTPCache sets the HTTP cache.
46-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
46+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
4747
return func(c *config) { c.cache = httpCache }
4848
}
4949

pkg/bluesky/bluesky.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ func AuthRequired() bool { return false }
2929
// Client handles BlueSky requests.
3030
type Client struct {
3131
httpClient *http.Client
32-
cache *httpcache.Cache
32+
cache httpcache.Cacher
3333
logger *slog.Logger
3434
}
3535

3636
// Option configures a Client.
3737
type Option func(*config)
3838

3939
type config struct {
40-
cache *httpcache.Cache
40+
cache httpcache.Cacher
4141
logger *slog.Logger
4242
}
4343

4444
// WithHTTPCache sets the HTTP cache.
45-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
45+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
4646
return func(c *config) { c.cache = httpCache }
4747
}
4848

pkg/codeberg/codeberg.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ func AuthRequired() bool { return false }
4949
// Client handles Codeberg requests.
5050
type Client struct {
5151
httpClient *http.Client
52-
cache *httpcache.Cache
52+
cache httpcache.Cacher
5353
logger *slog.Logger
5454
}
5555

5656
// Option configures a Client.
5757
type Option func(*config)
5858

5959
type config struct {
60-
cache *httpcache.Cache
60+
cache httpcache.Cacher
6161
logger *slog.Logger
6262
}
6363

6464
// WithHTTPCache sets the HTTP cache.
65-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
65+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
6666
return func(c *config) { c.cache = httpCache }
6767
}
6868

pkg/devto/devto.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ func AuthRequired() bool { return false }
3131
// Client handles Dev.to requests.
3232
type Client struct {
3333
httpClient *http.Client
34-
cache *httpcache.Cache
34+
cache httpcache.Cacher
3535
logger *slog.Logger
3636
}
3737

3838
// Option configures a Client.
3939
type Option func(*config)
4040

4141
type config struct {
42-
cache *httpcache.Cache
42+
cache httpcache.Cacher
4343
logger *slog.Logger
4444
}
4545

4646
// WithHTTPCache sets the HTTP cache.
47-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
47+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
4848
return func(c *config) { c.cache = httpCache }
4949
}
5050

pkg/generic/generic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ func AuthRequired() bool { return false }
3434
// Client handles generic website requests.
3535
type Client struct {
3636
httpClient *http.Client
37-
cache *httpcache.Cache
37+
cache httpcache.Cacher
3838
logger *slog.Logger
3939
}
4040

4141
// Option configures a Client.
4242
type Option func(*config)
4343

4444
type config struct {
45-
cache *httpcache.Cache
45+
cache httpcache.Cacher
4646
logger *slog.Logger
4747
}
4848

4949
// WithHTTPCache sets the HTTP cache.
50-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
50+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
5151
return func(c *config) { c.cache = httpCache }
5252
}
5353

pkg/github/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func AuthRequired() bool { return false }
6767
// Client handles GitHub requests.
6868
type Client struct {
6969
httpClient *http.Client
70-
cache *httpcache.Cache
70+
cache httpcache.Cacher
7171
logger *slog.Logger
7272
token string
7373
}
@@ -76,13 +76,13 @@ type Client struct {
7676
type Option func(*config)
7777

7878
type config struct {
79-
cache *httpcache.Cache
79+
cache httpcache.Cacher
8080
logger *slog.Logger
8181
token string
8282
}
8383

8484
// WithHTTPCache sets the HTTP cache.
85-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
85+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
8686
return func(c *config) { c.cache = httpCache }
8787
}
8888

pkg/habr/habr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ func AuthRequired() bool { return false }
3939
// Client handles Habr requests.
4040
type Client struct {
4141
httpClient *http.Client
42-
cache *httpcache.Cache
42+
cache httpcache.Cacher
4343
logger *slog.Logger
4444
}
4545

4646
// Option configures a Client.
4747
type Option func(*config)
4848

4949
type config struct {
50-
cache *httpcache.Cache
50+
cache httpcache.Cacher
5151
logger *slog.Logger
5252
}
5353

5454
// WithHTTPCache sets the HTTP cache.
55-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
55+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
5656
return func(c *config) { c.cache = httpCache }
5757
}
5858

pkg/httpcache/httpcache.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ import (
2222
"github.com/codeGROOVE-dev/sfcache/pkg/persist/localfs"
2323
)
2424

25+
// Cacher is the interface that cache implementations must satisfy.
26+
// This allows external packages to provide their own cache implementation.
27+
type Cacher interface {
28+
GetSet(ctx context.Context, key string, fetch func(context.Context) ([]byte, error), ttl ...time.Duration) ([]byte, error)
29+
TTL() time.Duration
30+
}
31+
2532
// Cache wraps sfcache for HTTP response caching.
2633
type Cache struct {
2734
*sfcache.TieredCache[string, []byte]
@@ -57,6 +64,11 @@ func NewWithPath(ttl time.Duration, cachePath string) (*Cache, error) {
5764
return &Cache{TieredCache: tc, ttl: ttl}, nil
5865
}
5966

67+
// TTL returns the default TTL for cache entries.
68+
func (c *Cache) TTL() time.Duration {
69+
return c.ttl
70+
}
71+
6072
// URLToKey converts a URL to a cache key using SHA256 hash.
6173
func URLToKey(rawURL string) string {
6274
hash := sha256.Sum256([]byte(rawURL))
@@ -78,15 +90,15 @@ type ResponseValidator func(body []byte) bool
7890

7991
// FetchURL fetches a URL with caching and thundering herd prevention.
8092
// If cache is non-nil, uses GetSet to ensure only one request is made for concurrent calls.
81-
func FetchURL(ctx context.Context, cache *Cache, client *http.Client, req *http.Request, logger *slog.Logger) ([]byte, error) {
93+
func FetchURL(ctx context.Context, cache Cacher, client *http.Client, req *http.Request, logger *slog.Logger) ([]byte, error) {
8294
return FetchURLWithValidator(ctx, cache, client, req, logger, nil)
8395
}
8496

8597
// FetchURLWithValidator fetches a URL with caching and optional response validation.
8698
// If validator returns false, the response is returned but NOT cached.
8799
func FetchURLWithValidator(
88100
ctx context.Context,
89-
cache *Cache,
101+
cache Cacher,
90102
client *http.Client,
91103
req *http.Request,
92104
logger *slog.Logger,
@@ -123,7 +135,7 @@ func FetchURLWithValidator(
123135
return nil, &validationError{data: body}
124136
}
125137
return body, nil
126-
}, cache.ttl)
138+
}, cache.TTL())
127139

128140
// Handle validation failure - return the data but it wasn't cached.
129141
var validErr *validationError

pkg/linkedin/linkedin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Option func(*config)
3434

3535
type config struct {
3636
cookies map[string]string
37-
cache *httpcache.Cache
37+
cache httpcache.Cacher
3838
logger *slog.Logger
3939
browserCookies bool
4040
}
@@ -45,7 +45,7 @@ func WithCookies(cookies map[string]string) Option {
4545
}
4646

4747
// WithHTTPCache sets the HTTP cache (currently unused - auth is broken).
48-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
48+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
4949
return func(c *config) { c.cache = httpCache }
5050
}
5151

pkg/linktree/linktree.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ func AuthRequired() bool { return false }
3030
// Client handles Linktree requests.
3131
type Client struct {
3232
httpClient *http.Client
33-
cache *httpcache.Cache
33+
cache httpcache.Cacher
3434
logger *slog.Logger
3535
}
3636

3737
// Option configures a Client.
3838
type Option func(*config)
3939

4040
type config struct {
41-
cache *httpcache.Cache
41+
cache httpcache.Cacher
4242
logger *slog.Logger
4343
}
4444

4545
// WithHTTPCache sets the HTTP cache.
46-
func WithHTTPCache(httpCache *httpcache.Cache) Option {
46+
func WithHTTPCache(httpCache httpcache.Cacher) Option {
4747
return func(c *config) { c.cache = httpCache }
4848
}
4949

0 commit comments

Comments
 (0)