diff options
| author | Michael Matloob <matloob@golang.org> | 2023-08-30 14:15:02 -0400 |
|---|---|---|
| committer | Michael Matloob <matloob@golang.org> | 2023-08-30 20:21:49 +0000 |
| commit | 06e167958cf0d098b75f39cd9b570fd134c4379e (patch) | |
| tree | d713476bad764ae3c1a88f70403ec7e8c08efbf1 /internal/fetchdatasource/fetchdatasource.go | |
| parent | 6737fb01fe8bee1de75e4cb9fad2b25bc96ceb2a (diff) | |
| download | go-x-pkgsite-06e167958cf0d098b75f39cd9b570fd134c4379e.tar.xz | |
internal/lru: add a really simple LRU cache implementation
To replace github.com/hashicorp/golang-lru. The size used with the
cache in fetchdatasource is 100, so the efficiency of the cache is not
super important.
For golang/go#61399
Change-Id: I48383a4d1c00c4d153c0bad7b0a1a44c026b2314
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/524458
TryBot-Result: Gopher Robot <gobot@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'internal/fetchdatasource/fetchdatasource.go')
| -rw-r--r-- | internal/fetchdatasource/fetchdatasource.go | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/internal/fetchdatasource/fetchdatasource.go b/internal/fetchdatasource/fetchdatasource.go index 8aef0a69..88ab1382 100644 --- a/internal/fetchdatasource/fetchdatasource.go +++ b/internal/fetchdatasource/fetchdatasource.go @@ -16,12 +16,12 @@ import ( "strings" "time" - lru "github.com/hashicorp/golang-lru" "golang.org/x/mod/semver" "golang.org/x/pkgsite/internal" "golang.org/x/pkgsite/internal/derrors" "golang.org/x/pkgsite/internal/fetch" "golang.org/x/pkgsite/internal/log" + "golang.org/x/pkgsite/internal/lru" "golang.org/x/pkgsite/internal/proxy" "golang.org/x/pkgsite/internal/version" ) @@ -30,7 +30,7 @@ import ( // fetch.ModuleGetters to fetch modules and caching the results. type FetchDataSource struct { opts Options - cache *lru.Cache + cache *lru.Cache[internal.Modver, cacheEntry] } // Options are parameters for creating a new FetchDataSource. @@ -45,11 +45,8 @@ type Options struct { // New creates a new FetchDataSource from the options. func (o Options) New() *FetchDataSource { - cache, err := lru.New(maxCachedModules) - if err != nil { - // Can only happen if size is bad, and we control it. - panic(err) - } + cache := lru.New[internal.Modver, cacheEntry](maxCachedModules) + opts := o // Copy getters slice so caller doesn't modify us. opts.Getters = make([]fetch.ModuleGetter, len(opts.Getters)) @@ -75,7 +72,6 @@ func (ds *FetchDataSource) cacheGet(path, version string) (fetch.ModuleGetter, * // directory-based or GOPATH-mode module. for _, v := range []string{version, fetch.LocalVersion} { if e, ok := ds.cache.Get(internal.Modver{Path: path, Version: v}); ok { - e := e.(cacheEntry) return e.g, e.module, e.err } } @@ -84,7 +80,7 @@ func (ds *FetchDataSource) cacheGet(path, version string) (fetch.ModuleGetter, * // cachePut puts information into the cache. func (ds *FetchDataSource) cachePut(g fetch.ModuleGetter, path, version string, m *internal.Module, err error) { - ds.cache.Add(internal.Modver{Path: path, Version: version}, cacheEntry{g, m, err}) + ds.cache.Put(internal.Modver{Path: path, Version: version}, cacheEntry{g, m, err}) } // getModule gets the module at the given path and version. It first checks the |
