ledis_session.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Package ledis provide session Provider
  2. package ledis
  3. import (
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "github.com/astaxie/beego/session"
  9. "github.com/siddontang/ledisdb/config"
  10. "github.com/siddontang/ledisdb/ledis"
  11. )
  12. var ledispder = &Provider{}
  13. var c *ledis.DB
  14. // SessionStore ledis session store
  15. type SessionStore struct {
  16. sid string
  17. lock sync.RWMutex
  18. values map[interface{}]interface{}
  19. maxlifetime int64
  20. }
  21. // Set value in ledis session
  22. func (ls *SessionStore) Set(key, value interface{}) error {
  23. ls.lock.Lock()
  24. defer ls.lock.Unlock()
  25. ls.values[key] = value
  26. return nil
  27. }
  28. // Get value in ledis session
  29. func (ls *SessionStore) Get(key interface{}) interface{} {
  30. ls.lock.RLock()
  31. defer ls.lock.RUnlock()
  32. if v, ok := ls.values[key]; ok {
  33. return v
  34. }
  35. return nil
  36. }
  37. // Delete value in ledis session
  38. func (ls *SessionStore) Delete(key interface{}) error {
  39. ls.lock.Lock()
  40. defer ls.lock.Unlock()
  41. delete(ls.values, key)
  42. return nil
  43. }
  44. // Flush clear all values in ledis session
  45. func (ls *SessionStore) Flush() error {
  46. ls.lock.Lock()
  47. defer ls.lock.Unlock()
  48. ls.values = make(map[interface{}]interface{})
  49. return nil
  50. }
  51. // SessionID get ledis session id
  52. func (ls *SessionStore) SessionID() string {
  53. return ls.sid
  54. }
  55. // SessionRelease save session values to ledis
  56. func (ls *SessionStore) SessionRelease(w http.ResponseWriter) {
  57. b, err := session.EncodeGob(ls.values)
  58. if err != nil {
  59. return
  60. }
  61. c.Set([]byte(ls.sid), b)
  62. c.Expire([]byte(ls.sid), ls.maxlifetime)
  63. }
  64. // Provider ledis session provider
  65. type Provider struct {
  66. maxlifetime int64
  67. savePath string
  68. db int
  69. }
  70. // SessionInit init ledis session
  71. // savepath like ledis server saveDataPath,pool size
  72. // e.g. 127.0.0.1:6379,100,astaxie
  73. func (lp *Provider) SessionInit(maxlifetime int64, savePath string) error {
  74. var err error
  75. lp.maxlifetime = maxlifetime
  76. configs := strings.Split(savePath, ",")
  77. if len(configs) == 1 {
  78. lp.savePath = configs[0]
  79. } else if len(configs) == 2 {
  80. lp.savePath = configs[0]
  81. lp.db, err = strconv.Atoi(configs[1])
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. cfg := new(config.Config)
  87. cfg.DataDir = lp.savePath
  88. nowLedis, err := ledis.Open(cfg)
  89. c, err = nowLedis.Select(lp.db)
  90. if err != nil {
  91. println(err)
  92. return nil
  93. }
  94. return nil
  95. }
  96. // SessionRead read ledis session by sid
  97. func (lp *Provider) SessionRead(sid string) (session.Store, error) {
  98. kvs, err := c.Get([]byte(sid))
  99. var kv map[interface{}]interface{}
  100. if len(kvs) == 0 {
  101. kv = make(map[interface{}]interface{})
  102. } else {
  103. kv, err = session.DecodeGob(kvs)
  104. if err != nil {
  105. return nil, err
  106. }
  107. }
  108. ls := &SessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
  109. return ls, nil
  110. }
  111. // SessionExist check ledis session exist by sid
  112. func (lp *Provider) SessionExist(sid string) bool {
  113. count, _ := c.Exists([]byte(sid))
  114. if count == 0 {
  115. return false
  116. }
  117. return true
  118. }
  119. // SessionRegenerate generate new sid for ledis session
  120. func (lp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
  121. count, _ := c.Exists([]byte(sid))
  122. if count == 0 {
  123. // oldsid doesn't exists, set the new sid directly
  124. // ignore error here, since if it return error
  125. // the existed value will be 0
  126. c.Set([]byte(sid), []byte(""))
  127. c.Expire([]byte(sid), lp.maxlifetime)
  128. } else {
  129. data, _ := c.Get([]byte(oldsid))
  130. c.Set([]byte(sid), data)
  131. c.Expire([]byte(sid), lp.maxlifetime)
  132. }
  133. kvs, err := c.Get([]byte(sid))
  134. var kv map[interface{}]interface{}
  135. if len(kvs) == 0 {
  136. kv = make(map[interface{}]interface{})
  137. } else {
  138. kv, err = session.DecodeGob([]byte(kvs))
  139. if err != nil {
  140. return nil, err
  141. }
  142. }
  143. ls := &SessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
  144. return ls, nil
  145. }
  146. // SessionDestroy delete ledis session by id
  147. func (lp *Provider) SessionDestroy(sid string) error {
  148. c.Del([]byte(sid))
  149. return nil
  150. }
  151. // SessionGC Impelment method, no used.
  152. func (lp *Provider) SessionGC() {
  153. return
  154. }
  155. // SessionAll return all active session
  156. func (lp *Provider) SessionAll() int {
  157. return 0
  158. }
  159. func init() {
  160. session.Register("ledis", ledispder)
  161. }