Skip to content

Commit

Permalink
Merge pull request #81 from lxzan/dev
Browse files Browse the repository at this point in the history
switch mutex
  • Loading branch information
lxzan authored Feb 19, 2024
2 parents 340485a + 44b1e74 commit be5b1fd
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions session_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

type SessionStorage interface {
Len() int
Load(key string) (value any, exist bool)
Delete(key string)
Store(key string, value any)
Expand All @@ -17,19 +18,19 @@ type SessionStorage interface {
func newSmap() *smap { return &smap{data: make(map[string]any)} }

type smap struct {
sync.RWMutex
sync.Mutex
data map[string]any
}

func (c *smap) Len() int {
c.RLock()
defer c.RUnlock()
c.Lock()
defer c.Unlock()
return len(c.data)
}

func (c *smap) Load(key string) (value any, exist bool) {
c.RLock()
defer c.RUnlock()
c.Lock()
defer c.Unlock()
value, exist = c.data[key]
return
}
Expand All @@ -47,8 +48,8 @@ func (c *smap) Store(key string, value any) {
}

func (c *smap) Range(f func(key string, value any) bool) {
c.RLock()
defer c.RUnlock()
c.Lock()
defer c.Unlock()

for k, v := range c.data {
if !f(k, v) {
Expand Down

0 comments on commit be5b1fd

Please sign in to comment.