Skip to content

Commit

Permalink
✏ update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenny committed Nov 17, 2020
1 parent deb10fb commit ddef41c
Show file tree
Hide file tree
Showing 28 changed files with 173 additions and 99 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

<p align="center">
<!-- <a href="https://gofiber.io">
<img alt="Fiber" height="125" src="https://raw.githubusercontent.com/gofiber/docs/master/static/fiber_v2_logo.svg">
</a>
<br> -->

# 📦 Storage

<a href="https://pkg.go.dev/github.com/gofiber/storage?tab=doc">
<img src="https://img.shields.io/badge/%F0%9F%93%9A%20godoc-pkg-00ACD7.svg?color=00ACD7&style=flat">
</a>
<a href="https://goreportcard.com/report/github.com/gofiber/storage">
<img src="https://img.shields.io/badge/%F0%9F%93%9D%20goreport-A%2B-75C46B">
</a>
<a href="https://gocover.io/github.com/gofiber/storage">
<img src="https://img.shields.io/badge/%F0%9F%94%8E%20gocover-97.8%25-75C46B.svg?style=flat">
</a>
<a href="https://gofiber.io/discord">
<img src="https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7">
</a>
</p>

Premade storage drivers that implement [`Storage`](https://github.com/gofiber/storage/blob/main/storage.go) interface, designed to be used with various Fiber middlewares.

## 📑 Storage Implementations

* [Badger](/badger) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [DynamoDB](/dynamodb) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22DynamoDB%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/DynamoDB?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [Memcache](/memcache) <a href="https://github.com/gofiber/storage/actions?query=workflow%3AMemcache">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Memcache?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [Memory](/memory) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [MongoDB](/mongodb) <a href="https://github.com/gofiber/storage/actions?query=workflow%3AMongoDB">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/MongoDB?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [MySQL](/mysql) <a href="https://github.com/gofiber/storage/actions?query=workflow%3AMySQL">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/MySQL?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [Postgres](/postgres) <a href="https://github.com/gofiber/storage/actions?query=workflow%3APostgres">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Postgres?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [Redis](/redis) <a href="https://github.com/gofiber/storage/actions?query=workflow%3ARedis">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Redis?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [SQLite3](/sqlite3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>

## 🤔 Something missing?

If you've got a custom storage driver you made that's not listed here, why not submit a [PR](https://github.com/gofiber/storage/pulls) to add it?
3 changes: 2 additions & 1 deletion badger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ A fast key-value DB using [dgraph-io/badger](https://github.com/dgraph-io/badger
```go
func New(config ...Config) Storage

var ErrNotExist = errors.New("key does not exist")
// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
Expand Down
9 changes: 5 additions & 4 deletions badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ type Storage struct {
done chan struct{}
}

// Common storage errors
var ErrNotExist = errors.New("key does not exist")

// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

// New creates a new memory storage
func New(config ...Config) *Storage {
Expand Down Expand Up @@ -54,7 +55,7 @@ func New(config ...Config) *Storage {
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, ErrNotExist
return nil, ErrNotFound
}
var data []byte
err := s.db.View(func(txn *badger.Txn) error {
Expand All @@ -71,7 +72,7 @@ func (s *Storage) Get(key string) ([]byte, error) {
})
// If no value was found return false
if err == badger.ErrKeyNotFound {
return data, ErrNotExist
return data, ErrNotFound
}
return data, err
}
Expand Down
10 changes: 5 additions & 5 deletions badger/badger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func Test_Badger_Get_Expired(t *testing.T) {
)

result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

func Test_Badger_Get_NotExist(t *testing.T) {

result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand All @@ -89,7 +89,7 @@ func Test_Badger_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err)

result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand All @@ -108,11 +108,11 @@ func Test_Badger_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err)

result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)

result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand Down
3 changes: 2 additions & 1 deletion dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
```go
func New(config Config) Storage

var ErrNotExist = errors.New("key does not exist")
// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
Expand Down
9 changes: 5 additions & 4 deletions dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type Storage struct {
table string
}

// Common storage errors
var ErrNotExist = errors.New("key does not exist")

// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

// New creates a new storage
func New(config Config) *Storage {
Expand Down Expand Up @@ -112,11 +113,11 @@ func (s *Storage) Get(key string) ([]byte, error) {
if err != nil {
return nil, err
} else if getItemOutput.Item == nil {
return nil, ErrNotExist
return nil, ErrNotFound
}
attributeVal := getItemOutput.Item[valAttrName]
if attributeVal == nil {
return nil, ErrNotExist
return nil, ErrNotFound
}
return attributeVal.B, nil
}
Expand Down
3 changes: 2 additions & 1 deletion memcache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ A Memcache storage driver using [`bradfitz/gomemcache`](https://github.com/bradf
```go
func New(config ...Config) Storage

var ErrNotExist = errors.New("key does not exist")
// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
Expand Down
9 changes: 5 additions & 4 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ type Storage struct {
items *sync.Pool
}

// Common storage errors
var ErrNotExist = errors.New("key does not exist")

// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

// New creates a new storage
func New(config ...Config) *Storage {
Expand Down Expand Up @@ -61,11 +62,11 @@ func New(config ...Config) *Storage {
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, ErrNotExist
return nil, ErrNotFound
}
item, err := s.db.Get(key)
if err == mc.ErrCacheMiss {
return nil, ErrNotExist
return nil, ErrNotFound
} else if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func Test_Memcache_Get_Expired(t *testing.T) {
)

result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

func Test_Memcache_Get_NotExist(t *testing.T) {

result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand All @@ -89,7 +89,7 @@ func Test_Memcache_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err)

result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand All @@ -108,11 +108,11 @@ func Test_Memcache_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err)

result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)

result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand Down
3 changes: 2 additions & 1 deletion memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ An in-memory storage driver.
```go
func New(config ...Config) Storage

var ErrNotExist = errors.New("key does not exist")
// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
Expand Down
9 changes: 5 additions & 4 deletions memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type Storage struct {
done chan struct{}
}

// Common storage errors
var ErrNotExist = errors.New("key does not exist")

// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

type entry struct {
data []byte
Expand Down Expand Up @@ -43,13 +44,13 @@ func New(config ...Config) *Storage {
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, ErrNotExist
return nil, ErrNotFound
}
s.mux.RLock()
v, ok := s.db[key]
s.mux.RUnlock()
if !ok || v.expiry != 0 && v.expiry <= time.Now().Unix() {
return nil, ErrNotExist
return nil, ErrNotFound
}

return v.data, nil
Expand Down
10 changes: 5 additions & 5 deletions memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func Test_Memory_Get_Expired(t *testing.T) {
)

result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

func Test_Memory_Get_NotExist(t *testing.T) {

result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand All @@ -89,7 +89,7 @@ func Test_Memory_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err)

result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand All @@ -108,11 +108,11 @@ func Test_Memory_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err)

result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)

result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}

Expand Down
3 changes: 2 additions & 1 deletion mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ A MongoDB storage driver using [mongodb/mongo-go-driver](https://github.com/mong
```go
func New(config ...Config) Storage

var ErrNotExist = errors.New("key does not exist")
// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
Expand Down
11 changes: 6 additions & 5 deletions mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type Storage struct {
items *sync.Pool
}

// Common storage errors
var ErrNotExist = errors.New("key does not exist")

// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")

type item struct {
ObjectID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Expand Down Expand Up @@ -112,14 +113,14 @@ func New(config ...Config) *Storage {
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, ErrNotExist
return nil, ErrNotFound
}
res := s.col.FindOne(context.Background(), bson.M{"key": key})
item := s.acquireItem()

if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
return nil, ErrNotExist
return nil, ErrNotFound
}
return nil, err
}
Expand All @@ -128,7 +129,7 @@ func (s *Storage) Get(key string) ([]byte, error) {
}

if !item.Expiration.IsZero() && item.Expiration.Unix() <= time.Now().Unix() {
return nil, ErrNotExist
return nil, ErrNotFound
}
// // not safe?
// res := item.Val
Expand Down
Loading

0 comments on commit ddef41c

Please sign in to comment.