mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
parent
9724592697
commit
3f98b39632
|
@ -36,9 +36,11 @@ func NewApp(sess *sessions.Sessions) *iris.Application {
|
||||||
// set session values.
|
// set session values.
|
||||||
app.Get("/set", func(ctx iris.Context) {
|
app.Get("/set", func(ctx iris.Context) {
|
||||||
session := sessions.Get(ctx)
|
session := sessions.Get(ctx)
|
||||||
|
isNew := session.IsNew()
|
||||||
|
|
||||||
session.Set("name", "iris")
|
session.Set("name", "iris")
|
||||||
|
|
||||||
ctx.Writef("All ok session set to: %s", session.GetString("name"))
|
ctx.Writef("All ok session set to: %s [isNew=%t]", session.GetString("name"), isNew)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/get", func(ctx iris.Context) {
|
app.Get("/get", func(ctx iris.Context) {
|
||||||
|
@ -68,9 +70,11 @@ func NewApp(sess *sessions.Sessions) *iris.Application {
|
||||||
|
|
||||||
key := ctx.Params().Get("key")
|
key := ctx.Params().Get("key")
|
||||||
value := ctx.Params().Get("value")
|
value := ctx.Params().Get("value")
|
||||||
|
isNew := session.IsNew()
|
||||||
|
|
||||||
session.Set(key, value)
|
session.Set(key, value)
|
||||||
|
|
||||||
ctx.Writef("All ok session value of the '%s' is: %s", key, session.GetString(key))
|
ctx.Writef("All ok session value of the '%s' is: %s [isNew=%t]", key, session.GetString(key), isNew)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/get/{key}", func(ctx iris.Context) {
|
app.Get("/get/{key}", func(ctx iris.Context) {
|
||||||
|
|
|
@ -663,27 +663,22 @@ func TestApplicationDependency(t *testing.T) {
|
||||||
// Authenticated type.
|
// Authenticated type.
|
||||||
type Authenticated int64
|
type Authenticated int64
|
||||||
|
|
||||||
// BasePublicPrivateController base controller between public and private controllers.
|
// BasePrivateController base controller for private controllers.
|
||||||
type BasePublicPrivateController struct {
|
type BasePrivateController struct {
|
||||||
CurrentUserID Authenticated
|
CurrentUserID Authenticated
|
||||||
Ctx iris.Context
|
Ctx iris.Context // not-used.
|
||||||
}
|
}
|
||||||
|
|
||||||
type publicController struct {
|
type publicController struct {
|
||||||
Ctx iris.Context
|
Ctx iris.Context // not-used.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get desc
|
|
||||||
// Route / [GET]
|
|
||||||
func (c *publicController) Get() iris.Map {
|
func (c *publicController) Get() iris.Map {
|
||||||
return iris.Map{"data": "things"}
|
return iris.Map{"data": "things"}
|
||||||
}
|
}
|
||||||
|
|
||||||
// privateController serves the "public-private" Customer API.
|
type privateController struct{ BasePrivateController }
|
||||||
type privateController struct{ BasePublicPrivateController }
|
|
||||||
|
|
||||||
// Get desc
|
|
||||||
// Route / [GET]
|
|
||||||
func (c *privateController) Get() iris.Map {
|
func (c *privateController) Get() iris.Map {
|
||||||
return iris.Map{"id": c.CurrentUserID}
|
return iris.Map{"id": c.CurrentUserID}
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,6 +156,12 @@ func (db *Database) Get(sid string, key string) (value interface{}) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validSessionItem reports whether the current iterator's item key
|
||||||
|
// is a value of the session id "prefix".
|
||||||
|
func validSessionItem(key, prefix []byte) bool {
|
||||||
|
return len(key) > len(prefix) && bytes.Equal(key[0:len(prefix)], prefix)
|
||||||
|
}
|
||||||
|
|
||||||
// Visit loops through all session keys and values.
|
// Visit loops through all session keys and values.
|
||||||
func (db *Database) Visit(sid string, cb func(key string, value interface{})) {
|
func (db *Database) Visit(sid string, cb func(key string, value interface{})) {
|
||||||
prefix := makePrefix(sid)
|
prefix := makePrefix(sid)
|
||||||
|
@ -166,30 +172,28 @@ func (db *Database) Visit(sid string, cb func(key string, value interface{})) {
|
||||||
iter := txn.NewIterator(badger.DefaultIteratorOptions)
|
iter := txn.NewIterator(badger.DefaultIteratorOptions)
|
||||||
defer iter.Close()
|
defer iter.Close()
|
||||||
|
|
||||||
for iter.Rewind(); iter.ValidForPrefix(prefix); iter.Next() {
|
for iter.Rewind(); ; iter.Next() {
|
||||||
|
if !iter.Valid() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
item := iter.Item()
|
item := iter.Item()
|
||||||
|
key := item.Key()
|
||||||
|
if !validSessionItem(key, prefix) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
var value interface{}
|
var value interface{}
|
||||||
|
|
||||||
// err := item.Value(func(valueBytes []byte) {
|
|
||||||
// if err := sessions.DefaultTranscoder.Unmarshal(valueBytes, &value); err != nil {
|
|
||||||
// golog.Error(err)
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
|
|
||||||
// if err != nil {
|
|
||||||
// golog.Error(err)
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
|
|
||||||
err := item.Value(func(valueBytes []byte) error {
|
err := item.Value(func(valueBytes []byte) error {
|
||||||
return sessions.DefaultTranscoder.Unmarshal(valueBytes, &value)
|
return sessions.DefaultTranscoder.Unmarshal(valueBytes, &value)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
golog.Error(err)
|
golog.Errorf("[sessionsdb.badger.Visit] %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
cb(string(bytes.TrimPrefix(item.Key(), prefix)), value)
|
cb(string(bytes.TrimPrefix(key, prefix)), value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,8 +211,14 @@ func (db *Database) Len(sid string) (n int) {
|
||||||
txn := db.Service.NewTransaction(false)
|
txn := db.Service.NewTransaction(false)
|
||||||
iter := txn.NewIterator(iterOptionsNoValues)
|
iter := txn.NewIterator(iterOptionsNoValues)
|
||||||
|
|
||||||
for iter.Rewind(); iter.ValidForPrefix(prefix); iter.Next() {
|
for iter.Rewind(); ; iter.Next() {
|
||||||
n++
|
if !iter.Valid() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if validSessionItem(iter.Item().Key(), prefix) {
|
||||||
|
n++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
iter.Close()
|
iter.Close()
|
||||||
|
|
|
@ -263,7 +263,12 @@ func (r *RadixDriver) getKeys(cursor, prefix string) ([]string, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := res.keys[0:]
|
if len(res.keys) <= 1 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := res.keys[1:] // the first one is always the session id itself.
|
||||||
|
|
||||||
if res.cur != "0" {
|
if res.cur != "0" {
|
||||||
moreKeys, err := r.getKeys(res.cur, prefix)
|
moreKeys, err := r.getKeys(res.cur, prefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -205,10 +205,19 @@ func (r *RedigoDriver) getKeysConn(c redis.Conn, cursor interface{}, prefix stri
|
||||||
if len(replies) == 2 {
|
if len(replies) == 2 {
|
||||||
// take the second, it must contain the slice of keys.
|
// take the second, it must contain the slice of keys.
|
||||||
if keysSliceAsBytes, ok := replies[1].([]interface{}); ok {
|
if keysSliceAsBytes, ok := replies[1].([]interface{}); ok {
|
||||||
keys := make([]string, len(keysSliceAsBytes))
|
n := len(keysSliceAsBytes) - 1 // scan match returns the session id key too.
|
||||||
|
if n <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := make([]string, n)
|
||||||
|
|
||||||
for i, k := range keysSliceAsBytes {
|
for i, k := range keysSliceAsBytes {
|
||||||
keys[i] = fmt.Sprintf("%s", k)[len(r.Config.Prefix):]
|
key := fmt.Sprintf("%s", k)[len(r.Config.Prefix):]
|
||||||
|
if key == prefix {
|
||||||
|
continue // it's the session id itself.
|
||||||
|
}
|
||||||
|
keys[i] = key
|
||||||
}
|
}
|
||||||
|
|
||||||
if cur := fmt.Sprintf("%s", replies[0]); cur != "0" {
|
if cur := fmt.Sprintf("%s", replies[0]); cur != "0" {
|
||||||
|
|
|
@ -83,6 +83,13 @@ func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOpt
|
||||||
sid := s.config.SessionIDGenerator(ctx)
|
sid := s.config.SessionIDGenerator(ctx)
|
||||||
|
|
||||||
sess := s.provider.Init(s, sid, s.config.Expires)
|
sess := s.provider.Init(s, sid, s.config.Expires)
|
||||||
|
// n := s.provider.db.Len(sid)
|
||||||
|
// fmt.Printf("db.Len(%s) = %d\n", sid, n)
|
||||||
|
// if n > 0 {
|
||||||
|
// s.provider.db.Visit(sid, func(key string, value interface{}) {
|
||||||
|
// fmt.Printf("%s=%s\n", key, value)
|
||||||
|
// })
|
||||||
|
// }
|
||||||
sess.isNew = s.provider.db.Len(sid) == 0
|
sess.isNew = s.provider.db.Len(sid) == 0
|
||||||
|
|
||||||
s.updateCookie(ctx, sid, s.config.Expires, cookieOptions...)
|
s.updateCookie(ctx, sid, s.config.Expires, cookieOptions...)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user