Skip to content

Commit

Permalink
Move the memory cache to the test
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 20, 2024
1 parent 076fa00 commit 6762e99
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
48 changes: 0 additions & 48 deletions httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"net/http"
"net/http/httputil"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
48 changes: 48 additions & 0 deletions httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httptest"
"os"
"strconv"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -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()
}

0 comments on commit 6762e99

Please sign in to comment.