Skip to content

Commit

Permalink
💼 introduce sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenny committed Nov 6, 2020
1 parent 0483406 commit 8dd6631
Show file tree
Hide file tree
Showing 7 changed files with 823 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gofiber/fiber/v2
go 1.14

require (
github.com/klauspost/compress v1.11.0 // indirect
github.com/valyala/fasthttp v1.17.0
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDa
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.0 h1:wJbzvpYMVGG9iTI9VxpnNZfd4DzMPoCWze3GgSqz8yg=
github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.17.0 h1:P8/koH4aSnJ4xbd0cUUFEGQs3jQqIxoDDyRQrUiAkqg=
Expand Down
103 changes: 103 additions & 0 deletions middleware/sessions/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package sessions

import (
"sync"
)

// go:generate msgp
// msgp -file="db.go" -o="db_msgp.go" -tests=false -unexported
// don't forget to replace the msgp import path to:
// "github.com/gofiber/fiber/v2/internal/msgp"
type db struct {
d []kv
}

// go:generate msgp
type kv struct {
k string
v interface{}
}

var dbPool = sync.Pool{
New: func() interface{} {
return new(db)
},
}

func acquireDB() *db {
return dbPool.Get().(*db)
}

func releaseDB(d *db) {
d.Reset()
dbPool.Put(d)
}

func (d *db) Reset() {
d.d = d.d[:0]
}

func (d *db) Get(key string) interface{} {
idx := d.indexOf(key)
if idx > -1 {
return d.d[idx].v
}
return nil
}

func (d *db) Set(key string, value interface{}) {
idx := d.indexOf(key)
if idx > -1 {
kv := &d.d[idx]
kv.v = value
} else {
d.append(key, value)
}
}

func (d *db) Delete(key string) {
idx := d.indexOf(key)
if idx > -1 {
n := len(d.d) - 1
d.swap(idx, n)
d.d = d.d[:n]
}
}

func (d *db) Len() int {
return len(d.d)
}

func (d *db) swap(i, j int) {
iKey, iValue := d.d[i].k, d.d[i].v
jKey, jValue := d.d[j].k, d.d[j].v

d.d[i].k, d.d[i].v = jKey, jValue
d.d[j].k, d.d[j].v = iKey, iValue
}

func (d *db) allocPage() *kv {
n := len(d.d)
if cap(d.d) > n {
d.d = d.d[:n+1]
} else {
d.d = append(d.d, kv{})
}
return &d.d[n]
}

func (d *db) append(key string, value interface{}) {
kv := d.allocPage()
kv.k = key
kv.v = value
}

func (d *db) indexOf(key string) int {
n := len(d.d)
for i := 0; i < n; i++ {
if d.d[i].k == key {
return i
}
}
return -1
}
Loading

0 comments on commit 8dd6631

Please sign in to comment.