sess_ssdb.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package ssdb
  2. import (
  3. "errors"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "github.com/astaxie/beego/session"
  9. "github.com/ssdb/gossdb/ssdb"
  10. )
  11. var ssdbProvider = &SsdbProvider{}
  12. type SsdbProvider struct {
  13. client *ssdb.Client
  14. host string
  15. port int
  16. maxLifetime int64
  17. }
  18. func (p *SsdbProvider) connectInit() error {
  19. var err error
  20. if p.host == "" || p.port == 0 {
  21. return errors.New("SessionInit First")
  22. }
  23. p.client, err = ssdb.Connect(p.host, p.port)
  24. if err != nil {
  25. return err
  26. }
  27. return nil
  28. }
  29. func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error {
  30. var e error = nil
  31. p.maxLifetime = maxLifetime
  32. address := strings.Split(savePath, ":")
  33. p.host = address[0]
  34. p.port, e = strconv.Atoi(address[1])
  35. if e != nil {
  36. return e
  37. }
  38. err := p.connectInit()
  39. if err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) {
  45. if p.client == nil {
  46. if err := p.connectInit(); err != nil {
  47. return nil, err
  48. }
  49. }
  50. var kv map[interface{}]interface{}
  51. value, err := p.client.Get(sid)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if value == nil || len(value.(string)) == 0 {
  56. kv = make(map[interface{}]interface{})
  57. } else {
  58. kv, err = session.DecodeGob([]byte(value.(string)))
  59. if err != nil {
  60. return nil, err
  61. }
  62. }
  63. rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
  64. return rs, nil
  65. }
  66. func (p *SsdbProvider) SessionExist(sid string) bool {
  67. if p.client == nil {
  68. if err := p.connectInit(); err != nil {
  69. panic(err)
  70. }
  71. }
  72. value, err := p.client.Get(sid)
  73. if err != nil {
  74. panic(err)
  75. }
  76. if value == nil || len(value.(string)) == 0 {
  77. return false
  78. }
  79. return true
  80. }
  81. func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
  82. //conn.Do("setx", key, v, ttl)
  83. if p.client == nil {
  84. if err := p.connectInit(); err != nil {
  85. return nil, err
  86. }
  87. }
  88. value, err := p.client.Get(oldsid)
  89. if err != nil {
  90. return nil, err
  91. }
  92. var kv map[interface{}]interface{}
  93. if value == nil || len(value.(string)) == 0 {
  94. kv = make(map[interface{}]interface{})
  95. } else {
  96. kv, err = session.DecodeGob([]byte(value.(string)))
  97. if err != nil {
  98. return nil, err
  99. }
  100. _, err = p.client.Del(oldsid)
  101. if err != nil {
  102. return nil, err
  103. }
  104. }
  105. _, e := p.client.Do("setx", sid, value, p.maxLifetime)
  106. if e != nil {
  107. return nil, e
  108. }
  109. rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
  110. return rs, nil
  111. }
  112. func (p *SsdbProvider) SessionDestroy(sid string) error {
  113. if p.client == nil {
  114. if err := p.connectInit(); err != nil {
  115. return err
  116. }
  117. }
  118. _, err := p.client.Del(sid)
  119. if err != nil {
  120. return err
  121. }
  122. return nil
  123. }
  124. func (p *SsdbProvider) SessionGC() {
  125. return
  126. }
  127. func (p *SsdbProvider) SessionAll() int {
  128. return 0
  129. }
  130. type SessionStore struct {
  131. sid string
  132. lock sync.RWMutex
  133. values map[interface{}]interface{}
  134. maxLifetime int64
  135. client *ssdb.Client
  136. }
  137. func (s *SessionStore) Set(key, value interface{}) error {
  138. s.lock.Lock()
  139. defer s.lock.Unlock()
  140. s.values[key] = value
  141. return nil
  142. }
  143. func (s *SessionStore) Get(key interface{}) interface{} {
  144. s.lock.Lock()
  145. defer s.lock.Unlock()
  146. if value, ok := s.values[key]; ok {
  147. return value
  148. }
  149. return nil
  150. }
  151. func (s *SessionStore) Delete(key interface{}) error {
  152. s.lock.Lock()
  153. defer s.lock.Unlock()
  154. delete(s.values, key)
  155. return nil
  156. }
  157. func (s *SessionStore) Flush() error {
  158. s.lock.Lock()
  159. defer s.lock.Unlock()
  160. s.values = make(map[interface{}]interface{})
  161. return nil
  162. }
  163. func (s *SessionStore) SessionID() string {
  164. return s.sid
  165. }
  166. func (s *SessionStore) SessionRelease(w http.ResponseWriter) {
  167. b, err := session.EncodeGob(s.values)
  168. if err != nil {
  169. return
  170. }
  171. s.client.Do("setx", s.sid, string(b), s.maxLifetime)
  172. }
  173. func init() {
  174. session.Register("ssdb", ssdbProvider)
  175. }