2025-02-17 16:29:07 +08:00

52 lines
805 B
Go

package svc
import (
"encoding/json"
"sync"
)
type EcpmConfigCli struct {
*sync.Map
}
func NewEcpmConfig() *EcpmConfigCli {
return &EcpmConfigCli{
Map: &sync.Map{},
}
}
type Ecpm struct {
AppId string `json:"appId"`
ECPM float64 `json:"eCPM"`
IPU int `json:"IPU"`
}
func (c *EcpmConfigCli) Get(appId string) (*Ecpm, bool) {
v, ok := c.Map.Load(appId)
if !ok {
return nil, false
}
return v.(*Ecpm), true
}
func (c *EcpmConfigCli) Set(appId string, ecpmConfig *Ecpm) {
c.Map.Store(appId, ecpmConfig)
}
func (c *EcpmConfigCli) SetAll(list []Ecpm) {
c.Map.Clear()
for _, v := range list {
c.Map.Store(v.AppId, &v)
}
}
func (c *EcpmConfigCli) SetAllByJson(data []byte) {
var list []Ecpm
err := json.Unmarshal(data, &list)
if err != nil {
return
}
c.SetAll(list)
}