Skip to content

Commit

Permalink
google cloud storage cachestore (#18)
Browse files Browse the repository at this point in the history
* MVP: google cloud storage cachestore

* MVP: google cloud storage cachestore - handle does not exist

* MVP: google cloud storage cachestore - ClearAll

* MVP: google cloud storage cachestore - small refactor to match other cachestore impl

* MVP: google cloud storage cachestore - add example

* MVP: google cloud storage cachestore - unit tests

* MVP: google cloud storage cachestore - unit tests missing error check

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage_test.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* refactor + apply review remarks

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* apply review remarks

* replace bucket name

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

* Update gcstorage/gcstorage.go

Co-authored-by: David Sedláček <[email protected]>

---------

Co-authored-by: David Sedláček <[email protected]>
  • Loading branch information
marino39 and david-littlefarmer authored Oct 9, 2024
1 parent 509f7d6 commit 75090a8
Show file tree
Hide file tree
Showing 8 changed files with 843 additions and 18 deletions.
103 changes: 103 additions & 0 deletions _examples/gcstorage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"context"
"fmt"
"time"

"github.com/goware/cachestore"
"github.com/goware/cachestore/cachestorectl"
"github.com/goware/cachestore/gcstorage"
)

func main() {
cfg := &gcstorage.Config{
Bucket: "test-bucket",
KeyPrefix: "test/",
}

backend := gcstorage.Backend(cfg) //, cachestore.WithDefaultKeyExpiry(1*time.Second))

store, err := cachestorectl.Open[string](backend, cachestore.WithDefaultKeyExpiry(30*time.Second))
if err != nil {
panic(err)
}

ctx := context.Background()

// Set
for i := 0; i < 10; i++ {
err = store.Set(ctx, fmt.Sprintf("foo:%d", i), fmt.Sprintf("value-%d", i))
if err != nil {
panic(err)
}
}

store.SetEx(ctx, "foo:999", "value-999", 10*time.Minute)

// Exists
ok, err := store.Exists(ctx, "foo:9")
if err != nil {
panic(err)
}
if !ok {
panic("unexpected")
}
fmt.Println("=> exists(foo:9) =", ok)

// Get
v, ok, err := store.Get(ctx, "foo:9")
if err != nil {
panic(err)
}
if !ok {
panic("unexpected")
}
fmt.Println("=> get(foo:9) =", v)

time.Sleep(30 * time.Second)

// should expire based on rule above
ok, err = store.Exists(ctx, "foo:9")
if err != nil {
panic(err)
}
if ok {
panic("unexpected")
}
fmt.Println("=> exists(foo:9) =", ok)

v, ok, err = store.Get(ctx, "foo:9")
if err != nil {
panic(err)
}
if ok {
panic("unexpected")
}
fmt.Println("=> get(foo:9) =", v)

// should still have
v, ok, err = store.Get(ctx, "foo:999")
if err != nil {
panic(err)
}
if !ok {
panic("unexpected")
}
fmt.Println("=> get(foo:999) =", v)

// DeletePrefix
err = store.DeletePrefix(ctx, "foo:")
if err != nil {
panic(err)
}

// be gone
_, ok, _ = store.Get(ctx, "foo:999")
if ok {
panic("unexpected")
}

fmt.Println("done.")
fmt.Println("")
}
2 changes: 2 additions & 0 deletions cachestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var (

ErrKeyLengthTooLong = errors.New("cachestore: key length is too long")
ErrInvalidKey = errors.New("cachestore: invalid key")
ErrInvalidKeyPrefix = errors.New("cachestore: invalid key prefix")
ErrNotSupported = errors.New("cachestore: not supported")
)

type Store[V any] interface {
Expand Down
3 changes: 3 additions & 0 deletions cachestorectl/cachestorectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/goware/cachestore"
"github.com/goware/cachestore/gcstorage"
"github.com/goware/cachestore/memlru"
"github.com/goware/cachestore/nostore"
"github.com/goware/cachestore/redis"
Expand All @@ -12,6 +13,8 @@ import (
func Open[T any](backend cachestore.Backend, opts ...cachestore.StoreOptions) (cachestore.Store[T], error) {
switch t := backend.(type) {

case *gcstorage.Config:
return gcstorage.NewWithBackend[T](backend, opts...)
case *memlru.Config:
return memlru.NewWithBackend[T](backend, opts...)

Expand Down
19 changes: 19 additions & 0 deletions gcstorage/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gcstorage

import (
"github.com/goware/cachestore"
"golang.org/x/oauth2/google"
)

type Config struct {
cachestore.StoreOptions

Credentials *google.Credentials

Bucket string
KeyPrefix string
}

func (c *Config) Apply(options *cachestore.StoreOptions) {
c.StoreOptions.Apply(options)
}
Loading

0 comments on commit 75090a8

Please sign in to comment.