first commit
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
andrey1s
2021-04-26 17:13:36 +03:00
commit 7da0cd57ce
45 changed files with 3703 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package pebble
import (
"context"
"errors"
"fmt"
"github.com/cockroachdb/pebble"
"gitoa.ru/go-4devs/cache"
"gitoa.ru/go-4devs/cache/item"
)
func New(db *pebble.DB) cache.Provider {
return func(ctx context.Context, operation string, i *cache.Item) error {
key := []byte(i.Key.String())
switch operation {
case cache.OperationGet:
val, cl, err := db.Get([]byte(i.Key.String()))
if err != nil {
if errors.Is(err, pebble.ErrNotFound) {
return wrapErr(cache.ErrCacheMiss)
}
return wrapErr(err)
}
defer func() {
_ = cl.Close()
}()
return wrapErr(item.UnmarshalExpired(i, val))
case cache.OperationSet:
b, err := item.MarshalExpired(i)
if err != nil {
return wrapErr(err)
}
return wrapErr(db.Set(key, b, pebble.Sync))
case cache.OperationDelete:
return wrapErr(db.Delete(key, pebble.Sync))
}
return wrapErr(cache.ErrOperationNotAllwed)
}
}
func wrapErr(err error) error {
if err != nil {
return fmt.Errorf("%w: pebble", err)
}
return nil
}

View File

@@ -0,0 +1,17 @@
package pebble_test
import (
"testing"
"gitoa.ru/go-4devs/cache/provider/pebble"
"gitoa.ru/go-4devs/cache/test"
)
func TestPebble(t *testing.T) {
t.Parallel()
db, cl := test.PebbleDB()
defer cl()
test.RunSute(t, pebble.New(db))
}