修复缓存可能遇到的问题

This commit is contained in:
ayflying
2025-03-10 19:00:29 +08:00
parent 92a127e128
commit f8ec441241
9 changed files with 84 additions and 54 deletions

4
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/ayflying/utility_go module github.com/ayflying/utility_go
go 1.23.0 go 1.24.0
require ( require (
github.com/apolloconfig/agollo/v4 v4.4.0 github.com/apolloconfig/agollo/v4 v4.4.0
@@ -79,6 +79,8 @@ require (
google.golang.org/appengine v1.6.7 // indirect google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect google.golang.org/grpc v1.38.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect

4
go.sum
View File

@@ -696,10 +696,14 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@@ -2,7 +2,7 @@ package act
import ( import (
"fmt" "fmt"
"github.com/ayflying/utility_go/package/aycache" "github.com/ayflying/utility_go/pgk"
"github.com/ayflying/utility_go/service" "github.com/ayflying/utility_go/service"
"github.com/gogf/gf/v2/container/gvar" "github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
@@ -11,7 +11,7 @@ import (
) )
var ( var (
Cache = aycache.New() Cache = pgk.Cache("redis")
ActIdListIsShow map[int]func(uid int64) bool ActIdListIsShow map[int]func(uid int64) bool
RedDotList map[string]func(uid int64) int32 RedDotList map[string]func(uid int64) int32
) )

View File

@@ -2,10 +2,10 @@ package gameAct
import ( import (
"fmt" "fmt"
"github.com/ayflying/utility_go/internal/game/act"
"github.com/ayflying/utility_go/internal/model/do" "github.com/ayflying/utility_go/internal/model/do"
"github.com/ayflying/utility_go/internal/model/entity" "github.com/ayflying/utility_go/internal/model/entity"
"github.com/ayflying/utility_go/package/aycache" "github.com/ayflying/utility_go/package/aycache"
"github.com/ayflying/utility_go/pgk"
service2 "github.com/ayflying/utility_go/service" service2 "github.com/ayflying/utility_go/service"
"github.com/ayflying/utility_go/tools" "github.com/ayflying/utility_go/tools"
"github.com/gogf/gf/v2/container/gset" "github.com/gogf/gf/v2/container/gset"
@@ -221,46 +221,10 @@ func (s *sGameAct) Save(actId int) (err error) {
// 清空GetRedDot缓存 // 清空GetRedDot缓存
func (s *sGameAct) RefreshGetRedDotCache(uid int64) { func (s *sGameAct) RefreshGetRedDotCache(uid int64) {
//cacheKey2 := fmt.Sprintf("gameAct:GetRedDot:%d", uid) cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("d"), uid)
cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("Ymd"), uid) _, err := pgk.Cache("redis").Remove(gctx.New(), cacheKey)
act.Cache.Remove(gctx.New(), cacheKey) if err != nil {
g.Log().Error(ctx, err)
g.Dump(err)
}
} }
//
//func (s *sGameAct) GetRedDot(uid int64) (res map[string]int32, err error) {
// cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("Ymd"), uid)
// if get, _ := act.Cache.Get(ctx, cacheKey); !get.IsEmpty() {
// err = get.Scan(&res)
// return
// }
//
// res = make(map[string]int32)
//
// //res["notice_count"] = 0
// //获取所有帖子红点
// for _, v := range communityNotice.Types {
// res[fmt.Sprintf("notice_%d", v)], err = service.CommunityNotice().Ping(uid, noticeV1.NoticeType(v))
// }
//
// //邮件红点
// res["mail_count"], err = service.GameMail().RedDot(uid)
//
// //act1可领取数量
// res["act1_count"], err = act1.New().RedDot(uid)
//
// //act2可领取数量
// res["act2_count"], err = act2.New().RedDot(uid)
//
// //成就红点
// res["act4_count"], err = act4.New().RedDot(uid)
//
// //广告点击
// res["act6_count"], err = act6.New().RedDot(uid)
//
// for k, v := range act.RedDotList {
// res[k] = v(uid)
// }
//
// aycache.New().Set(ctx, cacheKey, res, time.Hour)
// return
//}

View File

@@ -1,8 +1,10 @@
package drive package drive
import ( import (
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/gctx"
) )
var adapterRedisClient gcache.Adapter var adapterRedisClient gcache.Adapter
@@ -11,7 +13,13 @@ var adapterRedisCache = gcache.New()
func NewAdapterRedis() gcache.Adapter { func NewAdapterRedis() gcache.Adapter {
if adapterRedisClient == nil { if adapterRedisClient == nil {
adapterRedisClient = gcache.NewAdapterRedis(g.Redis("default")) _cfg, _ := g.Cfg().Get(gctx.New(), "redis.default")
var cfg *gredis.Config
_cfg.Scan(&cfg)
redisObj, _ := gredis.New(cfg)
//adapterRedisClient = gcache.NewAdapterRedis(g.Redis("default"))
adapterRedisClient = gcache.NewAdapterRedis(redisObj)
adapterRedisCache.SetAdapter(adapterRedisClient) adapterRedisCache.SetAdapter(adapterRedisClient)
} }
return adapterRedisCache return adapterRedisCache

View File

@@ -6,17 +6,17 @@ import (
"github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/os/gtime"
) )
type Mod struct { type DingTalkMod struct {
DingTalkWebHook string DingTalkWebHook string
} }
func Load(webHook string) *Mod { func DingTalkLoad(webHook string) *DingTalkMod {
return &Mod{ return &DingTalkMod{
DingTalkWebHook: webHook, DingTalkWebHook: webHook,
} }
} }
func (m Mod) Send(value string) { func (m DingTalkMod) Send(value string) {
ctx := gctx.New() ctx := gctx.New()
// 从配置中获取发送者名称 // 从配置中获取发送者名称
name, _ := g.Cfg().Get(ctx, "name") name, _ := g.Cfg().Get(ctx, "name")

51
pgk/notice/drive/email.go Normal file
View File

@@ -0,0 +1,51 @@
package drive
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"gopkg.in/gomail.v2"
)
type MailMod struct {
Host string `json:"host" dc:"邮件服务器地址"`
Port int `json:"port" dc:"邮件服务器端口"`
User string `json:"user" dc:"邮件服务器用户名"`
Pass string `json:"pass" dc:"邮件服务器密码"`
From string `json:"from" dc:"邮件发送者"`
To string `json:"to" dc:"邮件接收者"`
Subject string `json:"subject" dc:"邮件主题"`
}
func MailLoad(Host string, port int, to string, subject string) *MailMod {
return &MailMod{
Host: Host,
Port: port,
User: "root",
Pass: "root",
From: "root",
To: to,
Subject: subject,
}
}
func (m MailMod) Send(value string) {
// 创建一个新的消息
obj := gomail.NewMessage()
// 设置发件人
obj.SetHeader("From", m.From)
// 设置收件人
obj.SetHeader("To", m.To)
// 设置邮件主题
obj.SetHeader("Subject", m.Subject)
// 设置邮件正文
obj.SetBody("text/plain", value)
// 创建 SMTP 拨号器,这里需要提供 SMTP 服务器地址、端口、发件人邮箱和密码
d := gomail.NewDialer(m.Host, m.Port, m.User, m.Pass)
// 发送邮件
if err := d.DialAndSend(obj); err != nil {
g.Log().Error(gctx.New(), err)
}
return
}

View File

@@ -9,11 +9,12 @@ type MessageV1 interface {
Send(value string) Send(value string)
} }
func New(typ v1.NoticeType, host string) MessageV1 { func New(typ v1.NoticeType, host string, value ...interface{}) MessageV1 {
switch typ { switch typ {
case v1.NoticeType_DINGTALK: case v1.NoticeType_DINGTALK:
return drive.Load(host) return drive.DingTalkLoad(host)
case v1.NoticeType_EMAIL:
return drive.MailLoad(host, value[0].(int), value[1].(string), value[2].(string))
} }
return nil return nil
} }

View File