forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package cacheSer
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"stat/common/log"
|
||||
"stat/common/redis"
|
||||
"stat/common/statutils"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
|
||||
"github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
type IpStatCache struct {
|
||||
Lock sync.RWMutex
|
||||
IpLimitBucketTopToday redis.BucketList
|
||||
IpLimitBucketTopYesterday redis.BucketList
|
||||
}
|
||||
|
||||
var (
|
||||
Cache *cache.Cache
|
||||
IpLimitStatCache = IpStatCache{
|
||||
IpLimitBucketTopToday: make(redis.BucketList, 0),
|
||||
IpLimitBucketTopYesterday: make(redis.BucketList, 0),
|
||||
}
|
||||
Writers struct {
|
||||
LandPageVisitWriter BaseWriter
|
||||
LandPageClickWriter BaseWriter
|
||||
}
|
||||
_cron *cron.Cron
|
||||
)
|
||||
|
||||
func Init() {
|
||||
Cache = cache.New(10*time.Minute, 10*time.Minute)
|
||||
Writers.LandPageVisitWriter = NewWriter(landPageVisitSync)
|
||||
Writers.LandPageClickWriter = NewWriter(landPageClickSync)
|
||||
_cron = cron.New(cron.WithSeconds(), cron.WithChain(cron.SkipIfStillRunning(&log.Logger{}))) // 秒级定时任务
|
||||
_, _ = _cron.AddFunc("*/5 * * * * ?", func() {
|
||||
err := statutils.ServerMon.RunWithName(SyncWriters, statutils.GetFuncName(SyncWriters))
|
||||
if err != nil {
|
||||
log.Error("SyncWriters err", log.E(err))
|
||||
}
|
||||
})
|
||||
_cron.Start()
|
||||
}
|
||||
|
||||
func SyncWriters() error {
|
||||
err := Writers.LandPageVisitWriter.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Writers.LandPageClickWriter.Sync()
|
||||
}
|
||||
|
||||
func Key(key string) string {
|
||||
pc, _, _, _ := runtime.Caller(1)
|
||||
return runtime.FuncForPC(pc).Name() + "##" + key
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cacheSer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stat/models/downloadCpc"
|
||||
"stat/models/downloadinfo"
|
||||
)
|
||||
|
||||
func landPageVisitSync(list []interface{}) error {
|
||||
if len(list) > 0 {
|
||||
return downloadCpc.InsertDownloadInfoMany(context.Background(), list)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func landPageClickSync(list []interface{}) error {
|
||||
if len(list) > 0 {
|
||||
return downloadinfo.InsertDownloadInfoMany(context.Background(), list)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cacheSer
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type SyncFunc func(list []interface{}) error
|
||||
|
||||
type BaseWriter interface {
|
||||
Add(doc interface{})
|
||||
Sync() error
|
||||
}
|
||||
|
||||
type Writer struct {
|
||||
lock sync.Mutex
|
||||
list []interface{}
|
||||
sync SyncFunc
|
||||
}
|
||||
|
||||
func NewWriter(syncFunc SyncFunc) BaseWriter {
|
||||
return &Writer{
|
||||
lock: sync.Mutex{},
|
||||
list: make([]interface{}, 0),
|
||||
sync: syncFunc,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Writer) Add(doc interface{}) {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
w.list = append(w.list, doc)
|
||||
}
|
||||
|
||||
func (w *Writer) Sync() error {
|
||||
list := make([]interface{}, 0)
|
||||
func() {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
list = w.list
|
||||
w.list = make([]interface{}, 0)
|
||||
}()
|
||||
return w.sync(list)
|
||||
}
|
||||
Reference in New Issue
Block a user