Skip to content

Commit

Permalink
refactor: support get expire time of session
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Feb 14, 2023
1 parent 938035d commit 0a8fefc
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 66 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ jobs:
strategy:
matrix:
go:
- '1.20'
- '1.19'
- '1.18'
- '1.17'
- '1.16'
- '1.15'
- '1.14'
- '1.13'
steps:

- name: Go ${{ matrix.go }} test
Expand Down
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
module github.com/vicanso/elton-session

go 1.12
go 1.18

require (
github.com/hashicorp/golang-lru v0.5.4
github.com/hashicorp/golang-lru/v2 v2.0.1
github.com/spf13/cast v1.5.0
github.com/stretchr/testify v1.8.0
github.com/vicanso/elton v1.9.5
github.com/vicanso/hes v0.6.0
github.com/stretchr/testify v1.8.1
github.com/vicanso/elton v1.10.0
github.com/vicanso/hes v0.6.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vicanso/intranet-ip v0.1.0 // indirect
github.com/vicanso/keygrip v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
35 changes: 10 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/vicanso/elton v1.9.5 h1:vWCota4myz2BXT40RqbxMZlSTTvxiSPR06rJ/VWXEqQ=
github.com/vicanso/elton v1.9.5/go.mod h1:KIEn5zohLKfKo3d/Rlww5j2/jbOXDdNlO/UTwxrc+wk=
github.com/vicanso/hes v0.6.0 h1:rmLhJw1PkZ+BTlJXt91i12mr7vpIC65S5KRCD4uF9HU=
github.com/vicanso/hes v0.6.0/go.mod h1:bO1pAVgzuwBJAv76LXx8YN6jXLWcCYVyNx4qzPw9h6Y=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/vicanso/elton v1.10.0 h1:Qd6Dr5sarzkij+Vdgvtsd08jaNAKuZpBDpyPtKzESaY=
github.com/vicanso/elton v1.10.0/go.mod h1:GnFxH3+Vtz0HQbhGhCmssbdi67+yLjFOUbZDdB8mRcQ=
github.com/vicanso/hes v0.6.1 h1:BRGUDhV2sJYMieJf4dgxFXjvuhDgUWBsINELcti0Z8M=
github.com/vicanso/hes v0.6.1/go.mod h1:awwBbvcDTk8APxRmiV7Hxrir89/iCxgB6RMeLc5toh0=
github.com/vicanso/intranet-ip v0.1.0 h1:UeoxilO2VDIkeZZxmu6aT+f5o79mfGdsSdwoEv75nYo=
github.com/vicanso/intranet-ip v0.1.0/go.mod h1:N1yrHdDYWNsOs5V374DuAJHba+d2dxUDcjVALgIlfOg=
github.com/vicanso/keygrip v1.2.1 h1:876fXDwGJqxdi4JxZ1lNGBxYswyLZotrs7AA2QWcLeY=
github.com/vicanso/keygrip v1.2.1/go.mod h1:tfB5az1yqold78zotkzNugk3sV+QW5m71CFz3zg9eeo=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 6 additions & 18 deletions memory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"sync/atomic"
"time"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/vicanso/hes"
)

Expand All @@ -53,7 +53,7 @@ const (
type (
// MemoryStore memory store for session
MemoryStore struct {
client *lru.Cache
client *lru.Cache[string, *MemoryStoreInfo]
flushStatus int32
}
// MemoryStoreInfo memory store info
Expand All @@ -78,14 +78,10 @@ func (ms *MemoryStore) Get(_ context.Context, key string) (data []byte, err erro
err = ErrNotInit
return
}
v, found := client.Get(key)
info, found := client.Get(key)
if !found {
return
}
info, ok := v.(*MemoryStoreInfo)
if !ok {
return
}
if info.ExpiredAt < time.Now().Unix() {
return
}
Expand Down Expand Up @@ -136,19 +132,11 @@ func (ms *MemoryStore) intervalFlush(saveAs string, interval time.Duration) {
}
keys := client.Keys()
m := make(map[string]*MemoryStoreInfo)
for _, k := range keys {
key, _ := k.(string)
if key == "" {
continue
}
v, found := client.Get(key)
for _, key := range keys {
info, found := client.Get(key)
if !found {
continue
}
info, ok := v.(*MemoryStoreInfo)
if !ok {
continue
}
if info.ExpiredAt < time.Now().Unix() {
continue
}
Expand All @@ -167,7 +155,7 @@ func (ms *MemoryStore) StopFlush() {

// NewMemoryStore create new memory store instance
func NewMemoryStore(size int) (store *MemoryStore, err error) {
client, err := lru.New(size)
client, err := lru.New[string, *MemoryStoreInfo](size)
if err != nil {
return
}
Expand Down
1 change: 0 additions & 1 deletion memory_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func TestMemoryStore(t *testing.T) {

t.Run("get data", func(t *testing.T) {
assert := assert.New(t)
ms.client.Add(key, data)
buf, err := ms.Get(ctx, key)
assert.Nil(err)
assert.Empty(buf, "get invalid data should be empty")
Expand Down
17 changes: 13 additions & 4 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ import (
)

const (
// CreatedAt the created time for session
// CreatedAt the created time of session
CreatedAt = "_createdAt"
// UpdatedAt the updated time for session
// UpdatedAt the updated time of session
UpdatedAt = "_updatedAt"
// ExpiredAt the expired time of session
ExpiredAt = "_expiredAt"
// ErrCategory session error category
ErrCategory = "elton-session"
// Key session key
Expand Down Expand Up @@ -306,16 +308,21 @@ func (s *Session) GetStringSlice(key string) []string {
return cast.ToStringSlice(s.Get(key))
}

// GetCreatedAt get the created at of session
// GetCreatedAt get the created time of session
func (s *Session) GetCreatedAt() string {
return cast.ToString(s.Get(CreatedAt))
}

// GetUpdatedAt get the updated at of session
// GetUpdatedAt get the updated time of session
func (s *Session) GetUpdatedAt() string {
return cast.ToString(s.Get(UpdatedAt))
}

// GetExpiredAt get the expired time of session
func (s *Session) GetExpiredAt() string {
return cast.ToString(s.Get(ExpiredAt))
}

// GetData get the session's data
func (s *Session) GetData() M {
return s.data
Expand All @@ -333,6 +340,8 @@ func (s *Session) Commit(ctx context.Context, ttl time.Duration) error {
if s.ID == "" {
return ErrIDNil
}
// 写入store时更新expired at
s.data[ExpiredAt] = time.Now().Add(ttl).Format(time.RFC3339)

buf, err := json.Marshal(s.data)
if err != nil {
Expand Down
20 changes: 12 additions & 8 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ package session

import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"runtime"
"strings"
"testing"
"time"
Expand All @@ -39,16 +41,14 @@ import (
"github.com/vicanso/hes"
)

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

// generateID gen id
func generateID() string {
b := make([]rune, 24)
for i := range b {
rand.Seed(time.Now().UnixNano())
b[i] = letterRunes[rand.Intn(len(letterRunes))]
b := make([]byte, 24)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return string(b)
return base64.StdEncoding.EncodeToString(b)
}

func TestWrapError(t *testing.T) {
Expand Down Expand Up @@ -381,6 +381,10 @@ func TestSessionMiddleware(t *testing.T) {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
rc := m.Run()
// go 1.20获取到Coverage为0
if runtime.Version() == "go1.20" {
return
}

// rc 0 means we've passed,
// and CoverMode will be non empty if run with -cover
Expand Down

0 comments on commit 0a8fefc

Please sign in to comment.