Skip to content

Commit

Permalink
feta: use go-redis, del expire
Browse files Browse the repository at this point in the history
  • Loading branch information
soxft committed Apr 22, 2024
1 parent 404a19c commit 1167ea1
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 72 deletions.
2 changes: 1 addition & 1 deletion app/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ApiHandler(c *gin.Context) {
userIdentity := c.GetString("user_identity")

// count
sitePv, siteUv, pagePv, pageUv := core.Count(host, path, userIdentity)
sitePv, siteUv, pagePv, pageUv := core.Count(c, host, path, userIdentity)

// json
c.JSON(200, gin.H{
Expand Down
2 changes: 1 addition & 1 deletion app/middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", config.Web.Cors)
c.Header("Server", "busuanzi-by-xcsoft/2.7.5")
c.Header("Server", "busuanzi-by-xcsoft/2.8.0")
if c.Request.Method == "OPTIONS" {
c.Header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS")
c.Header("Access-Control-Allow-Headers", "x-bsz-referer, Authorization")
Expand Down
3 changes: 2 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Redis:
Prefix: bsz # redis前缀
MaxIdle: 25 # 最大空闲连接数
MaxActive: 100 # 最大连接数
MinIdle: 25 # 最小空闲连接数
MaxRetries: 3 # 最大重试次数
Bsz:
Expire: 2592000 # 统计数据过期时间 单位秒, 请输入整数 (无任何访问, 超过这个时间后, 统计数据将被清空, 0为不过期)
JwtSecret: "bsz" # JWT签名密钥 // 请设置为任意长度的随机值
17 changes: 9 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ var (

var defaultConfig = Config{
Redis: RedisConfig{
Address: "redis:6379",
Password: "",
Database: 0,
TLS: false,
Prefix: "bsz_",
MaxIdle: 10,
MaxActive: 100,
Address: "redis:6379",
Password: "",
Database: 0,
TLS: false,
Prefix: "bsz_",
MaxIdle: 10,
MaxActive: 100,
MinIdle: 1,
MaxRetries: 3,
},
Web: WebConfig{
Address: "0.0.0.0:8080",
Expand All @@ -37,7 +39,6 @@ var defaultConfig = Config{
Debug: false,
},
Bsz: BszConfig{
Expire: 0,
JwtSecret: "bsz",
},
}
Expand Down
17 changes: 9 additions & 8 deletions config/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ type Config struct {
}

type RedisConfig struct {
Address string `yaml:"Address"`
Password string `yaml:"Password"`
Database int `yaml:"Database"`
TLS bool `yaml:"TLS"`
Prefix string `yaml:"Prefix"`
MaxIdle int `yaml:"MaxIdle"`
MaxActive int `yaml:"MaxActive"`
Address string `yaml:"Address"`
Password string `yaml:"Password"`
Database int `yaml:"Database"`
TLS bool `yaml:"TLS"`
Prefix string `yaml:"Prefix"`
MaxIdle int `yaml:"MaxIdle"`
MaxActive int `yaml:"MaxActive"`
MinIdle int `yaml:"MinIdle"`
MaxRetries int `yaml:"MaxRetries"`
}

type WebConfig struct {
Expand All @@ -24,6 +26,5 @@ type WebConfig struct {
}

type BszConfig struct {
Expire int `yaml:"Expire"`
JwtSecret string `yaml:"JwtSecret"`
}
52 changes: 19 additions & 33 deletions core/count.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
package core

import (
"github.com/gomodule/redigo/redis"
"context"
"fmt"
"github.com/soxft/busuanzi/config"
"github.com/soxft/busuanzi/library/tool"
"github.com/soxft/busuanzi/process/redisutil"
)

// Count
// @description return and count the number of users in the redis
func Count(host string, path string, userIdentity string) (int, int, int, int) {
_redis := redisutil.Pool.Get()
defer func(_redis redis.Conn) {
_ = _redis.Close()
}(_redis)
func Count(ctx context.Context, host string, path string, userIdentity string) (int64, int64, int64, int64) {
_redis := redisutil.RDB

// encode
var pathUnique = tool.Md5(host + "&" + path)
var siteUnique = tool.Md5(host)

redisPrefix := config.Redis.Prefix
siteUvKey := redisPrefix + ":site_uv:" + siteUnique
pageUvKey := redisPrefix + ":page_uv:" + siteUnique + ":" + pathUnique

sitePvKey := redisPrefix + ":site_pv:" + siteUnique
pagePvKey := redisPrefix + ":page_pv:" + siteUnique
// user view keys 用户数
siteUvKey := fmt.Sprintf("%s:site_uv:%s", redisPrefix, siteUnique)
pageUvKey := fmt.Sprintf("%s:page_uv:%s:%s", redisPrefix, siteUnique, pathUnique)

// count sitePv ans pagePv
sitePv, _ := redis.Int(_redis.Do("INCR", sitePvKey))
pagePv, _ := redis.Int(_redis.Do("ZINCRBY", pagePvKey, 1, pathUnique))
_, _ = _redis.Do("SADD", siteUvKey, userIdentity)
_, _ = _redis.Do("SADD", pageUvKey, userIdentity)
// page view keys 页面访问数
sitePvKey := fmt.Sprintf("%s:site_pv:%s", redisPrefix, siteUnique)
pagePvKey := fmt.Sprintf("%s:page_pv:%s", redisPrefix, siteUnique)

siteUv, _ := redis.Int(_redis.Do("SCARD", siteUvKey))
pageUv, _ := redis.Int(_redis.Do("SCARD", pageUvKey))
// count sitePv ans pagePv
sitePv, _ := _redis.Incr(ctx, sitePvKey).Result()
pagePv, _ := _redis.ZIncrBy(ctx, pagePvKey, 1, pathUnique).Result() // pagePv 使用 ZSet 存储

if config.Bsz.Expire > 0 {
go setExpire(sitePvKey, siteUvKey, pagePvKey, pageUvKey)
}
// siteUv 和 pageUv 使用 Set 存储
_, _ = _redis.SAdd(ctx, siteUvKey, userIdentity).Result()
_, _ = _redis.SAdd(ctx, pageUvKey, userIdentity).Result()

return sitePv, siteUv, pagePv, pageUv
}
siteUv, _ := _redis.SCard(ctx, siteUvKey).Result()
pageUv, _ := _redis.SCard(ctx, pageUvKey).Result()

func setExpire(key ...string) {
var _redis = redisutil.Pool.Get()
defer func(_redis redis.Conn) {
_ = _redis.Close()
}(_redis)
// multi-set expire
_, _ = _redis.Do("MULTI")
for _, k := range key {
_, _ = _redis.Do("EXPIRE", k, config.Bsz.Expire)
}
_, _ = _redis.Do("EXEC")
return sitePv, siteUv, int64(pagePv), pageUv
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ go 1.18
require (
github.com/gin-gonic/gin v1.7.7
github.com/gomodule/redigo v1.8.8
github.com/redis/go-redis/v9 v9.5.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
Expand Down Expand Up @@ -30,6 +36,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
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/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
49 changes: 29 additions & 20 deletions process/redisutil/redisutil.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
package redisutil

import (
"github.com/gomodule/redigo/redis"
"context"
"github.com/redis/go-redis/v9"
"github.com/soxft/busuanzi/config"

"log"
"time"
)

var (
Pool *redis.Pool
)
var RDB *redis.Client

func Init() {
log.Printf("[INFO] Redis trying connect to tcp://%s/%d", config.Redis.Address, config.Redis.Database)

r := config.Redis

func init() {
Pool = &redis.Pool{
MaxIdle: config.Redis.MaxIdle,
MaxActive: config.Redis.MaxActive,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", config.Redis.Address,
redis.DialPassword(config.Redis.Password),
redis.DialDatabase(config.Redis.Database),
redis.DialUseTLS(config.Redis.TLS),
)
if err != nil {
log.Fatalf("redis connect error: %s", err.Error())
}
return c, err
},
rdb := redis.NewClient(&redis.Options{
Addr: r.Address,
Password: r.Password,
DB: r.Database,
MinIdleConns: r.MinIdle,
MaxIdleConns: r.MaxIdle,
MaxRetries: r.MaxRetries,
ConnMaxLifetime: 5 * time.Minute,
MaxActiveConns: r.MaxActive,
})

RDB = rdb

// test redis
pong, err := rdb.Ping(context.Background()).Result()
if err != nil {
log.Fatalf("[ERROR] Redis ping failed: %v", err)
}
_, _ = Pool.Get().Do("PING")

log.Printf("[INFO] Redis init success, pong: %s ", pong)
}

0 comments on commit 1167ea1

Please sign in to comment.