c5_labsci/zciyon/memkv.go
2026-01-27 00:52:00 +08:00

51 lines
872 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package zciyon
import "runtime"
type kvitem struct {
data any
exptimes int
}
//删除后不被自动回收内存需手动重建Kv变量
var MemKV map[string]kvitem
func init() {
MemKV = make(map[string]kvitem)
}
func Memsetrows(key string, value any, sec int) {
MemKV[key] = kvitem{data: value, exptimes: Tostamp() + sec}
}
func Memgetrows(key string) any {
if value, ok := MemKV[key]; ok {
if value.exptimes > Tostamp() {
return value.data
} else {
delete(MemKV, key)
return nil
}
}
return nil
}
func Memdel(key string) {
delete(MemKV, key)
}
func MemRebuild() {
kv := make(map[string]kvitem)
for k, v := range MemKV {
kv[k] = v
}
MemKV = kv
}
func Memstats() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
Clog("Alloc = %v TotalAlloc = %v Sys = %v NumGC = %v",
m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC)
}