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)
}
