In the previous article, we modified the middleware so that we can now implement session functionality based on the middleware. To implement this, we need to create a Redis connection, because our session data is stored in Redis
The Go-Redis expansion pack is used here
go get github.com/go-redis/redis/v8
Copy the code
Create the Redis folder in your project and create redis.go
package redis
import (
"context"
"github.com/go-redis/redis/v8"
"sync"
"time"
)
type connect struct {
client *redis.Client
}
var once = sync.Once{}
var _connect *connect
func connectRedis(a) {
cxt, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
conf := &redis.Options{
Addr: "127.0.0.1:6379",
DB: 0,
}
c := redis.NewClient(conf)
re := c.Ping(cxt)
ifre.Err() ! =nil {
panic(re.Err())
}
_connect = &connect{
client: c,
}
}
func Client(a) *redis.Client {
if _connect == nil {
once.Do(func(a) {
connectRedis()
})
}
return _connect.client
}
Copy the code
Here we use the normal singleton to get a Redis connection object
Next, modify the route and test it with the root path
package routes
//routes/routes.go
import (
"github.com/gin-gonic/gin"
"myGin/controller"
"myGin/kernel"
)
func config(router group) {
router.Registered(GET, "/", controller.Index)
}
func Load(r *gin.Engine) {
router := newRouter(r)
router.Group("".func(g group) {
config(g)
}, kernel.Middleware...) // Load global middleware
}
Copy the code
Create a session middleware to set the session key
package session
//middleware/session/session.go
import (
"fmt"
"myGin/context"
)
func Session(c *context.Context){}Copy the code
Add this middleware to the global middleware
package kernel
//middleware/session/session.go
import (
"myGin/context"
"myGin/middleware/exception"
"myGin/middleware/session"
)
// Middleware Global Middleware
var Middleware []context.HandlerFunc
func Load(a) {
Middleware = []context.HandlerFunc{
exception.Exception,
session.Session,
}
}
Copy the code
The first step in implementing a session is to give the browser a cookie as the session key and modify session.go
package session
import (
uuid "github.com/satori/go.uuid"
"myGin/context"
)
var cookieName = "my_gin"
func Session(c *context.Context) {
sessionKey := uuid.NewV4().String()
c.SetCookie(cookieName, sessionKey, 3600."/", c.Domain(), false.true)}Copy the code
Here in a uuid package, the installation please use: go get github.com/satori/go.uuid
After running the browser
The cookie has been set successfully
Next, save the session initialization data to Redis. Before saving it to Redis, we need to design the data structure of the session. First, Redis uses k-V structure, and key is a random string generated by UUID.
The structure of value is designed in this way first, which can meet the needs of the present stage
key | type | describe |
---|---|---|
cookie | uuid | Cookie value of the browser |
expire_time | int | Expiration time, consistent with the current string expiration time |
session_list | map[string]interface{} | Session values |
Shown in Redis as follows