3 Star 2 Fork 0

info-superbahn-ict/superbahn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cache.go 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
tangshibo 提交于 2022-01-06 11:04 +08:00 . update
package cache
import (
"sync"
"time"
)
type Metrics struct {
Cpu float64
Memory float64
Time time.Time
}
func (m *Metrics)DeepCopy() *Metrics {
c := *m
return &c
}
type MetricsCache struct {
size int64
head int64
tail int64
rwLock *sync.RWMutex
metricCahce []*Metrics
}
func NewMetricsCache(size int64) *MetricsCache {
return &MetricsCache{
head: 0,
tail: 0,
size: size + 1,
rwLock: &sync.RWMutex{},
metricCahce: make([]*Metrics, size+1),
}
}
func (r *MetricsCache)Put(m *Metrics){
r.rwLock.Lock()
defer r.rwLock.Unlock()
if (r.tail + 1) % r.size == r.head {
r.metricCahce[r.head] = nil
r.head = (r.head + 1) % r.size
}
r.metricCahce[r.tail]=m.DeepCopy()
r.tail = (r.tail + 1) % r.size
}
func (r *MetricsCache) Read(size int64)[]*Metrics {
r.rwLock.Lock()
defer r.rwLock.Unlock()
if size > (r.tail + r.size - r.head ) % r.size {
size = (r.tail + r.size - r.head ) % r.size
}
data := make([]*Metrics,size)
for i,j := int64(0),r.head ; i < size; i++ {
data[i] = r.metricCahce[j].DeepCopy()
j = (j+1) % r.size
}
return data
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/info-superbahn-ict/superbahn.git
git@gitee.com:info-superbahn-ict/superbahn.git
info-superbahn-ict
superbahn
superbahn
5fda629dab96

搜索帮助