本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

204 lines
4.7KB

  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. "fmt"
  18. "strings"
  19. "sync"
  20. "gitea.com/macaron/session"
  21. "github.com/bradfitz/gomemcache/memcache"
  22. )
  23. // MemcacheStore represents a memcache session store implementation.
  24. type MemcacheStore struct {
  25. c *memcache.Client
  26. sid string
  27. expire int32
  28. lock sync.RWMutex
  29. data map[interface{}]interface{}
  30. }
  31. // NewMemcacheStore creates and returns a memcache session store.
  32. func NewMemcacheStore(c *memcache.Client, sid string, expire int32, kv map[interface{}]interface{}) *MemcacheStore {
  33. return &MemcacheStore{
  34. c: c,
  35. sid: sid,
  36. expire: expire,
  37. data: kv,
  38. }
  39. }
  40. func NewItem(sid string, data []byte, expire int32) *memcache.Item {
  41. return &memcache.Item{
  42. Key: sid,
  43. Value: data,
  44. Expiration: expire,
  45. }
  46. }
  47. // Set sets value to given key in session.
  48. func (s *MemcacheStore) Set(key, val interface{}) error {
  49. s.lock.Lock()
  50. defer s.lock.Unlock()
  51. s.data[key] = val
  52. return nil
  53. }
  54. // Get gets value by given key in session.
  55. func (s *MemcacheStore) Get(key interface{}) interface{} {
  56. s.lock.RLock()
  57. defer s.lock.RUnlock()
  58. return s.data[key]
  59. }
  60. // Delete delete a key from session.
  61. func (s *MemcacheStore) Delete(key interface{}) error {
  62. s.lock.Lock()
  63. defer s.lock.Unlock()
  64. delete(s.data, key)
  65. return nil
  66. }
  67. // ID returns current session ID.
  68. func (s *MemcacheStore) ID() string {
  69. return s.sid
  70. }
  71. // Release releases resource and save data to provider.
  72. func (s *MemcacheStore) Release() error {
  73. // Skip encoding if the data is empty
  74. if len(s.data) == 0 {
  75. return nil
  76. }
  77. data, err := session.EncodeGob(s.data)
  78. if err != nil {
  79. return err
  80. }
  81. return s.c.Set(NewItem(s.sid, data, s.expire))
  82. }
  83. // Flush deletes all session data.
  84. func (s *MemcacheStore) Flush() error {
  85. s.lock.Lock()
  86. defer s.lock.Unlock()
  87. s.data = make(map[interface{}]interface{})
  88. return nil
  89. }
  90. // MemcacheProvider represents a memcache session provider implementation.
  91. type MemcacheProvider struct {
  92. c *memcache.Client
  93. expire int32
  94. }
  95. // Init initializes memcache session provider.
  96. // connStrs: 127.0.0.1:9090;127.0.0.1:9091
  97. func (p *MemcacheProvider) Init(expire int64, connStrs string) error {
  98. p.expire = int32(expire)
  99. p.c = memcache.New(strings.Split(connStrs, ";")...)
  100. return nil
  101. }
  102. // Read returns raw session store by session ID.
  103. func (p *MemcacheProvider) Read(sid string) (session.RawStore, error) {
  104. if !p.Exist(sid) {
  105. if err := p.c.Set(NewItem(sid, []byte(""), p.expire)); err != nil {
  106. return nil, err
  107. }
  108. }
  109. var kv map[interface{}]interface{}
  110. item, err := p.c.Get(sid)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if len(item.Value) == 0 {
  115. kv = make(map[interface{}]interface{})
  116. } else {
  117. kv, err = session.DecodeGob(item.Value)
  118. if err != nil {
  119. return nil, err
  120. }
  121. }
  122. return NewMemcacheStore(p.c, sid, p.expire, kv), nil
  123. }
  124. // Exist returns true if session with given ID exists.
  125. func (p *MemcacheProvider) Exist(sid string) bool {
  126. _, err := p.c.Get(sid)
  127. return err == nil
  128. }
  129. // Destroy deletes a session by session ID.
  130. func (p *MemcacheProvider) Destroy(sid string) error {
  131. return p.c.Delete(sid)
  132. }
  133. // Regenerate regenerates a session store from old session ID to new one.
  134. func (p *MemcacheProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) {
  135. if p.Exist(sid) {
  136. return nil, fmt.Errorf("new sid '%s' already exists", sid)
  137. }
  138. item := NewItem(sid, []byte(""), p.expire)
  139. if p.Exist(oldsid) {
  140. item, err = p.c.Get(oldsid)
  141. if err != nil {
  142. return nil, err
  143. } else if err = p.c.Delete(oldsid); err != nil {
  144. return nil, err
  145. }
  146. item.Key = sid
  147. }
  148. if err = p.c.Set(item); err != nil {
  149. return nil, err
  150. }
  151. var kv map[interface{}]interface{}
  152. if len(item.Value) == 0 {
  153. kv = make(map[interface{}]interface{})
  154. } else {
  155. kv, err = session.DecodeGob(item.Value)
  156. if err != nil {
  157. return nil, err
  158. }
  159. }
  160. return NewMemcacheStore(p.c, sid, p.expire, kv), nil
  161. }
  162. // Count counts and returns number of sessions.
  163. func (p *MemcacheProvider) Count() int {
  164. // FIXME: how come this library does not have Stats method?
  165. return -1
  166. }
  167. // GC calls GC to clean expired sessions.
  168. func (p *MemcacheProvider) GC() {}
  169. func init() {
  170. session.Register("memcache", &MemcacheProvider{})
  171. }
上海开阖软件有限公司 沪ICP备12045867号-1