From ddef41c2ee9f2094acc5c612e05942b42d045efa Mon Sep 17 00:00:00 2001 From: Fenny <25108519+Fenny@users.noreply.github.com> Date: Tue, 17 Nov 2020 13:56:09 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=20update=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 58 +++++++++++++++++++++++++++++++++++++++ badger/README.md | 3 +- badger/badger.go | 9 +++--- badger/badger_test.go | 10 +++---- dynamodb/README.md | 3 +- dynamodb/dynamodb.go | 9 +++--- memcache/README.md | 3 +- memcache/memcache.go | 9 +++--- memcache/memcache_test.go | 10 +++---- memory/README.md | 3 +- memory/memory.go | 9 +++--- memory/memory_test.go | 10 +++---- mongodb/README.md | 3 +- mongodb/mongodb.go | 11 ++++---- mongodb/mongodb_test.go | 10 +++---- mysql/README.md | 3 +- mysql/mysql.go | 11 ++++---- mysql/mysql_test.go | 10 +++---- postgres/README.md | 3 +- postgres/postgres.go | 11 ++++---- postgres/postgres_test.go | 10 +++---- redis/README.md | 3 +- redis/redis.go | 8 +++--- redis/redis_test.go | 10 +++---- sqlite3/README.md | 3 +- sqlite3/sqlite3.go | 10 +++---- sqlite3/sqlite3_test.go | 10 +++---- storage.go | 20 +++++++------- 28 files changed, 173 insertions(+), 99 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..de7e050b --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ + +

+ + + # 📦 Storage + + + + + + + + + + + + + +

+ +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) + + +* [DynamoDB](/dynamodb) + + +* [Memcache](/memcache) + + +* [Memory](/memory) + + +* [MongoDB](/mongodb) + + +* [MySQL](/mysql) + + +* [Postgres](/postgres) + + +* [Redis](/redis) + + +* [SQLite3](/sqlite3) + + + +## 🤔 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? diff --git a/badger/README.md b/badger/README.md index affdb469..20b53aa8 100644 --- a/badger/README.md +++ b/badger/README.md @@ -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 diff --git a/badger/badger.go b/badger/badger.go index d5be5b55..c0dbdcb2 100644 --- a/badger/badger.go +++ b/badger/badger.go @@ -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 { @@ -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 { @@ -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 } diff --git a/badger/badger_test.go b/badger/badger_test.go index b39a8b5b..9b413412 100644 --- a/badger/badger_test.go +++ b/badger/badger_test.go @@ -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) } @@ -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) } @@ -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) } diff --git a/dynamodb/README.md b/dynamodb/README.md index 29c63527..30391f59 100644 --- a/dynamodb/README.md +++ b/dynamodb/README.md @@ -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 diff --git a/dynamodb/dynamodb.go b/dynamodb/dynamodb.go index b5b72628..0692938a 100644 --- a/dynamodb/dynamodb.go +++ b/dynamodb/dynamodb.go @@ -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 { @@ -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 } diff --git a/memcache/README.md b/memcache/README.md index e622a28e..cf41391d 100644 --- a/memcache/README.md +++ b/memcache/README.md @@ -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 diff --git a/memcache/memcache.go b/memcache/memcache.go index 186f225c..a30d7163 100644 --- a/memcache/memcache.go +++ b/memcache/memcache.go @@ -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 { @@ -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 } diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index f128d6d1..860fb4cc 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -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) } @@ -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) } @@ -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) } diff --git a/memory/README.md b/memory/README.md index 8d5c9ffa..a9009923 100644 --- a/memory/README.md +++ b/memory/README.md @@ -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 diff --git a/memory/memory.go b/memory/memory.go index 8490a528..7c434fd3 100644 --- a/memory/memory.go +++ b/memory/memory.go @@ -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 @@ -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 diff --git a/memory/memory_test.go b/memory/memory_test.go index e2977a9b..9e156906 100644 --- a/memory/memory_test.go +++ b/memory/memory_test.go @@ -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) } @@ -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) } @@ -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) } diff --git a/mongodb/README.md b/mongodb/README.md index 4c56a0f0..4faeeb2b 100644 --- a/mongodb/README.md +++ b/mongodb/README.md @@ -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 diff --git a/mongodb/mongodb.go b/mongodb/mongodb.go index b9b5ba70..398f4552 100644 --- a/mongodb/mongodb.go +++ b/mongodb/mongodb.go @@ -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"` @@ -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 } @@ -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 diff --git a/mongodb/mongodb_test.go b/mongodb/mongodb_test.go index f204dd1a..19a40050 100644 --- a/mongodb/mongodb_test.go +++ b/mongodb/mongodb_test.go @@ -67,14 +67,14 @@ func Test_MongoDB_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_MongoDB_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) } @@ -91,7 +91,7 @@ func Test_MongoDB_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) } @@ -110,11 +110,11 @@ func Test_MongoDB_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) } diff --git a/mysql/README.md b/mysql/README.md index ed468e25..84fe69fe 100644 --- a/mysql/README.md +++ b/mysql/README.md @@ -13,7 +13,8 @@ A MySQL storage driver using `database/sql` and [go-sql-driver/mysql](https://gi ```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 diff --git a/mysql/mysql.go b/mysql/mysql.go index 071d0d01..c69f06bc 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -23,8 +23,9 @@ type Storage struct { 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 ( dropQuery = "DROP TABLE IF EXISTS %s;" @@ -100,7 +101,7 @@ var noRows = "sql: no rows in result set" // Get value by key func (s *Storage) Get(key string) ([]byte, error) { if len(key) <= 0 { - return nil, ErrNotExist + return nil, ErrNotFound } 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 == sql.ErrNoRows { - return nil, ErrNotExist + return nil, ErrNotFound } return nil, err } // If the expiration time has already passed, then return nil if exp != 0 && exp <= time.Now().Unix() { - return nil, ErrNotExist + return nil, ErrNotFound } return data, nil diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index b33c8d50..a66e53f9 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -71,14 +71,14 @@ func Test_MYSQL_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_MYSQL_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) } @@ -95,7 +95,7 @@ func Test_MYSQL_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) } @@ -114,11 +114,11 @@ func Test_MYSQL_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) } diff --git a/postgres/README.md b/postgres/README.md index a47ed091..64aaf5a0 100644 --- a/postgres/README.md +++ b/postgres/README.md @@ -13,7 +13,8 @@ A Postgres storage driver using [lib/pq](https://github.com/lib/pq). ```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 diff --git a/postgres/postgres.go b/postgres/postgres.go index 25ff3c3e..ab4f5b64 100644 --- a/postgres/postgres.go +++ b/postgres/postgres.go @@ -24,8 +24,9 @@ type Storage struct { 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 ( dropQuery = `DROP TABLE IF EXISTS %s;` @@ -117,7 +118,7 @@ var noRows = errors.New("sql: no rows in result set") // Get value by key func (s *Storage) Get(key string) ([]byte, error) { if len(key) <= 0 { - return nil, ErrNotExist + return nil, ErrNotFound } row := s.db.QueryRow(s.sqlSelect, key) // 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 == sql.ErrNoRows { - return nil, ErrNotExist + return nil, ErrNotFound } return nil, err } // If the expiration time has already passed, then return nil if exp != 0 && exp <= time.Now().Unix() { - return nil, ErrNotExist + return nil, ErrNotFound } return data, nil diff --git a/postgres/postgres_test.go b/postgres/postgres_test.go index d0b3d212..d17d1294 100644 --- a/postgres/postgres_test.go +++ b/postgres/postgres_test.go @@ -71,14 +71,14 @@ func Test_Postgres_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_Postgres_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) } @@ -95,7 +95,7 @@ func Test_Postgres_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) } @@ -114,11 +114,11 @@ func Test_Postgres_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) } diff --git a/redis/README.md b/redis/README.md index 30ed2291..074489f4 100644 --- a/redis/README.md +++ b/redis/README.md @@ -13,7 +13,8 @@ A Redis storage driver using [go-redis/redis](github.com/go-redis/redis). ```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 diff --git a/redis/redis.go b/redis/redis.go index f3e80436..e801d03b 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -14,8 +14,8 @@ type Storage struct { db *redis.Client } -// 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 redis storage func New(config ...Config) *Storage { @@ -51,11 +51,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 } val, err := s.db.Get(context.Background(), key).Bytes() if err == redis.Nil { - return nil, ErrNotExist + return nil, ErrNotFound } return val, err } diff --git a/redis/redis_test.go b/redis/redis_test.go index d6e8c648..0bf0aa4c 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -67,13 +67,13 @@ func Test_Redis_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_Redis_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) } @@ -90,7 +90,7 @@ func Test_Redis_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) } @@ -109,11 +109,11 @@ func Test_Redis_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) } diff --git a/sqlite3/README.md b/sqlite3/README.md index abc29149..63dcfa36 100644 --- a/sqlite3/README.md +++ b/sqlite3/README.md @@ -13,7 +13,8 @@ A SQLite3 storage driver using [mattn/go-sqlite3](https://github.com/mattn/go-sq ```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 diff --git a/sqlite3/sqlite3.go b/sqlite3/sqlite3.go index 6e1b9497..8c3e3d1a 100644 --- a/sqlite3/sqlite3.go +++ b/sqlite3/sqlite3.go @@ -24,8 +24,8 @@ type Storage struct { 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 ( dropQuery = `DROP TABLE IF EXISTS %s;` @@ -98,7 +98,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 } row := s.db.QueryRow(s.sqlSelect, key) // 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 == sql.ErrNoRows { - return nil, ErrNotExist + return nil, ErrNotFound } return nil, err } // If the expiration time has already passed, then return nil if exp != 0 && exp <= time.Now().Unix() { - return nil, ErrNotExist + return nil, ErrNotFound } return data, nil diff --git a/sqlite3/sqlite3_test.go b/sqlite3/sqlite3_test.go index 96e8f9a8..c3666b62 100644 --- a/sqlite3/sqlite3_test.go +++ b/sqlite3/sqlite3_test.go @@ -68,14 +68,14 @@ func Test_SQLite3_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_SQLite3_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) } @@ -92,7 +92,7 @@ func Test_SQLite3_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) } @@ -111,11 +111,11 @@ func Test_SQLite3_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) } diff --git a/storage.go b/storage.go index 1b3c9191..cfbb1cbd 100644 --- a/storage.go +++ b/storage.go @@ -2,26 +2,26 @@ package storage import "time" -// Storage interface that is implemented by storage providers for different -// middleware packages like cache, limiter, session and csrf +// Storage interface for communicating with different database/key-value +// providers. Visit https://github.com/gofiber/storage for more info. type Storage interface { - // Get retrieves the value for the given key. - // If no value is not found it returns ErrNotExit error + // Get gets the value for the given key. + // It returns ErrNotFound if the storage does not contain the key. Get(key string) ([]byte, error) // Set stores the given value for the given key along with a // 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 - // Delete deletes the stored value for the given key. - // Deleting a non-existing key-value pair does NOT lead to an error. - // The key must not be "". + // Delete deletes the value for the given key. + // It returns no error if the storage does not contain the key, Delete(key string) error - // Reset the storage + // Reset resets the storage and delete all keys. Reset() error - // Close the storage + // Close closes the storage and will stop any running garbage + // collectors and open connections. Close() error }