Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(os/gsession): add session reset function. #3726

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ go.work.sum
node_modules
.docusaurus
output
config/*
3 changes: 3 additions & 0 deletions cmd/gf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.7.2
github.com/gogf/gf/contrib/drivers/dm/v2 v2.7.1
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.7.2
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.7.2
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.7.2
Expand All @@ -18,6 +19,7 @@ require (

require (
aead.dev/minisign v0.2.0 // indirect
gitee.com/chunanyong/dm v1.8.12 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.0.15 // indirect
github.com/clbanning/mxj/v2 v2.7.0 // indirect
Expand All @@ -31,6 +33,7 @@ require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions cmd/gf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.7.2 h1:ieOB/AaYmRyLnFc/t5HMMpWXTEJ+A9O+d9AcWnIizQs=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.7.2/go.mod h1:TfTV5FErx1SGjQM100MkuO390Iy+BO1sPZ0OMm2Q5N4=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.7.2 h1:+rnLsVwFCNG294D3dJmhbfPz7dG+qA7JOnlL6cYk55Y=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.7.2/go.mod h1:nxAQjtQtcbGrF705gkTxdc032c/MNY28sCyWYQrD1Fw=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.7.2 h1:GpE2JuHVoNJD4lNP1omC1+TKXNCSvXr5oil1bNULYd0=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.7.2/go.mod h1:0h3UmNAmA8hnjvTyozZelSWWxiAjGDQttzZqMhkCkJo=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.7.2 h1:EAcV1aIbFyZwsFkXmqVuOoz5JwuTybDNRFpTDNKQtLo=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.7.2/go.mod h1:XUo35CiDBTJ9T3K5VkiEnQzphEPq+lRHft6s8XWun74=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.7.2 h1:tI4V57XyvKIpCX8WUtyGlh6aN+rFcg2reKnDqbhxuLA=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.7.2/go.mod h1:IB+g+oXMF6SZQQY4Ec8Kh3c3LddfEkgIsmYBAc3CsKM=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.7.2 h1:P/1w0jayiYZUd3X7EFuZ7r5FinBupNhWf8BzJkB2V9Y=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.7.2/go.mod h1:TSNhyUnkedVxHOHbFlr4hVMxpNXklF80kz1gjLlCIuE=
github.com/gogf/gf/v2 v2.7.2 h1:uZDfyblasI12lZUtFd1mfxsIr8b14cd/F88DJUTCSDM=
github.com/gogf/gf/v2 v2.7.2/go.mod h1:EBXneAg/wes86rfeh68XC0a2JBNQylmT7Sp6/8Axk88=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f h1:7xfXR/BhG3JDqO1s45n65Oyx9t4E/UqDOXep6jXdLCM=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f/go.mod h1:HnYoio6S7VaFJdryKcD/r9HgX+4QzYfr00XiXUo/xz0=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down
16 changes: 15 additions & 1 deletion i18n/gi18n/gi18n_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package gi18n
import (
"context"
"fmt"
"github.com/gogf/gf/v2/text/gstr"
"strings"
"sync"

Expand Down Expand Up @@ -166,8 +167,21 @@ func (m *Manager) Tf(ctx context.Context, format string, values ...interface{})

// TranslateFormat translates, formats and returns the `format` with configured language
// and given `values`.
// When values[0] is of type map[string]string, parameter replacement is order-independent.
// the values' format :{key}
func (m *Manager) TranslateFormat(ctx context.Context, format string, values ...interface{}) string {
return fmt.Sprintf(m.Translate(ctx, format), values...)
result := m.Translate(ctx, format)
if len(values) > 0 {
val := values[0]
if valMapStrStr, ok := val.(map[string]string); ok {
for k, v := range valMapStrStr {
rpStr := ":" + k
result = gstr.Replace(result, rpStr, v)
}
return result
}
}
return fmt.Sprintf(result, values...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lxy1151 请先拆分pr,一个pr只做一个事情。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,我先处理下

}

// Translate translates `content` with configured language.
Expand Down
13 changes: 13 additions & 0 deletions i18n/gi18n/gi18n_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gi18n_test

import (
"fmt"
"time"

"github.com/gogf/gf/v2/encoding/gbase64"
Expand Down Expand Up @@ -121,6 +122,18 @@ func Test_DefaultManager(t *testing.T) {
})
}

func Test_MapStrStrRelace(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
i18n := gi18n.New(gi18n.Options{
Path: gtest.DataPath("i18n-file"),
})
i18n.SetLanguage("zh-CN")
str := i18n.Tf(context.Background(), "replace_map_test", map[string]string{"score": "3000", "name": "blue"})
fmt.Println(str)
t.Assert(str, "hello 3000 blue is ok")
})
}

func Test_Instance(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := gi18n.Instance()
Expand Down
3 changes: 2 additions & 1 deletion i18n/gi18n/testdata/i18n-file/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"你好": "hello",
"世界": "world",
"hello": "你好",
"world": "世界"
"world": "世界",
"replace_map_test": "hello :score :name is ok"
}
31 changes: 16 additions & 15 deletions net/gclient/gclient_z_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func init() {
if err != nil {
panic(err)
}
time.Sleep(time.Millisecond * 500)
time.Sleep(time.Millisecond * 2000)
}

func ExampleNew() {
Expand Down Expand Up @@ -195,7 +195,7 @@ func ExampleNew_MultiConn_Recommend() {
if r, err := client.Get(ctx, "http://127.0.0.1:8999/var/json"); err != nil {
panic(err)
} else {
fmt.Println(r.ReadAllString())
fmt.Print(r.ReadAllString())
r.Close()
}
}
Expand Down Expand Up @@ -272,9 +272,9 @@ func ExampleClient_ContentJson() {
}
)
// Post using JSON string.
fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonStr))
fmt.Print(g.Client().ContentJson().PostContent(ctx, url, jsonStr))
// Post using JSON map.
fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonMap))
fmt.Print(g.Client().ContentJson().PostContent(ctx, url, jsonMap))

// Output:
// Content-Type: application/json, id: 10000
Expand All @@ -289,7 +289,7 @@ func ExampleClient_Post() {
panic(err)
}
defer r1.Close()
fmt.Println(r1.ReadAllString())
fmt.Print(r1.ReadAllString())

// Send with map parameter.
r2, err := g.Client().Post(ctx, url, g.Map{
Expand All @@ -300,7 +300,7 @@ func ExampleClient_Post() {
panic(err)
}
defer r2.Close()
fmt.Println(r2.ReadAllString())
fmt.Print(r2.ReadAllString())

// Output:
// POST: form: 10000, john
Expand Down Expand Up @@ -425,15 +425,15 @@ func ExampleClient_Get() {
panic(err)
}
defer r1.Close()
fmt.Println(r1.ReadAllString())
fmt.Print(r1.ReadAllString())

// Send with string parameter in request body.
r2, err := g.Client().Get(ctx, url, "id=10000&name=john")
if err != nil {
panic(err)
}
defer r2.Close()
fmt.Println(r2.ReadAllString())
fmt.Print(r2.ReadAllString())

// Send with map parameter.
r3, err := g.Client().Get(ctx, url, g.Map{
Expand All @@ -444,7 +444,7 @@ func ExampleClient_Get() {
panic(err)
}
defer r3.Close()
fmt.Println(r3.ReadAllString())
fmt.Print(r3.ReadAllString())

// Output:
// GET: query: 10000, john
Expand Down Expand Up @@ -596,16 +596,16 @@ func ExampleClient_Proxy() {

client2 := g.Client()
_, err = client2.Proxy("socks5://127.0.0.1:1080").Get(ctx, "http://127.0.0.1:8999")
fmt.Println(err != nil)
fmt.Print(err != nil)

client3 := g.Client()
_, err = client3.Proxy("").Get(ctx, "http://127.0.0.1:8999")
fmt.Println(err != nil)
fmt.Print(err != nil)

client4 := g.Client()
url := "http://127.0.0.1:1081" + string([]byte{0x7f})
_, err = client4.Proxy(url).Get(ctx, "http://127.0.0.1:8999")
fmt.Println(err != nil)
fmt.Print(err != nil)

// Output:
// true
Expand Down Expand Up @@ -639,7 +639,7 @@ func ExampleClient_Prefix() {
"http://127.0.0.1:%d/api/v1/", s.GetListenedPort(),
))

fmt.Println(string(client.GetBytes(ctx, "prefix")))
fmt.Print(string(client.GetBytes(ctx, "prefix")))
fmt.Println(string(client.GetBytes(ctx, "hello")))

// Output:
Expand Down Expand Up @@ -742,7 +742,7 @@ func ExampleClient_SetRedirectLimit() {
"name": "john",
})
if err == nil {
fmt.Println(resp.ReadAllString())
fmt.Print(resp.ReadAllString())
resp.Close()
}

Expand All @@ -752,12 +752,13 @@ func ExampleClient_SetRedirectLimit() {
"name": "john",
})
if err == nil {
fmt.Println(resp.ReadAllString())
fmt.Print(resp.ReadAllString())
resp.Close()
}

// Output:
// Found
//
// hello world
}

Expand Down
29 changes: 29 additions & 0 deletions os/gsession/gsession_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gsession

import (
"context"
"fmt"
"time"

"github.com/gogf/gf/v2/container/gmap"
Expand Down Expand Up @@ -82,6 +83,7 @@ func (s *Session) init() error {
//
// NOTE that this function must be called ever after a session request done.
func (s *Session) Close() error {
fmt.Println("")
if s.manager.storage == nil {
return nil
}
Expand Down Expand Up @@ -155,6 +157,33 @@ func (s *Session) Remove(keys ...string) (err error) {
return nil
}

func (s *Session) RegenSession(delOld ...bool) (newSid string, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请描述增加此方法的必要性,例如需要解决什么样的业务场景痛点?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有这个方法,sessionId始终不能过期。
每次登录时要求上次的sessionId过期,无法实现,特此加了该方法

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个怎么说,还需修改什么吗?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接使用RemoveAll不就行了呢。

oldDel := false
if len(delOld) > 0 {
oldDel = delOld[0]
}
if s.id == "" {
if err = s.init(); err != nil {
return newSid, err
}
} else {
if oldDel {
if err = s.RemoveAll(); err != nil {
return newSid, err
}
}
s.id = ""
s.start = false
s.data.Clear()
if err = s.init(); err != nil {
return newSid, err
}
}
newSid = s.id
s.dirty = false
return newSid, err
}

// RemoveAll deletes all key-value pairs from this session.
func (s *Session) RemoveAll() (err error) {
if s.id == "" {
Expand Down
59 changes: 58 additions & 1 deletion os/gsession/gsession_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
package gsession

import (
"github.com/gogf/gf/v2/test/gtest"
"testing"

"github.com/gogf/gf/v2/test/gtest"
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
)

func Test_NewSessionId(t *testing.T) {
Expand All @@ -20,3 +21,59 @@ func Test_NewSessionId(t *testing.T) {
t.Assert(len(id1), 32)
})
}

func TestSession_RegenSession(t *testing.T) {
gtest.C(t, func(t *gtest.T) {

/*config := &gredis.Config{
Address: "192.168.5.1:6379",
Db: 10,
Pass: "123456",
IdleTimeout: 600,
MinIdle: 2,
WaitTimeout: 30,
}
rds, erRds := gredis.New(config)
if erRds != nil {
fmt.Println("err:", erRds)
return
}
storage := NewStorageRedisHashTable(rds, "tsid:")
sManger := New(time.Second*600, storage)
sid := "" //"19t8ieo1300d3eh4xqsnlbw100xj9q7q"
session := sManger.New(gctx.New(), sid)
sid, _ = session.Id()
fmt.Println(sid)
session.Set("score", 6666666)
session.Set("name", "erretre")
session.Close()
score := session.MustGet("score")
fmt.Println("score:", score.String())
name := session.MustGet("name")
fmt.Println("name:", name.String())
data, _ := session.Data()
fmt.Println(fmt.Sprintf("%+v", data))

newSid, errNewSid := session.RegenSession(true)
if errNewSid != nil {
fmt.Println("regen session err:", errNewSid)
return
}
fmt.Println("new sid:", newSid)
session.Set("score", "99999999999")
session.Set("tttt", 999999)
session.Close()

newScore, _ := session.Get("score")
fmt.Println("newScore:", newScore, " newSid:", session.id)

tttt, _ := session.Get("tttt")
fmt.Println("tttt:", tttt, " newSid:", session.id)

oldSid := sid
sessionOld := sManger.New(gctx.New(), oldSid)
oldData, _ := sessionOld.Data()
fmt.Println("oldData:", oldData, " oldSid:", oldSid)*/
//time.Sleep(time.Second * 2)
})
}
Loading