本站源代码
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.

208 lines
4.6KB

  1. // Copyright 2015 The Macaron Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package session
  15. import (
  16. "fmt"
  17. "sync"
  18. "gitea.com/macaron/session"
  19. "github.com/lunny/nodb"
  20. "github.com/lunny/nodb/config"
  21. )
  22. // NodbStore represents a nodb session store implementation.
  23. type NodbStore struct {
  24. c *nodb.DB
  25. sid string
  26. expire int64
  27. lock sync.RWMutex
  28. data map[interface{}]interface{}
  29. }
  30. // NewNodbStore creates and returns a ledis session store.
  31. func NewNodbStore(c *nodb.DB, sid string, expire int64, kv map[interface{}]interface{}) *NodbStore {
  32. return &NodbStore{
  33. c: c,
  34. expire: expire,
  35. sid: sid,
  36. data: kv,
  37. }
  38. }
  39. // Set sets value to given key in session.
  40. func (s *NodbStore) Set(key, val interface{}) error {
  41. s.lock.Lock()
  42. defer s.lock.Unlock()
  43. s.data[key] = val
  44. return nil
  45. }
  46. // Get gets value by given key in session.
  47. func (s *NodbStore) Get(key interface{}) interface{} {
  48. s.lock.RLock()
  49. defer s.lock.RUnlock()
  50. return s.data[key]
  51. }
  52. // Delete delete a key from session.
  53. func (s *NodbStore) Delete(key interface{}) error {
  54. s.lock.Lock()
  55. defer s.lock.Unlock()
  56. delete(s.data, key)
  57. return nil
  58. }
  59. // ID returns current session ID.
  60. func (s *NodbStore) ID() string {
  61. return s.sid
  62. }
  63. // Release releases resource and save data to provider.
  64. func (s *NodbStore) Release() error {
  65. // Skip encoding if the data is empty
  66. if len(s.data) == 0 {
  67. return nil
  68. }
  69. data, err := session.EncodeGob(s.data)
  70. if err != nil {
  71. return err
  72. }
  73. if err = s.c.Set([]byte(s.sid), data); err != nil {
  74. return err
  75. }
  76. _, err = s.c.Expire([]byte(s.sid), s.expire)
  77. return err
  78. }
  79. // Flush deletes all session data.
  80. func (s *NodbStore) Flush() error {
  81. s.lock.Lock()
  82. defer s.lock.Unlock()
  83. s.data = make(map[interface{}]interface{})
  84. return nil
  85. }
  86. // NodbProvider represents a ledis session provider implementation.
  87. type NodbProvider struct {
  88. c *nodb.DB
  89. expire int64
  90. }
  91. // Init initializes nodb session provider.
  92. func (p *NodbProvider) Init(expire int64, configs string) error {
  93. p.expire = expire
  94. cfg := new(config.Config)
  95. cfg.DataDir = configs
  96. dbs, err := nodb.Open(cfg)
  97. if err != nil {
  98. return fmt.Errorf("session/nodb: error opening db: %v", err)
  99. }
  100. p.c, err = dbs.Select(0)
  101. return err
  102. }
  103. // Read returns raw session store by session ID.
  104. func (p *NodbProvider) Read(sid string) (session.RawStore, error) {
  105. if !p.Exist(sid) {
  106. if err := p.c.Set([]byte(sid), []byte("")); err != nil {
  107. return nil, err
  108. }
  109. }
  110. var kv map[interface{}]interface{}
  111. kvs, err := p.c.Get([]byte(sid))
  112. if err != nil {
  113. return nil, err
  114. }
  115. if len(kvs) == 0 {
  116. kv = make(map[interface{}]interface{})
  117. } else {
  118. kv, err = session.DecodeGob(kvs)
  119. if err != nil {
  120. return nil, err
  121. }
  122. }
  123. return NewNodbStore(p.c, sid, p.expire, kv), nil
  124. }
  125. // Exist returns true if session with given ID exists.
  126. func (p *NodbProvider) Exist(sid string) bool {
  127. count, err := p.c.Exists([]byte(sid))
  128. return err == nil && count > 0
  129. }
  130. // Destroy deletes a session by session ID.
  131. func (p *NodbProvider) Destroy(sid string) error {
  132. _, err := p.c.Del([]byte(sid))
  133. return err
  134. }
  135. // Regenerate regenerates a session store from old session ID to new one.
  136. func (p *NodbProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) {
  137. if p.Exist(sid) {
  138. return nil, fmt.Errorf("new sid '%s' already exists", sid)
  139. }
  140. kvs := make([]byte, 0)
  141. if p.Exist(oldsid) {
  142. if kvs, err = p.c.Get([]byte(oldsid)); err != nil {
  143. return nil, err
  144. } else if _, err = p.c.Del([]byte(oldsid)); err != nil {
  145. return nil, err
  146. }
  147. }
  148. if err = p.c.Set([]byte(sid), kvs); err != nil {
  149. return nil, err
  150. } else if _, err = p.c.Expire([]byte(sid), p.expire); err != nil {
  151. return nil, err
  152. }
  153. var kv map[interface{}]interface{}
  154. if len(kvs) == 0 {
  155. kv = make(map[interface{}]interface{})
  156. } else {
  157. kv, err = session.DecodeGob([]byte(kvs))
  158. if err != nil {
  159. return nil, err
  160. }
  161. }
  162. return NewNodbStore(p.c, sid, p.expire, kv), nil
  163. }
  164. // Count counts and returns number of sessions.
  165. func (p *NodbProvider) Count() int {
  166. // FIXME: how come this library does not have DbSize() method?
  167. return -1
  168. }
  169. // GC calls GC to clean expired sessions.
  170. func (p *NodbProvider) GC() {}
  171. func init() {
  172. session.Register("nodb", &NodbProvider{})
  173. }
上海开阖软件有限公司 沪ICP备12045867号-1