From 6762e9902430c5319c9c58ada6b992fd0836e3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 20 May 2024 20:02:28 +0200 Subject: [PATCH] Move the memory cache to the test --- httpcache.go | 48 ----------------------------------------------- httpcache_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/httpcache.go b/httpcache.go index cd0e1f0..fa6d9b9 100644 --- a/httpcache.go +++ b/httpcache.go @@ -16,7 +16,6 @@ import ( "net/http" "net/http/httputil" "strings" - "sync" "time" ) @@ -78,46 +77,6 @@ func (t *Transport) cachedResponse(req *http.Request) (resp *http.Response, err return http.ReadResponse(bufio.NewReader(b), req) } -// memoryCache is an implemtation of Cache that stores responses in an in-memory map. -type memoryCache struct { - mu sync.RWMutex - items map[string][]byte -} - -func (c *memoryCache) Size() int { - c.mu.RLock() - defer c.mu.RUnlock() - return len(c.items) -} - -// Get returns the []byte representation of the response and true if present, false if not -func (c *memoryCache) Get(key string) (resp []byte, ok bool) { - c.mu.RLock() - resp, ok = c.items[key] - c.mu.RUnlock() - return resp, ok -} - -// Set saves response resp to the cache with key -func (c *memoryCache) Set(key string, resp []byte) { - c.mu.Lock() - c.items[key] = resp - c.mu.Unlock() -} - -// Delete removes key from the cache -func (c *memoryCache) Delete(key string) { - c.mu.Lock() - delete(c.items, key) - c.mu.Unlock() -} - -// newMemoryCache returns a new Cache that will store items in an in-memory map -func newMemoryCache() *memoryCache { - c := &memoryCache{items: map[string][]byte{}} - return c -} - // Transport is an implementation of http.RoundTripper that will return values from a cache // where possible (avoiding a network request) and will additionally add validators (etag/if-modified-since) // to repeated requests allowing servers to return 304 / Not Modified @@ -617,10 +576,3 @@ func (r *cachingReadCloser) Read(p []byte) (n int, err error) { func (r *cachingReadCloser) Close() error { return r.R.Close() } - -// newMemoryCacheTransport returns a new Transport using the in-memory cache implementation -func newMemoryCacheTransport() *Transport { - c := newMemoryCache() - t := &Transport{Cache: c} - return t -} diff --git a/httpcache_test.go b/httpcache_test.go index 6359f01..5d528e3 100644 --- a/httpcache_test.go +++ b/httpcache_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "os" "strconv" + "sync" "testing" "time" @@ -1325,3 +1326,50 @@ func doMethod(t testing.TB, method string, p string, headers map[string]string) return buf.String(), resp } + +// newMemoryCacheTransport returns a new Transport using the in-memory cache implementation +func newMemoryCacheTransport() *Transport { + c := newMemoryCache() + t := &Transport{Cache: c} + return t +} + +// memoryCache is an implemtation of Cache that stores responses in an in-memory map. +type memoryCache struct { + mu sync.RWMutex + items map[string][]byte +} + +// newMemoryCache returns a new Cache that will store items in an in-memory map +func newMemoryCache() *memoryCache { + c := &memoryCache{items: map[string][]byte{}} + return c +} + +func (c *memoryCache) Size() int { + c.mu.RLock() + defer c.mu.RUnlock() + return len(c.items) +} + +// Get returns the []byte representation of the response and true if present, false if not +func (c *memoryCache) Get(key string) (resp []byte, ok bool) { + c.mu.RLock() + resp, ok = c.items[key] + c.mu.RUnlock() + return resp, ok +} + +// Set saves response resp to the cache with key +func (c *memoryCache) Set(key string, resp []byte) { + c.mu.Lock() + c.items[key] = resp + c.mu.Unlock() +} + +// Delete removes key from the cache +func (c *memoryCache) Delete(key string) { + c.mu.Lock() + delete(c.items, key) + c.mu.Unlock() +}