Sessions from map[interface{}]interface{} to [string]interface{} - no one used object as keys so I change its type

This commit is contained in:
Makis Maropoulos 2016-07-01 17:00:24 +03:00
parent 950964b18d
commit c6e6c39946
4 changed files with 59 additions and 29 deletions

View File

@ -21,7 +21,7 @@ var (
func register() { func register() {
// the actual work is here. // the actual work is here.
Provider.NewStore = func(sessionId string, cookieLifeDuration time.Duration) store.IStore { Provider.NewStore = func(sessionId string, cookieLifeDuration time.Duration) store.IStore {
return &Store{sid: sessionId, lastAccessedTime: time.Now(), values: make(map[interface{}]interface{}, 0)} return &Store{sid: sessionId, lastAccessedTime: time.Now(), values: make(map[string]interface{}, 0)}
} }
sessions.Register(Provider) sessions.Register(Provider)
} }

View File

@ -1,6 +1,7 @@
package memory package memory
import ( import (
"sync"
"time" "time"
"github.com/kataras/iris/sessions/store" "github.com/kataras/iris/sessions/store"
@ -10,36 +11,42 @@ import (
type Store struct { type Store struct {
sid string sid string
lastAccessedTime time.Time lastAccessedTime time.Time
values map[interface{}]interface{} // here is the real memory store values map[string]interface{} // here is the real memory store
mu sync.Mutex
} }
var _ store.IStore = &Store{} var _ store.IStore = &Store{}
// GetAll returns all values // GetAll returns all values
func (s *Store) GetAll() map[interface{}]interface{} { func (s *Store) GetAll() map[string]interface{} {
s.mu.Lock()
defer s.mu.Unlock()
return s.values return s.values
} }
// VisitAll loop each one entry and calls the callback function func(key,value) // VisitAll loop each one entry and calls the callback function func(key,value)
func (s *Store) VisitAll(cb func(k interface{}, v interface{})) { func (s *Store) VisitAll(cb func(k string, v interface{})) {
s.mu.Lock()
defer s.mu.Unlock()
for key := range s.values { for key := range s.values {
cb(key, s.values[key]) cb(key, s.values[key])
} }
} }
// Get returns the value of an entry by its key // Get returns the value of an entry by its key
func (s *Store) Get(key interface{}) interface{} { func (s *Store) Get(key string) interface{} {
Provider.Update(s.sid) Provider.Update(s.sid)
s.mu.Lock()
if value, found := s.values[key]; found { if value, found := s.values[key]; found {
s.mu.Unlock()
return value return value
} }
s.mu.Unlock()
return nil return nil
} }
// GetString same as Get but returns as string, if nil then returns an empty string // GetString same as Get but returns as string, if nil then returns an empty string
func (s *Store) GetString(key interface{}) string { func (s *Store) GetString(key string) string {
if value := s.Get(key); value != nil { if value := s.Get(key); value != nil {
if v, ok := value.(string); ok { if v, ok := value.(string); ok {
return v return v
@ -51,7 +58,7 @@ func (s *Store) GetString(key interface{}) string {
} }
// GetInt same as Get but returns as int, if nil then returns -1 // GetInt same as Get but returns as int, if nil then returns -1
func (s *Store) GetInt(key interface{}) int { func (s *Store) GetInt(key string) int {
if value := s.Get(key); value != nil { if value := s.Get(key); value != nil {
if v, ok := value.(int); ok { if v, ok := value.(int); ok {
return v return v
@ -63,16 +70,20 @@ func (s *Store) GetInt(key interface{}) int {
// Set fills the session with an entry, it receives a key and a value // Set fills the session with an entry, it receives a key and a value
// returns an error, which is always nil // returns an error, which is always nil
func (s *Store) Set(key interface{}, value interface{}) error { func (s *Store) Set(key string, value interface{}) error {
s.mu.Lock()
s.values[key] = value s.values[key] = value
s.mu.Unlock()
Provider.Update(s.sid) Provider.Update(s.sid)
return nil return nil
} }
// Delete removes an entry by its key // Delete removes an entry by its key
// returns an error, which is always nil // returns an error, which is always nil
func (s *Store) Delete(key interface{}) error { func (s *Store) Delete(key string) error {
s.mu.Lock()
delete(s.values, key) delete(s.values, key)
s.mu.Unlock()
Provider.Update(s.sid) Provider.Update(s.sid)
return nil return nil
} }
@ -80,9 +91,11 @@ func (s *Store) Delete(key interface{}) error {
// Clear removes all entries // Clear removes all entries
// returns an error, which is always nil // returns an error, which is always nil
func (s *Store) Clear() error { func (s *Store) Clear() error {
s.mu.Lock()
for key := range s.values { for key := range s.values {
delete(s.values, key) delete(s.values, key)
} }
s.mu.Unlock()
Provider.Update(s.sid) Provider.Update(s.sid)
return nil return nil
} }
@ -105,7 +118,9 @@ func (s *Store) SetLastAccessedTime(lastacc time.Time) {
// Destroy // Destroy
func (s *Store) Destroy() { func (s *Store) Destroy() {
// clears without provider's update. // clears without provider's update.
s.mu.Lock()
for key := range s.values { for key := range s.values {
delete(s.values, key) delete(s.values, key)
} }
s.mu.Unlock()
} }

View File

@ -1,6 +1,7 @@
package redis package redis
import ( import (
"sync"
"time" "time"
"github.com/kataras/iris/sessions/store" "github.com/kataras/iris/sessions/store"
@ -28,8 +29,8 @@ with the memory provider. Or just have a values field inside the Store and use j
Ok then, let's convert it again. Ok then, let's convert it again.
*/ */
// Values is just a type of a map[interface{}]interface{} // Values is just a type of a map[string]interface{}
type Values map[interface{}]interface{} type Values map[string]interface{}
// Store the redis session store // Store the redis session store
type Store struct { type Store struct {
@ -37,6 +38,7 @@ type Store struct {
lastAccessedTime time.Time lastAccessedTime time.Time
values Values values Values
cookieLifeDuration time.Duration //used on .Set-> SETEX on redis cookieLifeDuration time.Duration //used on .Set-> SETEX on redis
mu sync.Mutex
} }
var _ store.IStore = &Store{} var _ store.IStore = &Store{}
@ -78,30 +80,35 @@ func (s *Store) update() {
} }
// GetAll returns all values // GetAll returns all values
func (s *Store) GetAll() map[interface{}]interface{} { func (s *Store) GetAll() map[string]interface{} {
s.mu.Lock()
defer s.mu.Unlock()
return s.values return s.values
} }
// VisitAll loop each one entry and calls the callback function func(key,value) // VisitAll loop each one entry and calls the callback function func(key,value)
func (s *Store) VisitAll(cb func(k interface{}, v interface{})) { func (s *Store) VisitAll(cb func(k string, v interface{})) {
s.mu.Lock()
defer s.mu.Unlock()
for key := range s.values { for key := range s.values {
cb(key, s.values[key]) cb(key, s.values[key])
} }
} }
// Get returns the value of an entry by its key // Get returns the value of an entry by its key
func (s *Store) Get(key interface{}) interface{} { func (s *Store) Get(key string) interface{} {
Provider.Update(s.sid) Provider.Update(s.sid)
s.mu.Lock()
if value, found := s.values[key]; found { if value, found := s.values[key]; found {
s.mu.Unlock()
return value return value
} }
s.mu.Unlock()
return nil return nil
} }
// GetString same as Get but returns as string, if nil then returns an empty string // GetString same as Get but returns as string, if nil then returns an empty string
func (s *Store) GetString(key interface{}) string { func (s *Store) GetString(key string) string {
if value := s.Get(key); value != nil { if value := s.Get(key); value != nil {
if v, ok := value.(string); ok { if v, ok := value.(string); ok {
return v return v
@ -112,7 +119,7 @@ func (s *Store) GetString(key interface{}) string {
} }
// GetInt same as Get but returns as int, if nil then returns -1 // GetInt same as Get but returns as int, if nil then returns -1
func (s *Store) GetInt(key interface{}) int { func (s *Store) GetInt(key string) int {
if value := s.Get(key); value != nil { if value := s.Get(key); value != nil {
if v, ok := value.(int); ok { if v, ok := value.(int); ok {
return v return v
@ -124,8 +131,10 @@ func (s *Store) GetInt(key interface{}) int {
// Set fills the session with an entry, it receives a key and a value // Set fills the session with an entry, it receives a key and a value
// returns an error, which is always nil // returns an error, which is always nil
func (s *Store) Set(key interface{}, value interface{}) error { func (s *Store) Set(key string, value interface{}) error {
s.mu.Lock()
s.values[key] = value s.values[key] = value
s.mu.Unlock()
Provider.Update(s.sid) Provider.Update(s.sid)
s.update() s.update()
@ -134,8 +143,10 @@ func (s *Store) Set(key interface{}, value interface{}) error {
// Delete removes an entry by its key // Delete removes an entry by its key
// returns an error, which is always nil // returns an error, which is always nil
func (s *Store) Delete(key interface{}) error { func (s *Store) Delete(key string) error {
s.mu.Lock()
delete(s.values, key) delete(s.values, key)
s.mu.Unlock()
Provider.Update(s.sid) Provider.Update(s.sid)
s.update() s.update()
return nil return nil
@ -145,9 +156,11 @@ func (s *Store) Delete(key interface{}) error {
// returns an error, which is always nil // returns an error, which is always nil
func (s *Store) Clear() error { func (s *Store) Clear() error {
//we are not using the Redis.Delete, I made so work for nothing.. we wanted only the .Set at the end... //we are not using the Redis.Delete, I made so work for nothing.. we wanted only the .Set at the end...
s.mu.Lock()
for key := range s.values { for key := range s.values {
delete(s.values, key) delete(s.values, key)
} }
s.mu.Unlock()
Provider.Update(s.sid) Provider.Update(s.sid)
s.update() s.update()
@ -173,8 +186,10 @@ func (s *Store) SetLastAccessedTime(lastacc time.Time) {
func (s *Store) Destroy() { func (s *Store) Destroy() {
// remove the whole value which is the s.values from real redis // remove the whole value which is the s.values from real redis
redis.Delete(s.sid) redis.Delete(s.sid)
s.mu.Lock()
for key := range s.values { for key := range s.values {
delete(s.values, key) delete(s.values, key)
} }
s.mu.Unlock()
} }

View File

@ -5,14 +5,14 @@ import "time"
// IStore is the interface which all session stores should implement // IStore is the interface which all session stores should implement
type IStore interface { type IStore interface {
Get(interface{}) interface{} Get(string) interface{}
GetString(key interface{}) string GetString(string) string
GetInt(key interface{}) int GetInt(string) int
Set(interface{}, interface{}) error Set(string, interface{}) error
Delete(interface{}) error Delete(string) error
Clear() error Clear() error
VisitAll(func(interface{}, interface{})) VisitAll(func(string, interface{}))
GetAll() map[interface{}]interface{} GetAll() map[string]interface{}
ID() string ID() string
LastAccessedTime() time.Time LastAccessedTime() time.Time
SetLastAccessedTime(time.Time) SetLastAccessedTime(time.Time)