Skip to content

Commit

Permalink
refactor: adjust fetch function
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Oct 22, 2021
1 parent 8782ddf commit a9fa4b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
5 changes: 2 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package main
import (
"bytes"
"strconv"
"strings"
"time"

"github.com/rs/xid"
"github.com/vicanso/elton"
session "github.com/vicanso/elton-session"
)
Expand All @@ -28,7 +26,8 @@ func main() {
Signed: true,
Expired: 10 * time.Hour,
GenID: func() string {
return strings.ToUpper(xid.New().String())
// 使用时需要使用uuid等生成唯一id
return strconv.Itoa(int(time.Now().UnixNano()))
},
Name: "jt",
Path: "/",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/hashicorp/golang-lru v0.5.4
github.com/spf13/cast v1.4.1
github.com/stretchr/testify v1.7.0
github.com/vicanso/elton v1.5.0
github.com/vicanso/elton v1.7.0
github.com/vicanso/hes v0.3.9
)
13 changes: 5 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.8.1/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vicanso/elton v1.4.3 h1:D7fZZteCZ+gp8kltfLgE85uEzzxurEAHjc0PQG+rXeY=
github.com/vicanso/elton v1.4.3/go.mod h1:BFhCB2ke3uPLo0Ids8wgYmNeq5nbivqvHtfwIX8PY/c=
github.com/vicanso/elton v1.5.0 h1:Id+CVDHBpvknq8AOI1dowwVVonU5JqH/jBxGCoR/PxQ=
github.com/vicanso/elton v1.5.0/go.mod h1:BFhCB2ke3uPLo0Ids8wgYmNeq5nbivqvHtfwIX8PY/c=
github.com/tidwall/gjson v1.9.4/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.7.0 h1:ilSWuF336Crczqx0Wm/kwLKtT1riomPJPsJTupVbO3A=
github.com/vicanso/elton v1.7.0/go.mod h1:9P/FRAZlWVZ1bgdh7xMSotcil3L9K8tTM8WaW5alu0w=
github.com/vicanso/hes v0.3.9 h1:IO21yElX6Xp3w+Lc1O2QIySrJj2jEhnl5dWbqbDYunc=
github.com/vicanso/hes v0.3.9/go.mod h1:B0l1NIQM/nYw7owAd+hyHuNnAD8Nsx0T6duhVxmXUBY=
github.com/vicanso/intranet-ip v0.0.1 h1:cYS+mExFsKqewWSuHtFwAqw/CO66GsheB/P1BPmSTx0=
Expand Down
28 changes: 10 additions & 18 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,9 @@ func (s *Session) Set(ctx context.Context, key string, value interface{}) error
if key == "" {
return nil
}
if !s.fetched {
err := s.fetch(ctx)
if err != nil {
return err
}
err := s.fetch(ctx)
if err != nil {
return err
}
if value == nil {
delete(s.data, key)
Expand All @@ -267,11 +265,9 @@ func (s *Session) SetMap(ctx context.Context, value map[string]interface{}) erro
if value == nil {
return nil
}
if !s.fetched {
err := s.fetch(ctx)
if err != nil {
return err
}
err := s.fetch(ctx)
if err != nil {
return err
}
for k, v := range value {
if v == nil {
Expand All @@ -297,21 +293,17 @@ func (s *Session) EnableReadonly() {

// Refresh refresh session (update updatedAt)
func (s *Session) Refresh(ctx context.Context) error {
if !s.fetched {
err := s.fetch(ctx)
if err != nil {
return err
}
err := s.fetch(ctx)
if err != nil {
return err
}
s.updatedAt()
return nil
}

// Get get data from session's data
func (s *Session) Get(key string) interface{} {
if !s.fetched {
return nil
}
_ = s.fetch(context.Background())
return s.data[key]
}

Expand Down

0 comments on commit a9fa4b7

Please sign in to comment.