✏ update comments

This commit is contained in:
Fenny
2020-11-17 13:56:09 +01:00
parent deb10fb2a7
commit ddef41c2ee
28 changed files with 173 additions and 99 deletions

58
README.md Normal file
View File

@@ -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?

View File

@@ -14,7 +14,8 @@ A fast key-value DB using [dgraph-io/badger](https://github.com/dgraph-io/badger
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -15,8 +15,9 @@ type Storage struct {
done chan 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 // New creates a new memory storage
func New(config ...Config) *Storage { func New(config ...Config) *Storage {
@@ -54,7 +55,7 @@ func New(config ...Config) *Storage {
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 { if len(key) <= 0 {
return nil, ErrNotExist return nil, ErrNotFound
} }
var data []byte var data []byte
err := s.db.View(func(txn *badger.Txn) error { err := s.db.View(func(txn *badger.Txn) error {
@@ -71,7 +72,7 @@ func (s *Storage) Get(key string) ([]byte, error) {
}) })
// If no value was found return false // If no value was found return false
if err == badger.ErrKeyNotFound { if err == badger.ErrKeyNotFound {
return data, ErrNotExist return data, ErrNotFound
} }
return data, err return data, err
} }

View File

@@ -65,14 +65,14 @@ func Test_Badger_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_Badger_Get_NotExist(t *testing.T) { func Test_Badger_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -89,7 +89,7 @@ func Test_Badger_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -108,11 +108,11 @@ func Test_Badger_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -14,7 +14,8 @@
```go ```go
func New(config Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -18,8 +18,9 @@ type Storage struct {
table string 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 // New creates a new storage
func New(config Config) *Storage { func New(config Config) *Storage {
@@ -112,11 +113,11 @@ func (s *Storage) Get(key string) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if getItemOutput.Item == nil { } else if getItemOutput.Item == nil {
return nil, ErrNotExist return nil, ErrNotFound
} }
attributeVal := getItemOutput.Item[valAttrName] attributeVal := getItemOutput.Item[valAttrName]
if attributeVal == nil { if attributeVal == nil {
return nil, ErrNotExist return nil, ErrNotFound
} }
return attributeVal.B, nil return attributeVal.B, nil
} }

View File

@@ -13,7 +13,8 @@ A Memcache storage driver using [`bradfitz/gomemcache`](https://github.com/bradf
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -16,8 +16,9 @@ type Storage struct {
items *sync.Pool 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 // New creates a new storage
func New(config ...Config) *Storage { func New(config ...Config) *Storage {
@@ -61,11 +62,11 @@ func New(config ...Config) *Storage {
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 { if len(key) <= 0 {
return nil, ErrNotExist return nil, ErrNotFound
} }
item, err := s.db.Get(key) item, err := s.db.Get(key)
if err == mc.ErrCacheMiss { if err == mc.ErrCacheMiss {
return nil, ErrNotExist return nil, ErrNotFound
} else if err != nil { } else if err != nil {
return nil, err return nil, err
} }

View File

@@ -65,14 +65,14 @@ func Test_Memcache_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_Memcache_Get_NotExist(t *testing.T) { func Test_Memcache_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -89,7 +89,7 @@ func Test_Memcache_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -108,11 +108,11 @@ func Test_Memcache_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -14,7 +14,8 @@ An in-memory storage driver.
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

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

View File

@@ -65,14 +65,14 @@ func Test_Memory_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_Memory_Get_NotExist(t *testing.T) { func Test_Memory_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -89,7 +89,7 @@ func Test_Memory_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -108,11 +108,11 @@ func Test_Memory_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -13,7 +13,8 @@ A MongoDB storage driver using [mongodb/mongo-go-driver](https://github.com/mong
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -21,8 +21,9 @@ type Storage struct {
items *sync.Pool 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 { type item struct {
ObjectID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"` ObjectID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
@@ -112,14 +113,14 @@ func New(config ...Config) *Storage {
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 { if len(key) <= 0 {
return nil, ErrNotExist return nil, ErrNotFound
} }
res := s.col.FindOne(context.Background(), bson.M{"key": key}) res := s.col.FindOne(context.Background(), bson.M{"key": key})
item := s.acquireItem() item := s.acquireItem()
if err := res.Err(); err != nil { if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments { if err == mongo.ErrNoDocuments {
return nil, ErrNotExist return nil, ErrNotFound
} }
return nil, err return nil, err
} }
@@ -128,7 +129,7 @@ func (s *Storage) Get(key string) ([]byte, error) {
} }
if !item.Expiration.IsZero() && item.Expiration.Unix() <= time.Now().Unix() { if !item.Expiration.IsZero() && item.Expiration.Unix() <= time.Now().Unix() {
return nil, ErrNotExist return nil, ErrNotFound
} }
// // not safe? // // not safe?
// res := item.Val // res := item.Val

View File

@@ -67,14 +67,14 @@ func Test_MongoDB_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_MongoDB_Get_NotExist(t *testing.T) { func Test_MongoDB_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -91,7 +91,7 @@ func Test_MongoDB_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -110,11 +110,11 @@ func Test_MongoDB_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -13,7 +13,8 @@ A MySQL storage driver using `database/sql` and [go-sql-driver/mysql](https://gi
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -23,8 +23,9 @@ type Storage struct {
sqlGC string sqlGC 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")
var ( var (
dropQuery = "DROP TABLE IF EXISTS %s;" dropQuery = "DROP TABLE IF EXISTS %s;"
@@ -100,7 +101,7 @@ var noRows = "sql: no rows in result set"
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 { if len(key) <= 0 {
return nil, ErrNotExist return nil, ErrNotFound
} }
row := s.db.QueryRow(s.sqlSelect, key) row := s.db.QueryRow(s.sqlSelect, key)
@@ -113,14 +114,14 @@ func (s *Storage) Get(key string) ([]byte, error) {
if err := row.Scan(&data, &exp); err != nil { if err := row.Scan(&data, &exp); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, ErrNotExist return nil, ErrNotFound
} }
return nil, err return nil, err
} }
// If the expiration time has already passed, then return nil // If the expiration time has already passed, then return nil
if exp != 0 && exp <= time.Now().Unix() { if exp != 0 && exp <= time.Now().Unix() {
return nil, ErrNotExist return nil, ErrNotFound
} }
return data, nil return data, nil

View File

@@ -71,14 +71,14 @@ func Test_MYSQL_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_MYSQL_Get_NotExist(t *testing.T) { func Test_MYSQL_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -95,7 +95,7 @@ func Test_MYSQL_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -114,11 +114,11 @@ func Test_MYSQL_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -13,7 +13,8 @@ A Postgres storage driver using [lib/pq](https://github.com/lib/pq).
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -24,8 +24,9 @@ type Storage struct {
sqlGC string sqlGC 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")
var ( var (
dropQuery = `DROP TABLE IF EXISTS %s;` dropQuery = `DROP TABLE IF EXISTS %s;`
@@ -117,7 +118,7 @@ var noRows = errors.New("sql: no rows in result set")
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 { if len(key) <= 0 {
return nil, ErrNotExist return nil, ErrNotFound
} }
row := s.db.QueryRow(s.sqlSelect, key) row := s.db.QueryRow(s.sqlSelect, key)
// Add db response to data // Add db response to data
@@ -127,14 +128,14 @@ func (s *Storage) Get(key string) ([]byte, error) {
) )
if err := row.Scan(&data, &exp); err != nil { if err := row.Scan(&data, &exp); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, ErrNotExist return nil, ErrNotFound
} }
return nil, err return nil, err
} }
// If the expiration time has already passed, then return nil // If the expiration time has already passed, then return nil
if exp != 0 && exp <= time.Now().Unix() { if exp != 0 && exp <= time.Now().Unix() {
return nil, ErrNotExist return nil, ErrNotFound
} }
return data, nil return data, nil

View File

@@ -71,14 +71,14 @@ func Test_Postgres_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_Postgres_Get_NotExist(t *testing.T) { func Test_Postgres_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -95,7 +95,7 @@ func Test_Postgres_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -114,11 +114,11 @@ func Test_Postgres_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -13,7 +13,8 @@ A Redis storage driver using [go-redis/redis](github.com/go-redis/redis).
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -14,8 +14,8 @@ type Storage struct {
db *redis.Client db *redis.Client
} }
// Common storage errors // ErrNotFound means that a get call did not find the requested key.
var ErrNotExist = errors.New("key does not exist") var ErrNotFound = errors.New("key not found")
// New creates a new redis storage // New creates a new redis storage
func New(config ...Config) *Storage { func New(config ...Config) *Storage {
@@ -51,11 +51,11 @@ func New(config ...Config) *Storage {
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 { if len(key) <= 0 {
return nil, ErrNotExist return nil, ErrNotFound
} }
val, err := s.db.Get(context.Background(), key).Bytes() val, err := s.db.Get(context.Background(), key).Bytes()
if err == redis.Nil { if err == redis.Nil {
return nil, ErrNotExist return nil, ErrNotFound
} }
return val, err return val, err
} }

View File

@@ -67,13 +67,13 @@ func Test_Redis_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_Redis_Get_NotExist(t *testing.T) { func Test_Redis_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -90,7 +90,7 @@ func Test_Redis_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -109,11 +109,11 @@ func Test_Redis_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -13,7 +13,8 @@ A SQLite3 storage driver using [mattn/go-sqlite3](https://github.com/mattn/go-sq
```go ```go
func New(config ...Config) Storage 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) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -24,8 +24,8 @@ type Storage struct {
sqlGC string sqlGC string
} }
// Common storage errors // ErrNotFound means that a get call did not find the requested key.
var ErrNotExist = errors.New("key does not exist") var ErrNotFound = errors.New("key not found")
var ( var (
dropQuery = `DROP TABLE IF EXISTS %s;` dropQuery = `DROP TABLE IF EXISTS %s;`
@@ -98,7 +98,7 @@ func New(config ...Config) *Storage {
// Get value by key // Get value by key
func (s *Storage) Get(key string) ([]byte, error) { func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 { if len(key) <= 0 {
return nil, ErrNotExist return nil, ErrNotFound
} }
row := s.db.QueryRow(s.sqlSelect, key) row := s.db.QueryRow(s.sqlSelect, key)
// Add db response to data // Add db response to data
@@ -108,13 +108,13 @@ func (s *Storage) Get(key string) ([]byte, error) {
) )
if err := row.Scan(&data, &exp); err != nil { if err := row.Scan(&data, &exp); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, ErrNotExist return nil, ErrNotFound
} }
return nil, err return nil, err
} }
// If the expiration time has already passed, then return nil // If the expiration time has already passed, then return nil
if exp != 0 && exp <= time.Now().Unix() { if exp != 0 && exp <= time.Now().Unix() {
return nil, ErrNotExist return nil, ErrNotFound
} }
return data, nil return data, nil

View File

@@ -68,14 +68,14 @@ func Test_SQLite3_Get_Expired(t *testing.T) {
) )
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
func Test_SQLite3_Get_NotExist(t *testing.T) { func Test_SQLite3_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -92,7 +92,7 @@ func Test_SQLite3_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }
@@ -111,11 +111,11 @@ func Test_SQLite3_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1") result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2") result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err) utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0) utils.AssertEqual(t, true, len(result) == 0)
} }

View File

@@ -2,26 +2,26 @@ package storage
import "time" import "time"
// Storage interface that is implemented by storage providers for different // Storage interface for communicating with different database/key-value
// middleware packages like cache, limiter, session and csrf // providers. Visit https://github.com/gofiber/storage for more info.
type Storage interface { type Storage interface {
// Get retrieves the value for the given key. // Get gets the value for the given key.
// If no value is not found it returns ErrNotExit error // It returns ErrNotFound if the storage does not contain the key.
Get(key string) ([]byte, error) Get(key string) ([]byte, error)
// Set stores the given value for the given key along with a // Set stores the given value for the given key along with a
// time-to-live expiration value, 0 means live for ever // time-to-live expiration value, 0 means live for ever
// The key must not be "" and the empty values are ignored. // Empty key or value will be ignored without an error.
Set(key string, val []byte, ttl time.Duration) error Set(key string, val []byte, ttl time.Duration) error
// Delete deletes the stored value for the given key. // Delete deletes the value for the given key.
// Deleting a non-existing key-value pair does NOT lead to an error. // It returns no error if the storage does not contain the key,
// The key must not be "".
Delete(key string) error Delete(key string) error
// Reset the storage // Reset resets the storage and delete all keys.
Reset() error Reset() error
// Close the storage // Close closes the storage and will stop any running garbage
// collectors and open connections.
Close() error Close() error
} }