Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
58dc7a2
MVP: google cloud storage cachestore
marino39 Oct 7, 2024
679c574
MVP: google cloud storage cachestore - handle does not exist
marino39 Oct 7, 2024
2bcaf9d
MVP: google cloud storage cachestore - ClearAll
marino39 Oct 7, 2024
fe7d99d
MVP: google cloud storage cachestore - small refactor to match other …
marino39 Oct 7, 2024
6c14ef6
MVP: google cloud storage cachestore - add example
marino39 Oct 7, 2024
59fbf2c
MVP: google cloud storage cachestore - unit tests
marino39 Oct 8, 2024
223e8c7
MVP: google cloud storage cachestore - unit tests missing error check
marino39 Oct 8, 2024
1a2abf7
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
46d347b
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
7347d19
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
b817aab
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
7955b04
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
b955fc8
Update gcstorage/gcstorage_test.go
marino39 Oct 8, 2024
f654b48
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
cdfa671
refactor + apply review remarks
marino39 Oct 8, 2024
c447ba8
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
e2dcfc9
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
80de574
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
9a161cc
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
e9d30aa
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
4323bad
apply review remarks
marino39 Oct 8, 2024
15f4936
replace bucket name
marino39 Oct 8, 2024
37bb7fe
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
98aa168
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
c4deac1
Update gcstorage/gcstorage.go
marino39 Oct 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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