You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
4.8 KiB

  1. // Copyright 2013 Beego Authors
  2. // Copyright 2014 The Macaron Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. package session
  16. import (
  17. "container/list"
  18. "fmt"
  19. "sync"
  20. "time"
  21. )
  22. // MemStore represents a in-memory session store implementation.
  23. type MemStore struct {
  24. sid string
  25. lock sync.RWMutex
  26. data map[interface{}]interface{}
  27. lastAccess time.Time
  28. }
  29. // NewMemStore creates and returns a memory session store.
  30. func NewMemStore(sid string) *MemStore {
  31. return &MemStore{
  32. sid: sid,
  33. data: make(map[interface{}]interface{}),
  34. lastAccess: time.Now(),
  35. }
  36. }
  37. // Set sets value to given key in session.
  38. func (s *MemStore) Set(key, val interface{}) error {
  39. s.lock.Lock()
  40. defer s.lock.Unlock()
  41. s.data[key] = val
  42. return nil
  43. }
  44. // Get gets value by given key in session.
  45. func (s *MemStore) Get(key interface{}) interface{} {
  46. s.lock.RLock()
  47. defer s.lock.RUnlock()
  48. return s.data[key]
  49. }
  50. // Delete deletes a key from session.
  51. func (s *MemStore) Delete(key interface{}) error {
  52. s.lock.Lock()
  53. defer s.lock.Unlock()
  54. delete(s.data, key)
  55. return nil
  56. }
  57. // ID returns current session ID.
  58. func (s *MemStore) ID() string {
  59. return s.sid
  60. }
  61. // Release releases resource and save data to provider.
  62. func (_ *MemStore) Release() error {
  63. return nil
  64. }
  65. // Flush deletes all session data.
  66. func (s *MemStore) Flush() error {
  67. s.lock.Lock()
  68. defer s.lock.Unlock()
  69. s.data = make(map[interface{}]interface{})
  70. return nil
  71. }
  72. // MemProvider represents a in-memory session provider implementation.
  73. type MemProvider struct {
  74. lock sync.RWMutex
  75. maxLifetime int64
  76. data map[string]*list.Element
  77. // A priority list whose lastAccess newer gets higer priority.
  78. list *list.List
  79. }
  80. // Init initializes memory session provider.
  81. func (p *MemProvider) Init(maxLifetime int64, _ string) error {
  82. p.lock.Lock()
  83. p.maxLifetime = maxLifetime
  84. p.lock.Unlock()
  85. return nil
  86. }
  87. // update expands time of session store by given ID.
  88. func (p *MemProvider) update(sid string) error {
  89. p.lock.Lock()
  90. defer p.lock.Unlock()
  91. if e, ok := p.data[sid]; ok {
  92. e.Value.(*MemStore).lastAccess = time.Now()
  93. p.list.MoveToFront(e)
  94. return nil
  95. }
  96. return nil
  97. }
  98. // Read returns raw session store by session ID.
  99. func (p *MemProvider) Read(sid string) (_ RawStore, err error) {
  100. p.lock.RLock()
  101. e, ok := p.data[sid]
  102. p.lock.RUnlock()
  103. if ok {
  104. if err = p.update(sid); err != nil {
  105. return nil, err
  106. }
  107. return e.Value.(*MemStore), nil
  108. }
  109. // Create a new session.
  110. p.lock.Lock()
  111. defer p.lock.Unlock()
  112. s := NewMemStore(sid)
  113. p.data[sid] = p.list.PushBack(s)
  114. return s, nil
  115. }
  116. // Exist returns true if session with given ID exists.
  117. func (p *MemProvider) Exist(sid string) bool {
  118. p.lock.RLock()
  119. defer p.lock.RUnlock()
  120. _, ok := p.data[sid]
  121. return ok
  122. }
  123. // Destory deletes a session by session ID.
  124. func (p *MemProvider) Destory(sid string) error {
  125. p.lock.Lock()
  126. defer p.lock.Unlock()
  127. e, ok := p.data[sid]
  128. if !ok {
  129. return nil
  130. }
  131. p.list.Remove(e)
  132. delete(p.data, sid)
  133. return nil
  134. }
  135. // Regenerate regenerates a session store from old session ID to new one.
  136. func (p *MemProvider) Regenerate(oldsid, sid string) (RawStore, error) {
  137. if p.Exist(sid) {
  138. return nil, fmt.Errorf("new sid '%s' already exists", sid)
  139. }
  140. s, err := p.Read(oldsid)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if err = p.Destory(oldsid); err != nil {
  145. return nil, err
  146. }
  147. s.(*MemStore).sid = sid
  148. p.lock.Lock()
  149. defer p.lock.Unlock()
  150. p.data[sid] = p.list.PushBack(s)
  151. return s, nil
  152. }
  153. // Count counts and returns number of sessions.
  154. func (p *MemProvider) Count() int {
  155. return p.list.Len()
  156. }
  157. // GC calls GC to clean expired sessions.
  158. func (p *MemProvider) GC() {
  159. p.lock.RLock()
  160. for {
  161. // No session in the list.
  162. e := p.list.Back()
  163. if e == nil {
  164. break
  165. }
  166. if (e.Value.(*MemStore).lastAccess.Unix() + p.maxLifetime) < time.Now().Unix() {
  167. p.lock.RUnlock()
  168. p.lock.Lock()
  169. p.list.Remove(e)
  170. delete(p.data, e.Value.(*MemStore).sid)
  171. p.lock.Unlock()
  172. p.lock.RLock()
  173. } else {
  174. break
  175. }
  176. }
  177. p.lock.RUnlock()
  178. }
  179. func init() {
  180. Register("memory", &MemProvider{list: list.New(), data: make(map[string]*list.Element)})
  181. }