@@ -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.
2633type 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.
6173func 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.
8799func 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
0 commit comments