diff options
| author | Michael Matloob <matloob@golang.org> | 2023-07-31 18:24:14 -0400 |
|---|---|---|
| committer | Michael Matloob <matloob@golang.org> | 2023-08-04 19:24:02 +0000 |
| commit | 4434dd5c09d0bc36b9f8c6d94d092d88cbdfcf2e (patch) | |
| tree | 17f6566721bb9d8cc3342151d3a812926e269560 /internal/frontend/server.go | |
| parent | 7ec3a4ee639385cfe278ad194c77be9484a330c9 (diff) | |
| download | go-x-pkgsite-4434dd5c09d0bc36b9f8c6d94d092d88cbdfcf2e.tar.xz | |
internal/frontend: add an interface for creating request caches
This change adds a new Cacher interface that is used to create
middlewares for caching requests. This abstracts away the use of redis
so that the frontend doesn't depend on redis. The tests still depend
on redis for the 404 page testing logic, but the 404 page logic will
be moved out into a different package so those tests will go too.
The Expirer and Middleware interfaces are not present on the Cache
function so that the interface can be defined in package
internal/frontend without needing the dependency on the Middleware
package.
For golang/go#61399
Change-Id: I6518b2ed1d772cb4deda3308c4190f0f1b8a35a0
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/514518
kokoro-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Diffstat (limited to 'internal/frontend/server.go')
| -rw-r--r-- | internal/frontend/server.go | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/internal/frontend/server.go b/internal/frontend/server.go index a22ac999..4ec4ca50 100644 --- a/internal/frontend/server.go +++ b/internal/frontend/server.go @@ -22,7 +22,6 @@ import ( "time" "cloud.google.com/go/errorreporting" - "github.com/go-redis/redis/v8" "github.com/google/safehtml" "github.com/google/safehtml/template" "golang.org/x/pkgsite/internal" @@ -127,24 +126,36 @@ func NewServer(scfg ServerConfig) (_ *Server, err error) { return s, nil } +// A Cacher is used to create request caches for http handlers. +type Cacher interface { + // Cache returns a new middleware that caches every request. + // The name of the cache is used only for metrics. + // The expirer is a func that is used to map a new request to its TTL. + // authHeader is the header key used by the cache to know that a + // request should bypass the cache. + // authValues is the set of values that could be set on the authHeader in + // order to bypass the cache. + Cache(name string, expirer func(r *http.Request) time.Duration, authValues []string) func(http.Handler) http.Handler +} + // Install registers server routes using the given handler registration func. // authValues is the set of values that can be set on authHeader to bypass the // cache. -func (s *Server) Install(handle func(string, http.Handler), redisClient *redis.Client, authValues []string) { +func (s *Server) Install(handle func(string, http.Handler), cacher Cacher, authValues []string) { var ( detailHandler http.Handler = s.errorHandler(s.serveDetails) fetchHandler http.Handler = s.errorHandler(s.serveFetch) searchHandler http.Handler = s.errorHandler(s.serveSearch) vulnHandler http.Handler = s.errorHandler(s.serveVuln) ) - if redisClient != nil { + if cacher != nil { // The cache middleware uses the URL string as the key for content served // by the handlers it wraps. Be careful not to wrap the handler it returns // with a handler that rewrites the URL in a way that could cause key // collisions, like http.StripPrefix. - detailHandler = middleware.Cache("details", redisClient, detailsTTL, authValues)(detailHandler) - searchHandler = middleware.Cache("search", redisClient, searchTTL, authValues)(searchHandler) - vulnHandler = middleware.Cache("vuln", redisClient, vulnTTL, authValues)(vulnHandler) + detailHandler = cacher.Cache("details", detailsTTL, authValues)(detailHandler) + searchHandler = cacher.Cache("search", searchTTL, authValues)(searchHandler) + vulnHandler = cacher.Cache("vuln", vulnTTL, authValues)(vulnHandler) } // Each AppEngine instance is created in response to a start request, which // is an empty HTTP GET request to /_ah/start when scaling is set to manual |
