Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-group
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-group
Commits
769a19b0
You need to sign in or sign up before continuing.
Commit
769a19b0
authored
Sep 01, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:替换group_in_user_duration_
parent
dad9dda9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
44 deletions
+29
-44
redisPrefix.go
_const/redis_key/redisPrefix.go
+2
-0
userRoomVisit.go
domain/cache/room_c/userRoomVisit.go
+24
-32
room.go
domain/model/group_m/room.go
+3
-12
No files found.
_const/redis_key/redisPrefix.go
View file @
769a19b0
...
@@ -760,6 +760,8 @@ func GetPrefixGroupMicNumType(groupUuid string) string {
...
@@ -760,6 +760,8 @@ func GetPrefixGroupMicNumType(groupUuid string) string {
return
strings
.
Replace
(
groupMicNumType
,
"{groupUuid}"
,
groupUuid
,
-
1
)
return
strings
.
Replace
(
groupMicNumType
,
"{groupUuid}"
,
groupUuid
,
-
1
)
}
}
// Deprecated: 用户进房时间
// 用 uer:g:${group_id}代替
func
GetPrefixGroupInUserDuration
(
groupUuid
string
)
string
{
func
GetPrefixGroupInUserDuration
(
groupUuid
string
)
string
{
return
strings
.
Replace
(
groupInUserDuration
,
"{groupUuid}"
,
groupUuid
,
-
1
)
return
strings
.
Replace
(
groupInUserDuration
,
"{groupUuid}"
,
groupUuid
,
-
1
)
}
}
...
...
domain/cache/room_c/userRoomVisit.go
View file @
769a19b0
...
@@ -10,36 +10,36 @@ import (
...
@@ -10,36 +10,36 @@ import (
redis2
"github.com/go-redis/redis/v8"
redis2
"github.com/go-redis/redis/v8"
"github.com/spf13/cast"
"github.com/spf13/cast"
"hilo-group/_const/redis_key"
"hilo-group/_const/redis_key"
"
strconv
"
"
hilo-group/_const/redis_key/group_k
"
"time"
"time"
)
)
// 处理访问房间的相关缓存
// 处理访问房间的相关缓存
func
ProcessRoomVisit
(
groupId
string
,
userId
uint64
)
error
{
func
ProcessRoomVisit
(
groupId
string
,
userId
uint64
)
error
{
key
:=
redis_key
.
GetPrefixGroupInUserDuration
(
groupId
)
//
key := redis_key.GetPrefixGroupInUserDuration(groupId)
now
:=
time
.
Now
()
//
now := time.Now()
ret
,
err
:=
redisCli
.
GetRedis
()
.
ZAdd
(
context
.
Background
(),
key
,
&
redis2
.
Z
{
//
ret, err := redisCli.GetRedis().ZAdd(context.Background(), key, &redis2.Z{
Score
:
float64
(
now
.
Unix
()),
//
Score: float64(now.Unix()),
Member
:
userId
,
//
Member: userId,
})
.
Result
()
//
}).Result()
if
err
!=
nil
{
//
if err != nil {
return
err
//
return err
}
//
}
mylogrus
.
MyLog
.
Infof
(
"ProcessRoomVisit, ZADD %s, return %d"
,
key
,
ret
)
//
mylogrus.MyLog.Infof("ProcessRoomVisit, ZADD %s, return %d", key, ret)
//
// 每群定时清一次数据可以了
//
//
每群定时清一次数据可以了
if
now
.
Second
()
%
redis_key
.
GroupInDurationClearPeriod
==
0
{
//
if now.Second()%redis_key.GroupInDurationClearPeriod == 0 {
rc
,
err
:=
clearRoomVisit
(
groupId
,
now
.
AddDate
(
0
,
0
,
-
redis_key
.
GroupInDurationTTL
))
//
rc, err := clearRoomVisit(groupId, now.AddDate(0, 0, -redis_key.GroupInDurationTTL))
if
err
==
nil
{
//
if err == nil {
mylogrus
.
MyLog
.
Infof
(
"ProcessRoomVisit, clearRoomVisit %s, return %d"
,
key
,
rc
)
//
mylogrus.MyLog.Infof("ProcessRoomVisit, clearRoomVisit %s, return %d", key, rc)
}
else
{
//
} else {
mylogrus
.
MyLog
.
Warnf
(
"ProcessRoomVisit, clearRoomVisit %s, failed %s"
,
key
,
err
.
Error
())
//
mylogrus.MyLog.Warnf("ProcessRoomVisit, clearRoomVisit %s, failed %s", key, err.Error())
}
//
}
}
//
}
// 马上更新roomVisitCount
// 马上更新roomVisitCount
if
_
,
err
:=
GetSetRoomVisitCount
(
groupId
);
err
!=
nil
{
if
_
,
err
:=
GetSetRoomVisitCount
(
groupId
);
err
!=
nil
{
mylogrus
.
MyLog
.
Warnf
(
"ProcessRoomVisit, failed for key %s, err: %s"
,
key
,
err
.
Error
())
mylogrus
.
MyLog
.
Errorf
(
"ProcessRoomVisit, failed for groupId %s, err: %s"
,
groupId
,
err
.
Error
())
}
}
return
nil
return
nil
...
@@ -108,7 +108,8 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
...
@@ -108,7 +108,8 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
}
}
func
GetSetRoomVisitCount
(
groupId
string
)
(
int64
,
error
)
{
func
GetSetRoomVisitCount
(
groupId
string
)
(
int64
,
error
)
{
key
:=
redis_key
.
GetPrefixGroupInUserDuration
(
groupId
)
//key := redis_key.GetPrefixGroupInUserDuration(groupId)
key
:=
group_k
.
GetUserEnterRoomGroupKey
(
groupId
)
vc
,
err
:=
redisCli
.
GetRedis
()
.
ZCard
(
context
.
Background
(),
key
)
.
Result
()
vc
,
err
:=
redisCli
.
GetRedis
()
.
ZCard
(
context
.
Background
(),
key
)
.
Result
()
// 查到合法值后,更新二级缓存
// 查到合法值后,更新二级缓存
if
err
==
nil
&&
vc
>=
0
{
if
err
==
nil
&&
vc
>=
0
{
...
@@ -133,12 +134,3 @@ func saveRoomVisitCount(groupId string, count int64) error {
...
@@ -133,12 +134,3 @@ func saveRoomVisitCount(groupId string, count int64) error {
return
nil
return
nil
//return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result()
//return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result()
}
}
func
clearRoomVisit
(
groupId
string
,
t
time
.
Time
)
(
int64
,
error
)
{
value
:=
strconv
.
FormatInt
(
t
.
Unix
(),
10
)
ret
,
err
:=
redisCli
.
GetRedis
()
.
ZRemRangeByScore
(
context
.
Background
(),
redis_key
.
GetPrefixGroupInUserDuration
(
groupId
),
"0"
,
value
)
.
Result
()
if
err
!=
nil
{
return
0
,
err
}
return
ret
,
nil
}
domain/model/group_m/room.go
View file @
769a19b0
...
@@ -14,6 +14,7 @@ import (
...
@@ -14,6 +14,7 @@ import (
"gorm.io/gorm/clause"
"gorm.io/gorm/clause"
"hilo-group/_const/enum/group_e"
"hilo-group/_const/enum/group_e"
"hilo-group/_const/redis_key"
"hilo-group/_const/redis_key"
"hilo-group/_const/redis_key/group_k"
"hilo-group/domain/cache/room_c"
"hilo-group/domain/cache/room_c"
"hilo-group/domain/model/user_m"
"hilo-group/domain/model/user_m"
"hilo-group/myerr"
"hilo-group/myerr"
...
@@ -389,18 +390,8 @@ func GetAllGroupsSorted(model *domain.Model) ([]string, error) {
...
@@ -389,18 +390,8 @@ func GetAllGroupsSorted(model *domain.Model) ([]string, error) {
// 取房间最近N天的访问人数
// 取房间最近N天的访问人数
func
GetRoomVisitCount
(
groupId
string
)
(
int64
,
error
)
{
func
GetRoomVisitCount
(
groupId
string
)
(
int64
,
error
)
{
// 每群定时请一次数据可以了
key
:=
group_k
.
GetUserEnterRoomGroupKey
(
groupId
)
now
:=
time
.
Now
()
visitCount
,
err
:=
redisCli
.
GetRedis
()
.
ZCard
(
context
.
Background
(),
key
)
.
Result
()
if
now
.
Second
()
%
redis_key
.
GroupInDurationClearPeriod
==
0
{
// 先移除(N天之前的),后统计
if
_
,
err
:=
redisCli
.
GetRedis
()
.
ZRemRangeByScore
(
context
.
Background
(),
redis_key
.
GetPrefixGroupInUserDuration
(
groupId
),
"0"
,
strconv
.
FormatInt
(
time
.
Now
()
.
AddDate
(
0
,
0
,
-
redis_key
.
GroupInDurationTTL
)
.
Unix
(),
10
))
.
Result
();
err
!=
nil
{
return
0
,
err
}
}
visitCount
,
err
:=
redisCli
.
GetRedis
()
.
ZCard
(
context
.
Background
(),
redis_key
.
GetPrefixGroupInUserDuration
(
groupId
))
.
Result
()
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
err
return
0
,
err
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment