mirror of
https://github.com/kataras/iris.git
synced 2026-01-06 11:37:06 +00:00
Update to version 10.6.1 | Re-implement the BoltDB as built'n session database and more. Please read the HISTORY file for further details
Former-commit-id: fa68a914bec5fe4f595bdeaea84ecab6374ba643
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package badger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
@@ -24,6 +26,8 @@ type Database struct {
|
||||
// it's initialized at `New` or `NewFromDB`.
|
||||
// Can be used to get stats.
|
||||
Service *badger.DB
|
||||
|
||||
closed uint32 // if 1 is closed.
|
||||
}
|
||||
|
||||
var _ sessions.Database = (*Database)(nil)
|
||||
@@ -76,7 +80,7 @@ func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime
|
||||
txn := db.Service.NewTransaction(true)
|
||||
defer txn.Commit(nil)
|
||||
|
||||
bsid := []byte(sid)
|
||||
bsid := makePrefix(sid)
|
||||
item, err := txn.Get(bsid)
|
||||
if err == nil {
|
||||
// found, return the expiration.
|
||||
@@ -98,10 +102,14 @@ func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime
|
||||
return sessions.LifeTime{} // session manager will handle the rest.
|
||||
}
|
||||
|
||||
var delim = byte('*')
|
||||
var delim = byte('_')
|
||||
|
||||
func makePrefix(sid string) []byte {
|
||||
return append([]byte(sid), delim)
|
||||
}
|
||||
|
||||
func makeKey(sid, key string) []byte {
|
||||
return append([]byte(sid), append([]byte(key), delim)...)
|
||||
return append(makePrefix(sid), []byte(key)...)
|
||||
}
|
||||
|
||||
// Set sets a key value of a specific session.
|
||||
@@ -114,8 +122,8 @@ func (db *Database) Set(sid string, lifetime sessions.LifeTime, key string, valu
|
||||
}
|
||||
|
||||
err = db.Service.Update(func(txn *badger.Txn) error {
|
||||
return txn.SetWithTTL(makeKey(sid, key), valueBytes, lifetime.DurationUntilExpiration())
|
||||
// return txn.Set(makeKey(sid, key), valueBytes)
|
||||
dur := lifetime.DurationUntilExpiration()
|
||||
return txn.SetWithTTL(makeKey(sid, key), valueBytes, dur)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -149,7 +157,7 @@ func (db *Database) Get(sid string, key string) (value interface{}) {
|
||||
|
||||
// Visit loops through all session keys and values.
|
||||
func (db *Database) Visit(sid string, cb func(key string, value interface{})) {
|
||||
prefix := append([]byte(sid), delim)
|
||||
prefix := makePrefix(sid)
|
||||
|
||||
txn := db.Service.NewTransaction(false)
|
||||
defer txn.Discard()
|
||||
@@ -171,7 +179,7 @@ func (db *Database) Visit(sid string, cb func(key string, value interface{})) {
|
||||
continue
|
||||
}
|
||||
|
||||
cb(string(item.Key()), value)
|
||||
cb(string(bytes.TrimPrefix(item.Key(), prefix)), value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,7 +192,7 @@ var iterOptionsNoValues = badger.IteratorOptions{
|
||||
|
||||
// Len returns the length of the session's entries (keys).
|
||||
func (db *Database) Len(sid string) (n int) {
|
||||
prefix := append([]byte(sid), delim)
|
||||
prefix := makePrefix(sid)
|
||||
|
||||
txn := db.Service.NewTransaction(false)
|
||||
iter := txn.NewIterator(iterOptionsNoValues)
|
||||
@@ -211,7 +219,7 @@ func (db *Database) Delete(sid string, key string) (deleted bool) {
|
||||
|
||||
// Clear removes all session key values but it keeps the session entry.
|
||||
func (db *Database) Clear(sid string) {
|
||||
prefix := append([]byte(sid), delim)
|
||||
prefix := makePrefix(sid)
|
||||
|
||||
txn := db.Service.NewTransaction(true)
|
||||
defer txn.Commit(nil)
|
||||
@@ -241,9 +249,14 @@ func (db *Database) Close() error {
|
||||
}
|
||||
|
||||
func closeDB(db *Database) error {
|
||||
if atomic.LoadUint32(&db.closed) > 0 {
|
||||
return nil
|
||||
}
|
||||
err := db.Service.Close()
|
||||
if err != nil {
|
||||
golog.Warnf("closing the badger connection: %v", err)
|
||||
} else {
|
||||
atomic.StoreUint32(&db.closed, 1)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user