From e6a587463cedf8a153a08a3a2fb484642033e60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 17 May 2024 13:34:40 +0200 Subject: [PATCH] Remove unsed, add fork info, run gomod init etc. --- .github/workflows/test.yml | 37 +++++++++++++++++++ .travis.yml | 18 --------- LICENSE.txt | 3 +- README.md | 27 ++------------ diskcache/diskcache.go | 61 ------------------------------- diskcache/diskcache_test.go | 19 ---------- go.mod | 3 ++ httpcache.go | 6 +-- httpcache_test.go | 41 ++++++++++----------- leveldbcache/leveldbcache.go | 51 -------------------------- leveldbcache/leveldbcache_test.go | 25 ------------- memcache/appengine.go | 61 ------------------------------- memcache/appengine_test.go | 20 ---------- memcache/memcache.go | 60 ------------------------------ memcache/memcache_test.go | 24 ------------ redis/redis.go | 43 ---------------------- redis/redis_test.go | 19 ---------- test/test.go | 35 ------------------ test/test_test.go | 12 ------ 19 files changed, 68 insertions(+), 497 deletions(-) create mode 100644 .github/workflows/test.yml delete mode 100644 .travis.yml delete mode 100644 diskcache/diskcache.go delete mode 100644 diskcache/diskcache_test.go create mode 100644 go.mod delete mode 100644 leveldbcache/leveldbcache.go delete mode 100644 leveldbcache/leveldbcache_test.go delete mode 100644 memcache/appengine.go delete mode 100644 memcache/appengine_test.go delete mode 100644 memcache/memcache.go delete mode 100644 memcache/memcache_test.go delete mode 100644 redis/redis.go delete mode 100644 redis/redis_test.go delete mode 100644 test/test.go delete mode 100644 test/test_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..81888ff --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,37 @@ +on: + push: + branches: [master] + pull_request: +name: Test +permissions: + contents: read +jobs: + test: + strategy: + matrix: + go-version: [1.21.x, 1.22.x] + platform: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.platform }} + steps: + - name: Install Go + uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 + with: + go-version: ${{ matrix.go-version }} + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + shell: bash + - name: Update PATH + run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH + shell: bash + - name: Checkout code + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + - name: Fmt + if: matrix.platform != 'windows-latest' + run: "diff <(gofmt -d .) <(printf '')" + shell: bash + - name: Vet + run: go vet ./... + - name: Staticcheck + run: staticcheck ./... + - name: Test + run: go test -race ./... -coverpkg=./... diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 597bc99..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -sudo: false -language: go -matrix: - allow_failures: - - go: master - fast_finish: true - include: - - go: 1.10.x - - go: 1.11.x - env: GOFMT=1 - - go: master -install: - - # Do nothing. This is needed to prevent default install action "go get -t -v ./..." from happening here (we want it to happen inside script step). -script: - - go get -t -v ./... - - if test -n "${GOFMT}"; then gofmt -w -s . && git diff --exit-code; fi - - go tool vet . - - go test -v -race ./... diff --git a/LICENSE.txt b/LICENSE.txt index 81316be..b77704c 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,5 @@ -Copyright © 2012 Greg Jones (greg.jones@gmail.com) +Original work Copyright © 2012 Greg Jones (greg.jones@gmail.com) +Modified work Copyright © 2024 The Hugo Authors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 51e7d23..4a24af6 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,8 @@ -httpcache -========= +[![Tests on Linux, MacOS and Windows](https://github.com/gohugoio/httpcache/workflows/Test/badge.svg)](https://github.com/gohugoio/httpcache/actions?query=workflow:Test) +[![Go Report Card](https://goreportcard.com/badge/github.com/gohugoio/httpcache)](https://goreportcard.com/report/github.com/gohugoio/httpcache) +[![GoDoc](https://godoc.org/github.com/gohugoio/httpcache?status.svg)](https://godoc.org/github.com/gohugoio/httpcache) -[![Build Status](https://travis-ci.org/gregjones/httpcache.svg?branch=master)](https://travis-ci.org/gregjones/httpcache) [![GoDoc](https://godoc.org/github.com/gregjones/httpcache?status.svg)](https://godoc.org/github.com/gregjones/httpcache) - -Package httpcache provides a http.RoundTripper implementation that works as a mostly [RFC 7234](https://tools.ietf.org/html/rfc7234) compliant cache for http responses. - -It is only suitable for use as a 'private' cache (i.e. for a web-browser or an API-client and not for a shared proxy). - -This project isn't actively maintained; it works for what I, and seemingly others, want to do with it, and I consider it "done". That said, if you find any issues, please open a Pull Request and I will try to review it. Any changes now that change the public API won't be considered. - -Cache Backends --------------- - -- The built-in 'memory' cache stores responses in an in-memory map. -- [`github.com/gregjones/httpcache/diskcache`](https://github.com/gregjones/httpcache/tree/master/diskcache) provides a filesystem-backed cache using the [diskv](https://github.com/peterbourgon/diskv) library. -- [`github.com/gregjones/httpcache/memcache`](https://github.com/gregjones/httpcache/tree/master/memcache) provides memcache implementations, for both App Engine and 'normal' memcache servers. -- [`sourcegraph.com/sourcegraph/s3cache`](https://sourcegraph.com/github.com/sourcegraph/s3cache) uses Amazon S3 for storage. -- [`github.com/gregjones/httpcache/leveldbcache`](https://github.com/gregjones/httpcache/tree/master/leveldbcache) provides a filesystem-backed cache using [leveldb](https://github.com/syndtr/goleveldb/leveldb). -- [`github.com/die-net/lrucache`](https://github.com/die-net/lrucache) provides an in-memory cache that will evict least-recently used entries. -- [`github.com/die-net/lrucache/twotier`](https://github.com/die-net/lrucache/tree/master/twotier) allows caches to be combined, for example to use lrucache above with a persistent disk-cache. -- [`github.com/birkelund/boltdbcache`](https://github.com/birkelund/boltdbcache) provides a BoltDB implementation (based on the [bbolt](https://github.com/coreos/bbolt) fork). - -If you implement any other backend and wish it to be linked here, please send a PR editing this file. +This is a fork of [gregjones/httpcache](https://github.com/gregjones/httpcache). License ------- diff --git a/diskcache/diskcache.go b/diskcache/diskcache.go deleted file mode 100644 index 42e3129..0000000 --- a/diskcache/diskcache.go +++ /dev/null @@ -1,61 +0,0 @@ -// Package diskcache provides an implementation of httpcache.Cache that uses the diskv package -// to supplement an in-memory map with persistent storage -// -package diskcache - -import ( - "bytes" - "crypto/md5" - "encoding/hex" - "github.com/peterbourgon/diskv" - "io" -) - -// Cache is an implementation of httpcache.Cache that supplements the in-memory map with persistent storage -type Cache struct { - d *diskv.Diskv -} - -// Get returns the response corresponding to key if present -func (c *Cache) Get(key string) (resp []byte, ok bool) { - key = keyToFilename(key) - resp, err := c.d.Read(key) - if err != nil { - return []byte{}, false - } - return resp, true -} - -// Set saves a response to the cache as key -func (c *Cache) Set(key string, resp []byte) { - key = keyToFilename(key) - c.d.WriteStream(key, bytes.NewReader(resp), true) -} - -// Delete removes the response with key from the cache -func (c *Cache) Delete(key string) { - key = keyToFilename(key) - c.d.Erase(key) -} - -func keyToFilename(key string) string { - h := md5.New() - io.WriteString(h, key) - return hex.EncodeToString(h.Sum(nil)) -} - -// New returns a new Cache that will store files in basePath -func New(basePath string) *Cache { - return &Cache{ - d: diskv.New(diskv.Options{ - BasePath: basePath, - CacheSizeMax: 100 * 1024 * 1024, // 100MB - }), - } -} - -// NewWithDiskv returns a new Cache using the provided Diskv as underlying -// storage. -func NewWithDiskv(d *diskv.Diskv) *Cache { - return &Cache{d} -} diff --git a/diskcache/diskcache_test.go b/diskcache/diskcache_test.go deleted file mode 100644 index 98e168b..0000000 --- a/diskcache/diskcache_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package diskcache - -import ( - "io/ioutil" - "os" - "testing" - - "github.com/gregjones/httpcache/test" -) - -func TestDiskCache(t *testing.T) { - tempDir, err := ioutil.TempDir("", "httpcache") - if err != nil { - t.Fatalf("TempDir: %v", err) - } - defer os.RemoveAll(tempDir) - - test.Cache(t, New(tempDir)) -} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..222cca6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/gohugoio/httpcache + +go 1.22.2 diff --git a/httpcache.go b/httpcache.go index b41a63d..be4e7e8 100644 --- a/httpcache.go +++ b/httpcache.go @@ -3,7 +3,6 @@ // // It is only suitable for use as a 'private' cache (i.e. for a web-browser or an API-client // and not for a shared proxy). -// package httpcache import ( @@ -11,7 +10,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "net/http" "net/http/httputil" "strings" @@ -234,7 +232,7 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error R: resp.Body, OnEOF: func(r io.Reader) { resp := *resp - resp.Body = ioutil.NopCloser(r) + resp.Body = io.NopCloser(r) respBytes, err := httputil.DumpResponse(&resp, true) if err == nil { t.Cache.Set(cacheKey, respBytes) @@ -287,7 +285,7 @@ var clock timer = &realClock{} // transparent indicates the response should not be used to fulfil the request // // Because this is only a private cache, 'public' and 'private' in cache-control aren't -// signficant. Similarly, smax-age isn't used. +// significant. Similarly, smax-age isn't used. func getFreshness(respHeaders, reqHeaders http.Header) (freshness int) { respCacheControl := parseCacheControl(respHeaders) reqCacheControl := parseCacheControl(reqHeaders) diff --git a/httpcache_test.go b/httpcache_test.go index a504641..61ed4f8 100644 --- a/httpcache_test.go +++ b/httpcache_test.go @@ -5,7 +5,6 @@ import ( "errors" "flag" "io" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -432,7 +431,7 @@ func TestGetOnlyIfCachedHit(t *testing.T) { if resp.Header.Get(XFromCache) != "" { t.Fatal("XFromCache header isn't blank") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -549,7 +548,7 @@ func TestGetWithEtag(t *testing.T) { if resp.Header.Get(XFromCache) != "" { t.Fatal("XFromCache header isn't blank") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -589,7 +588,7 @@ func TestGetWithLastModified(t *testing.T) { if resp.Header.Get(XFromCache) != "" { t.Fatal("XFromCache header isn't blank") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -622,7 +621,7 @@ func TestGetWithVary(t *testing.T) { if resp.Header.Get("Vary") != "Accept" { t.Fatalf(`Vary header isn't "Accept": %v`, resp.Header.Get("Vary")) } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -678,7 +677,7 @@ func TestGetWithDoubleVary(t *testing.T) { if resp.Header.Get("Vary") == "" { t.Fatalf(`Vary header is blank`) } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -740,7 +739,7 @@ func TestGetWith2VaryHeaders(t *testing.T) { if resp.Header.Get("Vary") == "" { t.Fatalf(`Vary header is blank`) } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -799,7 +798,7 @@ func TestGetWith2VaryHeaders(t *testing.T) { if resp.Header.Get(XFromCache) != "" { t.Fatal("XFromCache header isn't blank") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -832,7 +831,7 @@ func TestGetVaryUnused(t *testing.T) { if resp.Header.Get("Vary") == "" { t.Fatalf(`Vary header is blank`) } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -863,7 +862,7 @@ func TestUpdateFields(t *testing.T) { } defer resp.Body.Close() counter = resp.Header.Get("x-counter") - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -899,7 +898,7 @@ func TestCachedErrorsKeepStatus(t *testing.T) { t.Fatal(err) } defer resp.Body.Close() - io.Copy(ioutil.Discard, resp.Body) + io.Copy(io.Discard, resp.Body) } { resp, err := s.client.Do(req) @@ -1203,7 +1202,7 @@ func TestStaleIfErrorRequest(t *testing.T) { "Date": []string{now.Format(time.RFC1123)}, "Cache-Control": []string{"no-cache"}, }, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte("some data"))), + Body: io.NopCloser(bytes.NewBuffer([]byte("some data"))), }, err: nil, } @@ -1220,7 +1219,7 @@ func TestStaleIfErrorRequest(t *testing.T) { if resp == nil { t.Fatal("resp is nil") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -1248,7 +1247,7 @@ func TestStaleIfErrorRequestLifetime(t *testing.T) { "Date": []string{now.Format(time.RFC1123)}, "Cache-Control": []string{"no-cache"}, }, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte("some data"))), + Body: io.NopCloser(bytes.NewBuffer([]byte("some data"))), }, err: nil, } @@ -1265,7 +1264,7 @@ func TestStaleIfErrorRequestLifetime(t *testing.T) { if resp == nil { t.Fatal("resp is nil") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -1311,7 +1310,7 @@ func TestStaleIfErrorResponse(t *testing.T) { "Date": []string{now.Format(time.RFC1123)}, "Cache-Control": []string{"no-cache, stale-if-error"}, }, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte("some data"))), + Body: io.NopCloser(bytes.NewBuffer([]byte("some data"))), }, err: nil, } @@ -1327,7 +1326,7 @@ func TestStaleIfErrorResponse(t *testing.T) { if resp == nil { t.Fatal("resp is nil") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -1355,7 +1354,7 @@ func TestStaleIfErrorResponseLifetime(t *testing.T) { "Date": []string{now.Format(time.RFC1123)}, "Cache-Control": []string{"no-cache, stale-if-error=100"}, }, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte("some data"))), + Body: io.NopCloser(bytes.NewBuffer([]byte("some data"))), }, err: nil, } @@ -1371,7 +1370,7 @@ func TestStaleIfErrorResponseLifetime(t *testing.T) { if resp == nil { t.Fatal("resp is nil") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -1409,7 +1408,7 @@ func TestStaleIfErrorKeepsStatus(t *testing.T) { "Date": []string{now.Format(time.RFC1123)}, "Cache-Control": []string{"no-cache"}, }, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte("some data"))), + Body: io.NopCloser(bytes.NewBuffer([]byte("some data"))), }, err: nil, } @@ -1426,7 +1425,7 @@ func TestStaleIfErrorKeepsStatus(t *testing.T) { if resp == nil { t.Fatal("resp is nil") } - _, err = ioutil.ReadAll(resp.Body) + _, err = io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } diff --git a/leveldbcache/leveldbcache.go b/leveldbcache/leveldbcache.go deleted file mode 100644 index 9bcb7e2..0000000 --- a/leveldbcache/leveldbcache.go +++ /dev/null @@ -1,51 +0,0 @@ -// Package leveldbcache provides an implementation of httpcache.Cache that -// uses github.com/syndtr/goleveldb/leveldb -package leveldbcache - -import ( - "github.com/syndtr/goleveldb/leveldb" -) - -// Cache is an implementation of httpcache.Cache with leveldb storage -type Cache struct { - db *leveldb.DB -} - -// Get returns the response corresponding to key if present -func (c *Cache) Get(key string) (resp []byte, ok bool) { - var err error - resp, err = c.db.Get([]byte(key), nil) - if err != nil { - return []byte{}, false - } - return resp, true -} - -// Set saves a response to the cache as key -func (c *Cache) Set(key string, resp []byte) { - c.db.Put([]byte(key), resp, nil) -} - -// Delete removes the response with key from the cache -func (c *Cache) Delete(key string) { - c.db.Delete([]byte(key), nil) -} - -// New returns a new Cache that will store leveldb in path -func New(path string) (*Cache, error) { - cache := &Cache{} - - var err error - cache.db, err = leveldb.OpenFile(path, nil) - - if err != nil { - return nil, err - } - return cache, nil -} - -// NewWithDB returns a new Cache using the provided leveldb as underlying -// storage. -func NewWithDB(db *leveldb.DB) *Cache { - return &Cache{db} -} diff --git a/leveldbcache/leveldbcache_test.go b/leveldbcache/leveldbcache_test.go deleted file mode 100644 index e301e7b..0000000 --- a/leveldbcache/leveldbcache_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package leveldbcache - -import ( - "io/ioutil" - "os" - "path/filepath" - "testing" - - "github.com/gregjones/httpcache/test" -) - -func TestDiskCache(t *testing.T) { - tempDir, err := ioutil.TempDir("", "httpcache") - if err != nil { - t.Fatalf("TempDir: %v", err) - } - defer os.RemoveAll(tempDir) - - cache, err := New(filepath.Join(tempDir, "db")) - if err != nil { - t.Fatalf("New leveldb,: %v", err) - } - - test.Cache(t, cache) -} diff --git a/memcache/appengine.go b/memcache/appengine.go deleted file mode 100644 index e68d9bc..0000000 --- a/memcache/appengine.go +++ /dev/null @@ -1,61 +0,0 @@ -// +build appengine - -// Package memcache provides an implementation of httpcache.Cache that uses App -// Engine's memcache package to store cached responses. -// -// When not built for Google App Engine, this package will provide an -// implementation that connects to a specified memcached server. See the -// memcache.go file in this package for details. -package memcache - -import ( - "appengine" - "appengine/memcache" -) - -// Cache is an implementation of httpcache.Cache that caches responses in App -// Engine's memcache. -type Cache struct { - appengine.Context -} - -// cacheKey modifies an httpcache key for use in memcache. Specifically, it -// prefixes keys to avoid collision with other data stored in memcache. -func cacheKey(key string) string { - return "httpcache:" + key -} - -// Get returns the response corresponding to key if present. -func (c *Cache) Get(key string) (resp []byte, ok bool) { - item, err := memcache.Get(c.Context, cacheKey(key)) - if err != nil { - if err != memcache.ErrCacheMiss { - c.Context.Errorf("error getting cached response: %v", err) - } - return nil, false - } - return item.Value, true -} - -// Set saves a response to the cache as key. -func (c *Cache) Set(key string, resp []byte) { - item := &memcache.Item{ - Key: cacheKey(key), - Value: resp, - } - if err := memcache.Set(c.Context, item); err != nil { - c.Context.Errorf("error caching response: %v", err) - } -} - -// Delete removes the response with key from the cache. -func (c *Cache) Delete(key string) { - if err := memcache.Delete(c.Context, cacheKey(key)); err != nil { - c.Context.Errorf("error deleting cached response: %v", err) - } -} - -// New returns a new Cache for the given context. -func New(ctx appengine.Context) *Cache { - return &Cache{ctx} -} diff --git a/memcache/appengine_test.go b/memcache/appengine_test.go deleted file mode 100644 index f46c379..0000000 --- a/memcache/appengine_test.go +++ /dev/null @@ -1,20 +0,0 @@ -// +build appengine - -package memcache - -import ( - "testing" - - "appengine/aetest" - "github.com/gregjones/httpcache/test" -) - -func TestAppEngine(t *testing.T) { - ctx, err := aetest.NewContext(nil) - if err != nil { - t.Fatal(err) - } - defer ctx.Close() - - test.Cache(t, New(ctx)) -} diff --git a/memcache/memcache.go b/memcache/memcache.go deleted file mode 100644 index 462f0e5..0000000 --- a/memcache/memcache.go +++ /dev/null @@ -1,60 +0,0 @@ -// +build !appengine - -// Package memcache provides an implementation of httpcache.Cache that uses -// gomemcache to store cached responses. -// -// When built for Google App Engine, this package will provide an -// implementation that uses App Engine's memcache service. See the -// appengine.go file in this package for details. -package memcache - -import ( - "github.com/bradfitz/gomemcache/memcache" -) - -// Cache is an implementation of httpcache.Cache that caches responses in a -// memcache server. -type Cache struct { - *memcache.Client -} - -// cacheKey modifies an httpcache key for use in memcache. Specifically, it -// prefixes keys to avoid collision with other data stored in memcache. -func cacheKey(key string) string { - return "httpcache:" + key -} - -// Get returns the response corresponding to key if present. -func (c *Cache) Get(key string) (resp []byte, ok bool) { - item, err := c.Client.Get(cacheKey(key)) - if err != nil { - return nil, false - } - return item.Value, true -} - -// Set saves a response to the cache as key. -func (c *Cache) Set(key string, resp []byte) { - item := &memcache.Item{ - Key: cacheKey(key), - Value: resp, - } - c.Client.Set(item) -} - -// Delete removes the response with key from the cache. -func (c *Cache) Delete(key string) { - c.Client.Delete(cacheKey(key)) -} - -// New returns a new Cache using the provided memcache server(s) with equal -// weight. If a server is listed multiple times, it gets a proportional amount -// of weight. -func New(server ...string) *Cache { - return NewWithClient(memcache.New(server...)) -} - -// NewWithClient returns a new Cache with the given memcache client. -func NewWithClient(client *memcache.Client) *Cache { - return &Cache{client} -} diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go deleted file mode 100644 index cce9e02..0000000 --- a/memcache/memcache_test.go +++ /dev/null @@ -1,24 +0,0 @@ -// +build !appengine - -package memcache - -import ( - "net" - "testing" - - "github.com/gregjones/httpcache/test" -) - -const testServer = "localhost:11211" - -func TestMemCache(t *testing.T) { - conn, err := net.Dial("tcp", testServer) - if err != nil { - // TODO: rather than skip the test, fall back to a faked memcached server - t.Skipf("skipping test; no server running at %s", testServer) - } - conn.Write([]byte("flush_all\r\n")) // flush memcache - conn.Close() - - test.Cache(t, New(testServer)) -} diff --git a/redis/redis.go b/redis/redis.go deleted file mode 100644 index 3d69c6c..0000000 --- a/redis/redis.go +++ /dev/null @@ -1,43 +0,0 @@ -// Package redis provides a redis interface for http caching. -package redis - -import ( - "github.com/gomodule/redigo/redis" - "github.com/gregjones/httpcache" -) - -// cache is an implementation of httpcache.Cache that caches responses in a -// redis server. -type cache struct { - redis.Conn -} - -// cacheKey modifies an httpcache key for use in redis. Specifically, it -// prefixes keys to avoid collision with other data stored in redis. -func cacheKey(key string) string { - return "rediscache:" + key -} - -// Get returns the response corresponding to key if present. -func (c cache) Get(key string) (resp []byte, ok bool) { - item, err := redis.Bytes(c.Do("GET", cacheKey(key))) - if err != nil { - return nil, false - } - return item, true -} - -// Set saves a response to the cache as key. -func (c cache) Set(key string, resp []byte) { - c.Do("SET", cacheKey(key), resp) -} - -// Delete removes the response with key from the cache. -func (c cache) Delete(key string) { - c.Do("DEL", cacheKey(key)) -} - -// NewWithClient returns a new Cache with the given redis connection. -func NewWithClient(client redis.Conn) httpcache.Cache { - return cache{client} -} diff --git a/redis/redis_test.go b/redis/redis_test.go deleted file mode 100644 index 800863c..0000000 --- a/redis/redis_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package redis - -import ( - "testing" - - "github.com/gomodule/redigo/redis" - "github.com/gregjones/httpcache/test" -) - -func TestRedisCache(t *testing.T) { - conn, err := redis.Dial("tcp", "localhost:6379") - if err != nil { - // TODO: rather than skip the test, fall back to a faked redis server - t.Skipf("skipping test; no server running at localhost:6379") - } - conn.Do("FLUSHALL") - - test.Cache(t, NewWithClient(conn)) -} diff --git a/test/test.go b/test/test.go deleted file mode 100644 index c2ff257..0000000 --- a/test/test.go +++ /dev/null @@ -1,35 +0,0 @@ -package test - -import ( - "bytes" - "testing" - - "github.com/gregjones/httpcache" -) - -// Cache excercises a httpcache.Cache implementation. -func Cache(t *testing.T, cache httpcache.Cache) { - key := "testKey" - _, ok := cache.Get(key) - if ok { - t.Fatal("retrieved key before adding it") - } - - val := []byte("some bytes") - cache.Set(key, val) - - retVal, ok := cache.Get(key) - if !ok { - t.Fatal("could not retrieve an element we just added") - } - if !bytes.Equal(retVal, val) { - t.Fatal("retrieved a different value than what we put in") - } - - cache.Delete(key) - - _, ok = cache.Get(key) - if ok { - t.Fatal("deleted key still present") - } -} diff --git a/test/test_test.go b/test/test_test.go deleted file mode 100644 index 41a9c20..0000000 --- a/test/test_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package test_test - -import ( - "testing" - - "github.com/gregjones/httpcache" - "github.com/gregjones/httpcache/test" -) - -func TestMemoryCache(t *testing.T) { - test.Cache(t, httpcache.NewMemoryCache()) -}