mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 04:51:56 +00:00
add locks on the MemoryService of the vue mvc example, keep it simple but runnable:P
Former-commit-id: 5d661f401e486a26d66f5c0c297f5cf897fc1834
This commit is contained in:
@@ -1,20 +1,33 @@
|
||||
package todo
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
Get(owner string) []Item
|
||||
Save(owner string, newItems []Item) error
|
||||
}
|
||||
|
||||
type MemoryService struct {
|
||||
// key = session id, value the list of todo items that this session id has.
|
||||
items map[string][]Item
|
||||
// protected by locker for concurrent access.
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryService() *MemoryService {
|
||||
return &MemoryService{make(map[string][]Item, 0)}
|
||||
return &MemoryService{
|
||||
items: make(map[string][]Item, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryService) Get(sessionOwner string) (items []Item) {
|
||||
return s.items[sessionOwner]
|
||||
func (s *MemoryService) Get(sessionOwner string) []Item {
|
||||
s.mu.RLock()
|
||||
items := s.items[sessionOwner]
|
||||
s.mu.RUnlock()
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
func (s *MemoryService) Save(sessionOwner string, newItems []Item) error {
|
||||
@@ -26,6 +39,8 @@ func (s *MemoryService) Save(sessionOwner string, newItems []Item) error {
|
||||
}
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
s.items[sessionOwner] = newItems
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user