This commit is contained in:
54
provider/pebble/provider.go
Normal file
54
provider/pebble/provider.go
Normal 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
|
||||
}
|
||||
17
provider/pebble/provider_test.go
Normal file
17
provider/pebble/provider_test.go
Normal 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))
|
||||
}
|
||||
Reference in New Issue
Block a user