diff --git a/report/data_buried.go b/report/data_buried.go index 54545fb..2c05223 100644 --- a/report/data_buried.go +++ b/report/data_buried.go @@ -11,6 +11,12 @@ func NewDataBuried[DataID comparable, Data any](hitLogic HitLogic[Data], options data: asynchronization.NewMap[DataID, Data](), hitLogic: hitLogic, } + buried.setData = func(id DataID, data Data) { + buried.data.Set(id, data) + } + buried.getData = func(id DataID) Data { + return buried.data.Get(id) + } for _, option := range options { option(buried) } @@ -22,18 +28,18 @@ func NewDataBuried[DataID comparable, Data any](hitLogic HitLogic[Data], options type DataBuried[DataID comparable, Data any] struct { data hash.Map[DataID, Data] hitLogic HitLogic[Data] + getData func(DataID) Data + setData func(id DataID, data Data) } // Hit 命中数据埋点 func (slf *DataBuried[DataID, Data]) Hit(id DataID, data Data) { - slf.data.Atom(func(m hash.Map[DataID, Data]) { - m.Set(id, slf.hitLogic(m.Get(id), data)) - }) + slf.setData(id, slf.hitLogic(slf.getData(id), data)) } // GetData 获取数据 func (slf *DataBuried[DataID, Data]) GetData(id DataID) Data { - return slf.data.Get(id) + return slf.getData(id) } // GetSize 获取已触发该埋点的id数量 diff --git a/report/data_buried_options.go b/report/data_buried_options.go index c4bd5f3..9353164 100644 --- a/report/data_buried_options.go +++ b/report/data_buried_options.go @@ -1,12 +1,13 @@ package report -import "github.com/kercylan98/minotaur/utils/synchronization" - type DataBuriedOption[DataID comparable, Data any] func(buried *DataBuried[DataID, Data]) -// WithDataBuriedSync 通过并发安全的方式创建数据埋点 -func WithDataBuriedSync[DataId comparable, Data any]() DataBuriedOption[DataId, Data] { - return func(buried *DataBuried[DataId, Data]) { - buried.data = synchronization.NewMap[DataId, Data]() +// WithDataBuriedStorage 通过特定的存储模式创建数据埋点 +// - 默认情况下埋点数据存储在内存中 +// - 使用该方式可以将埋点存储存储在其他如数据库、消息队列中 +func WithDataBuriedStorage[DataID comparable, Data any](getData func(id DataID) Data, setData func(id DataID, data Data)) DataBuriedOption[DataID, Data] { + return func(buried *DataBuried[DataID, Data]) { + buried.getData = getData + buried.setData = setData } } diff --git a/report/global_buried.go b/report/global_buried.go index 773bae1..1ab5186 100644 --- a/report/global_buried.go +++ b/report/global_buried.go @@ -1,35 +1,37 @@ package report -import ( - "sync" -) - // NewGlobalBuried 创建一个全局埋点 -func NewGlobalBuried[Data any](hitLogic HitLogic[Data]) *GlobalBuried[Data] { - return &GlobalBuried[Data]{ +func NewGlobalBuried[Data any](hitLogic HitLogic[Data], options ...GlobalBuriedOption[Data]) *GlobalBuried[Data] { + buried := &GlobalBuried[Data]{ hitLogic: hitLogic, } + buried.setData = func(data Data) { + buried.data = data + } + buried.getData = func() Data { + return buried.data + } + for _, option := range options { + option(buried) + } + return buried } // GlobalBuried 全局埋点 -// - 天然并发安全 // - 全局埋点适用于活跃用户数等统计 type GlobalBuried[Data any] struct { - rw sync.RWMutex data Data hitLogic HitLogic[Data] + getData func() Data + setData func(data Data) } // Hit 命中数据埋点 func (slf *GlobalBuried[Data]) Hit(data Data) { - slf.rw.Lock() - defer slf.rw.Unlock() - slf.data = slf.hitLogic(slf.data, data) + slf.setData(slf.hitLogic(slf.getData(), data)) } // GetData 获取数据 func (slf *GlobalBuried[Data]) GetData() Data { - slf.rw.RLock() - defer slf.rw.RUnlock() - return slf.data + return slf.getData() } diff --git a/report/global_buried_options.go b/report/global_buried_options.go new file mode 100644 index 0000000..adb3c5d --- /dev/null +++ b/report/global_buried_options.go @@ -0,0 +1,13 @@ +package report + +type GlobalBuriedOption[Data any] func(buried *GlobalBuried[Data]) + +// WithGlobalBuriedStorage 通过特定的存储模式创建全局埋点 +// - 默认情况下埋点数据存储在内存中 +// - 使用该方式可以将埋点存储存储在其他如数据库、消息队列中 +func WithGlobalBuriedStorage[DataID comparable, Data any](getData func() Data, setData func(data Data)) GlobalBuriedOption[Data] { + return func(buried *GlobalBuried[Data]) { + buried.getData = getData + buried.setData = setData + } +}