|
1 | 1 | #6.3 session存储 |
| 2 | +上一节我们介绍了Session管理器的实现原理,定义了如何实现session存储的接口,这小节我们讲通过一个例子来实现内存的session存储,其他类似数据库或者文件的存储,用户可以根据相应的接口定义来实现,内存的实现请看下面的例子代码 |
| 3 | + |
| 4 | + package memory |
| 5 | + |
| 6 | + import ( |
| 7 | + "session/session" |
| 8 | + "time" |
| 9 | + ) |
| 10 | + |
| 11 | + var d = &Provider{} |
| 12 | + |
| 13 | + type SessionStore struct { |
| 14 | + sid string //session id唯一标示 |
| 15 | + timeAccessed time.Time //最后访问时间 |
| 16 | + value map[interface{}]interface{} //session里面存储的值 |
| 17 | + } |
| 18 | + |
| 19 | + func (this *SessionStore) Set(key, value interface{}) bool { |
| 20 | + this.value[key] = value |
| 21 | + d.SessionUpdate(this.sid) |
| 22 | + return true |
| 23 | + } |
| 24 | + |
| 25 | + func (this *SessionStore) Get(key interface{}) interface{} { |
| 26 | + d.SessionUpdate(this.sid) |
| 27 | + if v, ok := this.value[key]; ok { |
| 28 | + return v |
| 29 | + } else { |
| 30 | + return "" |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + func (this *SessionStore) Del(key interface{}) bool { |
| 35 | + delete(this.value, key) |
| 36 | + d.SessionUpdate(this.sid) |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + type Provider struct { |
| 41 | + lock sync.Mutex //用来锁 |
| 42 | + sessions map[string]*SessionStore //用来存储在内存 |
| 43 | + list *list.List //用来做gc |
| 44 | + } |
| 45 | + |
| 46 | + func (this *Provider) SessionInit(sid string) (session.Session, error) { |
| 47 | + this.lock.Lock() |
| 48 | + defer this.lock.Unlock() |
| 49 | + v := make(map[interface{}]interface{}, 0) |
| 50 | + newsess := &SessionStore{"sid": sid, "timeAccessed": time.Now(), "value": v} |
| 51 | + this.sessions[sid] = newsess |
| 52 | + this.list.Push(newsess) |
| 53 | + return newsess |
| 54 | + } |
| 55 | + |
| 56 | + func (this *Provider) SessionRead(sid string) (session.Session, error) { |
| 57 | + if s, ok := this.sessions[sid]; ok { |
| 58 | + return s, nil |
| 59 | + } else { |
| 60 | + sess, err := this.SessionInit(sid) |
| 61 | + return sess, err |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + func (this *Provider) SessionDestroy(sid string) bool { |
| 66 | + if s, ok := this.sessions[sid]; ok { |
| 67 | + delete(this.table, sid) |
| 68 | + this.list.Remove(s) |
| 69 | + return true |
| 70 | + } else { |
| 71 | + return false |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + func (this *Provider) SessionGC(maxlifetime int64) { |
| 76 | + this.lock.Lock() |
| 77 | + defer this.lock.Unlock() |
| 78 | + |
| 79 | + for { |
| 80 | + element := this.list.Back() |
| 81 | + if element == nil { |
| 82 | + break |
| 83 | + } |
| 84 | + if (element.Value.(*SessionStore).timeAccessed.Unix() + maxlifetime) < time.Now().Unix() { |
| 85 | + this.list.Remove(element) |
| 86 | + delete(this.table, element.Value.(*SessionStore).sid) |
| 87 | + } else { |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + func (this *Provider) SessionUpdate(sid string) bool { |
| 94 | + this.lock.Lock() |
| 95 | + defer this.lock.Unlock() |
| 96 | + if element, ok := this.sessions[sid]; ok { |
| 97 | + element.Value.(*SessionStore).value = s.value |
| 98 | + this.moveToFront(element) |
| 99 | + return true |
| 100 | + } else { |
| 101 | + return false |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + func (this *Provider) moveToFront(element *list.Element) { |
| 106 | + this.lock.Lock() |
| 107 | + defer this.lock.Unlock() |
| 108 | + element.Value.(*SessionStore).timeAccessed = time.Now() |
| 109 | + this.list.MoveToFront(element) |
| 110 | + } |
| 111 | + |
| 112 | + func init() { |
| 113 | + session.Register("memory", d) |
| 114 | + } |
| 115 | + |
| 116 | +上面这个代码实现了一个内存存储实现的session机制 |
2 | 117 | ## links |
3 | 118 | * [目录](<preface.md>) |
4 | 119 | * 上一节: [Go如何使用session](<6.2.md>) |
|
0 commit comments