Documentation
¶
Index ¶
- type Handler
- func (h *Handler) CacheKey(f func(*http.Request) string) *Handler
- func (h *Handler) OnError(f func(error, http.ResponseWriter, *http.Request)) *Handler
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Handler) TTL(f func(*http.Request) time.Duration) *Handler
- func (h *Handler) Transform(f func(*http.Request) (*http.Request, error)) *Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides a fluent API for creating cache-backed HTTP handlers.
Example usage:
h := handler.New(client, cache).
CacheKey(func(r *http.Request) string {
return "custom-key"
}).
Transform(func(r *http.Request) (*http.Request, error) {
// Modify request before fetching
return modifiedRequest, nil
})
func New ¶
New creates a new Handler with the given HTTP client and cache. By default: - Cache key is derived from the request URL - No request transformation is performed - Standard error handling is used.
func (*Handler) CacheKey ¶
CacheKey sets the function used to determine the cache key for a request. The function receives the original incoming request.
func (*Handler) OnError ¶
OnError sets a custom error handler for the built handler. If not set, a default error handler is used.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler. The handler will: 1. Determine the cache key using the configured function 2. Check if the content exists in cache 3. If cached, stream from cache 4. If not cached, transform the request and fetch from upstream 5. Cache the response while streaming to the client.
func (*Handler) TTL ¶
TTL sets the function used to determine the cache TTL for a request. The function receives the original incoming request. If not set or returns 0, the cache's default/maximum TTL is used.