Skip to content

Commit

Permalink
update to use whosonfirst/go-cache v0.5.0; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Jun 27, 2021
1 parent 5865dda commit ce57a24
Show file tree
Hide file tree
Showing 26 changed files with 1,016 additions and 112 deletions.
14 changes: 7 additions & 7 deletions blob.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cache
package blob

import (
"bufio"
"bytes"
"context"
wof_cache "github.com/whosonfirst/go-cache"
"github.com/whosonfirst/go-ioutil"
"gocloud.dev/blob"
"io"
"io/ioutil"
"sync/atomic"
)

Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *BlobCache) Name() string {
return "blob"
}

func (c *BlobCache) Get(ctx context.Context, key string) (io.ReadCloser, error) {
func (c *BlobCache) Get(ctx context.Context, key string) (io.ReadSeekCloser, error) {

fh, err := c.bucket.NewReader(ctx, key, nil)

Expand All @@ -67,10 +67,10 @@ func (c *BlobCache) Get(ctx context.Context, key string) (io.ReadCloser, error)
}

atomic.AddInt64(&c.hits, 1)
return fh, nil
return ioutil.NewReadSeekCloser(fh)
}

func (c *BlobCache) Set(ctx context.Context, key string, fh io.ReadCloser) (io.ReadCloser, error) {
func (c *BlobCache) Set(ctx context.Context, key string, fh io.ReadSeekCloser) (io.ReadSeekCloser, error) {

var wr_opts *blob.WriterOptions

Expand All @@ -79,7 +79,7 @@ func (c *BlobCache) Set(ctx context.Context, key string, fh io.ReadCloser) (io.R
if v != nil {
wr_opts = v.(*blob.WriterOptions)
}

bucket_wr, err := c.bucket.NewWriter(ctx, key, wr_opts)

if err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *BlobCache) Set(ctx context.Context, key string, fh io.ReadCloser) (io.R
atomic.AddInt64(&c.sets, 1)

r.Reset(b.Bytes())
return ioutil.NopCloser(r), nil
return ioutil.NewReadSeekCloser(r)
}

func (c *BlobCache) Unset(ctx context.Context, key string) error {
Expand Down
28 changes: 28 additions & 0 deletions blob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package blob

import (
"context"
"github.com/whosonfirst/go-cache"
_ "gocloud.dev/blob/memblob"
"testing"
)

func TestFSCache(t *testing.T) {

ctx := context.Background()

c, err := cache.NewCache(ctx, "mem://")

if err != nil {
t.Fatalf("Failed to create mem:// cache, %v", err)
}

opts := &testCacheOptions{}

// This is defined in testing.go
err = testCache(ctx, c, opts)

if err != nil {
t.Fatalf("Cache tests failed, %v", err)
}
}
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// package blob implements the Who's On First `go-cache` interface using Google's GoCloud `Blob` objects.
package blob
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/whosonfirst/go-cache-blob
go 1.16

require (
github.com/whosonfirst/go-cache v0.3.0
github.com/whosonfirst/go-cache v0.5.0
github.com/whosonfirst/go-ioutil v1.0.0
gocloud.dev v0.23.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ github.com/whosonfirst/go-cache v0.1.0 h1:w6o4pWT/q2q/uclV72EU3D9CCKOgU+w+D+GKpa
github.com/whosonfirst/go-cache v0.1.0/go.mod h1:M5Iche+26mVKFph49/pxS5W4ULZ9Dr01mebPqswG8QU=
github.com/whosonfirst/go-cache v0.3.0 h1:QwboIxWu3Ab2gPikTMAJT2kukRr5c5yr/+yoMFRo8vQ=
github.com/whosonfirst/go-cache v0.3.0/go.mod h1:uYn6IpxBxuSdULs15zkHHkOHTW8i4AS4A3jTv8wcbGg=
github.com/whosonfirst/go-cache v0.5.0 h1:s8o2FafYxI1TGJGqwi3G0XVD5yY1dvzwSe9dE5C+KfA=
github.com/whosonfirst/go-cache v0.5.0/go.mod h1:YawO5K9kQa3XnrtdfwZ6OS4gwuO87S06FqIPblMqZf4=
github.com/whosonfirst/go-ioutil v1.0.0 h1:sYpgJx7Wsp76e7PFGa8KKQBvWQW3+HMCWSJbKdD5m14=
github.com/whosonfirst/go-ioutil v1.0.0/go.mod h1:2dS1vWdAIkiHDvDF8fYyjv6k2NISmwaIjJJeEDBEdvg=
github.com/whosonfirst/go-whosonfirst-cache v0.1.0 h1:ryWTsHj7gAEjwHC/WjsjROpFflsz3SCa7W9Qnk4EU7k=
github.com/whosonfirst/go-whosonfirst-cache v0.1.0/go.mod h1:P5Tx34j2M50qX/kTfXY516apwGzJGkFSBYQI7TGArKw=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
110 changes: 110 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package blob

// Eventually this will be deprecated and all of these methods will be publicly
// available in go-cache/testing.go

import (
"bytes"
"context"
"fmt"
"github.com/whosonfirst/go-cache"
"io"
_ "log"
)

type testCacheOptions struct {
AllowCacheMiss bool
}

func testCache(ctx context.Context, c cache.Cache, opts *testCacheOptions) error {

k := "test"
v := "test"

fh1, err := cache.ReadSeekCloserFromString(v)

if err != nil {
return fmt.Errorf("Failed to create ReadSeekCloser, %v", err)
}

fh2, err := c.Set(ctx, k, fh1)

if err != nil {
return fmt.Errorf("Failed to set %s (%s), %v", k, v, err)
}

equals, err := compareReadSeekClosers(fh1, fh2)

if err != nil {
return fmt.Errorf("Failed to compare filehandles, %v", err)
}

if !equals {
return fmt.Errorf("Filehandles 1 and 2 failed equality test")
}

fh3, err := c.Get(ctx, k)

if err != nil && !cache.IsCacheMiss(err) {
return fmt.Errorf("Failed to get cache value for '%s', %v", k, err)
}

if err != nil {

if !opts.AllowCacheMiss {
return fmt.Errorf("Unexpected cache miss for '%s'", k)
}

} else {

equals, err = compareReadSeekClosers(fh1, fh3)

if err != nil {
return fmt.Errorf("Failed to compare filehandles 1 and 3, %v", err)
}

if !equals {
return fmt.Errorf("Filehandles 1 and 3 failed equality test")
}
}

return nil
}

func compareReadSeekClosers(fh1 io.ReadSeekCloser, fh2 io.ReadSeekCloser) (bool, error) {

fh1.Seek(0, 0)
fh2.Seek(0, 0)

defer func() {
fh1.Seek(0, 0)
fh2.Seek(0, 0)
}()

b1, err := io.ReadAll(fh1)

if err != nil {
return false, fmt.Errorf("Failed to read fh1, %v", err)
}

fh1.Seek(0, 0)

b2, err := io.ReadAll(fh2)

if err != nil {
return false, fmt.Errorf("Failed to read fh2, %v", err)
}

fh2.Seek(0, 0)

// log.Printf("1 '%s'\n", string(b1))
// log.Printf("2 '%s'\n", string(b2))

equals := bytes.Compare(b1, b2)

if equals != 0 {
return false, nil
}

return true, nil
}
12 changes: 6 additions & 6 deletions vendor/github.com/whosonfirst/go-cache/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/whosonfirst/go-cache/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/whosonfirst/go-cache/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions vendor/github.com/whosonfirst/go-cache/fs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/whosonfirst/go-cache/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/whosonfirst/go-cache/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions vendor/github.com/whosonfirst/go-cache/gocache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce57a24

Please sign in to comment.