Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 6762e99

Browse files
committed
Move the memory cache to the test
1 parent 076fa00 commit 6762e99

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

‎httpcache.go‎

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"net/http"
1717
"net/http/httputil"
1818
"strings"
19-
"sync"
2019
"time"
2120
)
2221

@@ -78,46 +77,6 @@ func (t *Transport) cachedResponse(req *http.Request) (resp *http.Response, err
7877
return http.ReadResponse(bufio.NewReader(b), req)
7978
}
8079

81-
// memoryCache is an implemtation of Cache that stores responses in an in-memory map.
82-
type memoryCache struct {
83-
mu sync.RWMutex
84-
items map[string][]byte
85-
}
86-
87-
func (c *memoryCache) Size() int {
88-
c.mu.RLock()
89-
defer c.mu.RUnlock()
90-
return len(c.items)
91-
}
92-
93-
// Get returns the []byte representation of the response and true if present, false if not
94-
func (c *memoryCache) Get(key string) (resp []byte, ok bool) {
95-
c.mu.RLock()
96-
resp, ok = c.items[key]
97-
c.mu.RUnlock()
98-
return resp, ok
99-
}
100-
101-
// Set saves response resp to the cache with key
102-
func (c *memoryCache) Set(key string, resp []byte) {
103-
c.mu.Lock()
104-
c.items[key] = resp
105-
c.mu.Unlock()
106-
}
107-
108-
// Delete removes key from the cache
109-
func (c *memoryCache) Delete(key string) {
110-
c.mu.Lock()
111-
delete(c.items, key)
112-
c.mu.Unlock()
113-
}
114-
115-
// newMemoryCache returns a new Cache that will store items in an in-memory map
116-
func newMemoryCache() *memoryCache {
117-
c := &memoryCache{items: map[string][]byte{}}
118-
return c
119-
}
120-
12180
// Transport is an implementation of http.RoundTripper that will return values from a cache
12281
// where possible (avoiding a network request) and will additionally add validators (etag/if-modified-since)
12382
// to repeated requests allowing servers to return 304 / Not Modified
@@ -617,10 +576,3 @@ func (r *cachingReadCloser) Read(p []byte) (n int, err error) {
617576
func (r *cachingReadCloser) Close() error {
618577
return r.R.Close()
619578
}
620-
621-
// newMemoryCacheTransport returns a new Transport using the in-memory cache implementation
622-
func newMemoryCacheTransport() *Transport {
623-
c := newMemoryCache()
624-
t := &Transport{Cache: c}
625-
return t
626-
}

‎httpcache_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http/httptest"
1010
"os"
1111
"strconv"
12+
"sync"
1213
"testing"
1314
"time"
1415

@@ -1325,3 +1326,50 @@ func doMethod(t testing.TB, method string, p string, headers map[string]string)
13251326

13261327
return buf.String(), resp
13271328
}
1329+
1330+
// newMemoryCacheTransport returns a new Transport using the in-memory cache implementation
1331+
func newMemoryCacheTransport() *Transport {
1332+
c := newMemoryCache()
1333+
t := &Transport{Cache: c}
1334+
return t
1335+
}
1336+
1337+
// memoryCache is an implemtation of Cache that stores responses in an in-memory map.
1338+
type memoryCache struct {
1339+
mu sync.RWMutex
1340+
items map[string][]byte
1341+
}
1342+
1343+
// newMemoryCache returns a new Cache that will store items in an in-memory map
1344+
func newMemoryCache() *memoryCache {
1345+
c := &memoryCache{items: map[string][]byte{}}
1346+
return c
1347+
}
1348+
1349+
func (c *memoryCache) Size() int {
1350+
c.mu.RLock()
1351+
defer c.mu.RUnlock()
1352+
return len(c.items)
1353+
}
1354+
1355+
// Get returns the []byte representation of the response and true if present, false if not
1356+
func (c *memoryCache) Get(key string) (resp []byte, ok bool) {
1357+
c.mu.RLock()
1358+
resp, ok = c.items[key]
1359+
c.mu.RUnlock()
1360+
return resp, ok
1361+
}
1362+
1363+
// Set saves response resp to the cache with key
1364+
func (c *memoryCache) Set(key string, resp []byte) {
1365+
c.mu.Lock()
1366+
c.items[key] = resp
1367+
c.mu.Unlock()
1368+
}
1369+
1370+
// Delete removes key from the cache
1371+
func (c *memoryCache) Delete(key string) {
1372+
c.mu.Lock()
1373+
delete(c.items, key)
1374+
c.mu.Unlock()
1375+
}

0 commit comments

Comments
 (0)