-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
81 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |