couchbase.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "strings"
  18. "sync"
  19. "github.com/couchbaselabs/go-couchbase"
  20. "github.com/go-macaron/session"
  21. )
  22. // CouchbaseSessionStore represents a couchbase session store implementation.
  23. type CouchbaseSessionStore struct {
  24. b *couchbase.Bucket
  25. sid string
  26. lock sync.RWMutex
  27. data map[interface{}]interface{}
  28. maxlifetime int64
  29. }
  30. // Set sets value to given key in session.
  31. func (s *CouchbaseSessionStore) Set(key, val interface{}) error {
  32. s.lock.Lock()
  33. defer s.lock.Unlock()
  34. s.data[key] = val
  35. return nil
  36. }
  37. // Get gets value by given key in session.
  38. func (s *CouchbaseSessionStore) Get(key interface{}) interface{} {
  39. s.lock.RLock()
  40. defer s.lock.RUnlock()
  41. return s.data[key]
  42. }
  43. // Delete delete a key from session.
  44. func (s *CouchbaseSessionStore) Delete(key interface{}) error {
  45. s.lock.Lock()
  46. defer s.lock.Unlock()
  47. delete(s.data, key)
  48. return nil
  49. }
  50. // ID returns current session ID.
  51. func (s *CouchbaseSessionStore) ID() string {
  52. return s.sid
  53. }
  54. // Release releases resource and save data to provider.
  55. func (s *CouchbaseSessionStore) Release() error {
  56. defer s.b.Close()
  57. data, err := session.EncodeGob(s.data)
  58. if err != nil {
  59. return err
  60. }
  61. return s.b.Set(s.sid, int(s.maxlifetime), data)
  62. }
  63. // Flush deletes all session data.
  64. func (s *CouchbaseSessionStore) Flush() error {
  65. s.lock.Lock()
  66. defer s.lock.Unlock()
  67. s.data = make(map[interface{}]interface{})
  68. return nil
  69. }
  70. // CouchbaseProvider represents a couchbase session provider implementation.
  71. type CouchbaseProvider struct {
  72. maxlifetime int64
  73. connStr string
  74. pool string
  75. bucket string
  76. b *couchbase.Bucket
  77. }
  78. func (cp *CouchbaseProvider) getBucket() *couchbase.Bucket {
  79. c, err := couchbase.Connect(cp.connStr)
  80. if err != nil {
  81. return nil
  82. }
  83. pool, err := c.GetPool(cp.pool)
  84. if err != nil {
  85. return nil
  86. }
  87. bucket, err := pool.GetBucket(cp.bucket)
  88. if err != nil {
  89. return nil
  90. }
  91. return bucket
  92. }
  93. // Init initializes memory session provider.
  94. // connStr is couchbase server REST/JSON URL
  95. // e.g. http://host:port/, Pool, Bucket
  96. func (p *CouchbaseProvider) Init(maxlifetime int64, connStr string) error {
  97. p.maxlifetime = maxlifetime
  98. configs := strings.Split(connStr, ",")
  99. if len(configs) > 0 {
  100. p.connStr = configs[0]
  101. }
  102. if len(configs) > 1 {
  103. p.pool = configs[1]
  104. }
  105. if len(configs) > 2 {
  106. p.bucket = configs[2]
  107. }
  108. return nil
  109. }
  110. // Read returns raw session store by session ID.
  111. func (p *CouchbaseProvider) Read(sid string) (session.RawStore, error) {
  112. p.b = p.getBucket()
  113. var doc []byte
  114. err := p.b.Get(sid, &doc)
  115. var kv map[interface{}]interface{}
  116. if doc == nil {
  117. kv = make(map[interface{}]interface{})
  118. } else {
  119. kv, err = session.DecodeGob(doc)
  120. if err != nil {
  121. return nil, err
  122. }
  123. }
  124. cs := &CouchbaseSessionStore{b: p.b, sid: sid, data: kv, maxlifetime: p.maxlifetime}
  125. return cs, nil
  126. }
  127. // Exist returns true if session with given ID exists.
  128. func (p *CouchbaseProvider) Exist(sid string) bool {
  129. p.b = p.getBucket()
  130. defer p.b.Close()
  131. var doc []byte
  132. if err := p.b.Get(sid, &doc); err != nil || doc == nil {
  133. return false
  134. } else {
  135. return true
  136. }
  137. }
  138. // Destory deletes a session by session ID.
  139. func (p *CouchbaseProvider) Destory(sid string) error {
  140. p.b = p.getBucket()
  141. defer p.b.Close()
  142. p.b.Delete(sid)
  143. return nil
  144. }
  145. // Regenerate regenerates a session store from old session ID to new one.
  146. func (p *CouchbaseProvider) Regenerate(oldsid, sid string) (session.RawStore, error) {
  147. p.b = p.getBucket()
  148. var doc []byte
  149. if err := p.b.Get(oldsid, &doc); err != nil || doc == nil {
  150. p.b.Set(sid, int(p.maxlifetime), "")
  151. } else {
  152. err := p.b.Delete(oldsid)
  153. if err != nil {
  154. return nil, err
  155. }
  156. _, _ = p.b.Add(sid, int(p.maxlifetime), doc)
  157. }
  158. err := p.b.Get(sid, &doc)
  159. if err != nil {
  160. return nil, err
  161. }
  162. var kv map[interface{}]interface{}
  163. if doc == nil {
  164. kv = make(map[interface{}]interface{})
  165. } else {
  166. kv, err = session.DecodeGob(doc)
  167. if err != nil {
  168. return nil, err
  169. }
  170. }
  171. cs := &CouchbaseSessionStore{b: p.b, sid: sid, data: kv, maxlifetime: p.maxlifetime}
  172. return cs, nil
  173. }
  174. // Count counts and returns number of sessions.
  175. func (p *CouchbaseProvider) Count() int {
  176. // FIXME
  177. return 0
  178. }
  179. // GC calls GC to clean expired sessions.
  180. func (p *CouchbaseProvider) GC() {}
  181. func init() {
  182. session.Register("couchbase", &CouchbaseProvider{})
  183. }