mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
improve cache handler, embracing #2210 too
This commit is contained in:
@@ -30,7 +30,7 @@ type Database interface {
|
||||
SetLogger(*golog.Logger)
|
||||
// Acquire receives a session's lifetime from the database,
|
||||
// if the return value is LifeTime{} then the session manager sets the life time based on the expiration duration lives in configuration.
|
||||
Acquire(sid string, expires time.Duration) LifeTime
|
||||
Acquire(sid string, expires time.Duration) memstore.LifeTime
|
||||
// OnUpdateExpiration should re-set the expiration (ttl) of the session entry inside the database,
|
||||
// it is fired on `ShiftExpiration` and `UpdateExpiration`.
|
||||
// If the database does not support change of ttl then the session entry will be cloned to another one
|
||||
@@ -81,11 +81,11 @@ func newMemDB() Database { return &mem{values: make(map[string]*memstore.Store)}
|
||||
|
||||
func (s *mem) SetLogger(*golog.Logger) {}
|
||||
|
||||
func (s *mem) Acquire(sid string, expires time.Duration) LifeTime {
|
||||
func (s *mem) Acquire(sid string, expires time.Duration) memstore.LifeTime {
|
||||
s.mu.Lock()
|
||||
s.values[sid] = new(memstore.Store)
|
||||
s.mu.Unlock()
|
||||
return LifeTime{}
|
||||
return memstore.LifeTime{}
|
||||
}
|
||||
|
||||
// Do nothing, the `LifeTime` of the Session will be managed by the callers automatically on memory-based storage.
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
)
|
||||
|
||||
// LifeTime controls the session expiration datetime.
|
||||
type LifeTime struct {
|
||||
// Remember, tip for the future:
|
||||
// No need of gob.Register, because we embed the time.Time.
|
||||
// And serious bug which has a result of me spending my whole evening:
|
||||
// Because of gob encoding it doesn't encodes/decodes the other fields if time.Time is embedded
|
||||
// (this should be a bug(go1.9-rc1) or not. We don't care atm)
|
||||
time.Time
|
||||
timer *time.Timer
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// Begin will begin the life based on the time.Now().Add(d).
|
||||
// Use `Continue` to continue from a stored time(database-based session does that).
|
||||
func (lt *LifeTime) Begin(d time.Duration, onExpire func()) {
|
||||
if d <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
lt.mu.Lock()
|
||||
lt.Time = time.Now().Add(d)
|
||||
lt.timer = time.AfterFunc(d, onExpire)
|
||||
lt.mu.Unlock()
|
||||
}
|
||||
|
||||
// Revive will continue the life based on the stored Time.
|
||||
// Other words that could be used for this func are: Continue, Restore, Resc.
|
||||
func (lt *LifeTime) Revive(onExpire func()) {
|
||||
if lt.Time.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if lt.Time.After(now) {
|
||||
d := lt.Time.Sub(now)
|
||||
lt.mu.Lock()
|
||||
lt.timer = time.AfterFunc(d, onExpire)
|
||||
lt.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Shift resets the lifetime based on "d".
|
||||
func (lt *LifeTime) Shift(d time.Duration) {
|
||||
lt.mu.Lock()
|
||||
if d > 0 && lt.timer != nil {
|
||||
lt.Time = time.Now().Add(d)
|
||||
lt.timer.Reset(d)
|
||||
}
|
||||
lt.mu.Unlock()
|
||||
}
|
||||
|
||||
// ExpireNow reduce the lifetime completely.
|
||||
func (lt *LifeTime) ExpireNow() {
|
||||
lt.mu.Lock()
|
||||
lt.Time = context.CookieExpireDelete
|
||||
if lt.timer != nil {
|
||||
lt.timer.Stop()
|
||||
}
|
||||
lt.mu.Unlock()
|
||||
}
|
||||
|
||||
// HasExpired reports whether "lt" represents is expired.
|
||||
func (lt *LifeTime) HasExpired() bool {
|
||||
if lt.IsZero() {
|
||||
return false
|
||||
}
|
||||
|
||||
return lt.Time.Before(time.Now())
|
||||
}
|
||||
|
||||
// DurationUntilExpiration returns the duration until expires, it can return negative number if expired,
|
||||
// a call to `HasExpired` may be useful before calling this `Dur` function.
|
||||
func (lt *LifeTime) DurationUntilExpiration() time.Duration {
|
||||
return time.Until(lt.Time)
|
||||
}
|
||||
@@ -21,7 +21,7 @@ type (
|
||||
mu sync.RWMutex // for flashes.
|
||||
// Lifetime it contains the expiration data, use it for read-only information.
|
||||
// See `Sessions.UpdateExpiration` too.
|
||||
Lifetime *LifeTime
|
||||
Lifetime *memstore.LifeTime
|
||||
// Man is the sessions manager that this session created of.
|
||||
Man *Sessions
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/memstore"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
|
||||
"github.com/dgraph-io/badger/v2"
|
||||
@@ -82,7 +83,7 @@ func (db *Database) SetLogger(logger *golog.Logger) {
|
||||
|
||||
// Acquire receives a session's lifetime from the database,
|
||||
// if the return value is LifeTime{} then the session manager sets the life time based on the expiration duration lives in configuration.
|
||||
func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime {
|
||||
func (db *Database) Acquire(sid string, expires time.Duration) memstore.LifeTime {
|
||||
txn := db.Service.NewTransaction(true)
|
||||
defer txn.Commit()
|
||||
|
||||
@@ -90,7 +91,7 @@ func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime
|
||||
item, err := txn.Get(bsid)
|
||||
if err == nil {
|
||||
// found, return the expiration.
|
||||
return sessions.LifeTime{Time: time.Unix(int64(item.ExpiresAt()), 0)}
|
||||
return memstore.LifeTime{Time: time.Unix(int64(item.ExpiresAt()), 0)}
|
||||
}
|
||||
|
||||
// not found, create an entry with ttl and return an empty lifetime, session manager will do its job.
|
||||
@@ -105,7 +106,7 @@ func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime
|
||||
db.logger.Error(err)
|
||||
}
|
||||
|
||||
return sessions.LifeTime{} // session manager will handle the rest.
|
||||
return memstore.LifeTime{} // session manager will handle the rest.
|
||||
}
|
||||
|
||||
// OnUpdateExpiration not implemented here, yet.
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/core/memstore"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
@@ -159,7 +160,7 @@ var expirationKey = []byte("exp") // it can be random.
|
||||
|
||||
// Acquire receives a session's lifetime from the database,
|
||||
// if the return value is LifeTime{} then the session manager sets the life time based on the expiration duration lives in configuration.
|
||||
func (db *Database) Acquire(sid string, expires time.Duration) (lifetime sessions.LifeTime) {
|
||||
func (db *Database) Acquire(sid string, expires time.Duration) (lifetime memstore.LifeTime) {
|
||||
bsid := []byte(sid)
|
||||
err := db.Service.Update(func(tx *bolt.Tx) (err error) {
|
||||
root := db.getBucket(tx)
|
||||
@@ -204,7 +205,7 @@ func (db *Database) Acquire(sid string, expires time.Duration) (lifetime session
|
||||
return
|
||||
}
|
||||
|
||||
lifetime = sessions.LifeTime{Time: expirationTime}
|
||||
lifetime = memstore.LifeTime{Time: expirationTime}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -214,7 +215,7 @@ func (db *Database) Acquire(sid string, expires time.Duration) (lifetime session
|
||||
})
|
||||
if err != nil {
|
||||
db.logger.Debugf("unable to acquire session '%s': %v", sid, err)
|
||||
return sessions.LifeTime{}
|
||||
return memstore.LifeTime{}
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/core/memstore"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
@@ -138,7 +139,7 @@ const SessionIDKey = "session_id"
|
||||
|
||||
// Acquire receives a session's lifetime from the database,
|
||||
// if the return value is LifeTime{} then the session manager sets the life time based on the expiration duration lives in configuration.
|
||||
func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime {
|
||||
func (db *Database) Acquire(sid string, expires time.Duration) memstore.LifeTime {
|
||||
sidKey := db.makeSID(sid)
|
||||
if !db.c.Driver.Exists(sidKey) {
|
||||
if err := db.Set(sid, SessionIDKey, sid, 0, false); err != nil {
|
||||
@@ -149,11 +150,11 @@ func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime
|
||||
}
|
||||
}
|
||||
|
||||
return sessions.LifeTime{} // session manager will handle the rest.
|
||||
return memstore.LifeTime{} // session manager will handle the rest.
|
||||
}
|
||||
|
||||
untilExpire := db.c.Driver.TTL(sidKey)
|
||||
return sessions.LifeTime{Time: time.Now().Add(untilExpire)}
|
||||
return memstore.LifeTime{Time: time.Now().Add(untilExpire)}
|
||||
}
|
||||
|
||||
// OnUpdateExpiration will re-set the database's session's entry ttl.
|
||||
|
||||
Reference in New Issue
Block a user