代码拉取完成,页面将自动刷新
package cachedata
import (
"gitee.com/prestonTao/utils"
"sync"
"time"
)
//存储结构
type Cache struct {
Data *sync.Map //存储共享数据(key/value:hash/cachedata)
Time time.Time //更新时间
}
func NewCache() *Cache {
c := new(Cache)
c.Data = new(sync.Map)
c.Time = time.Now()
return c
}
//格式化保存key,value
//uptime true 则更新时间
func (c *Cache) Save(d *CacheData) {
k, v := d.buildKV()
c.Data.Store(k, v)
}
//根据KEY获取原始数据DATA
func (c *Cache) Get(key []byte) []byte {
khash := buildHash(key)
k := utils.Bytes2string(khash) // khash.B58String()
data, ok := c.Data.Load(k)
if ok {
b := data.([]byte)
cd, err := Parse(b)
if err != nil {
return nil
}
return cd.Value
}
return nil
}
//根据KEY删除数据
func (c *Cache) Del(key []byte) {
khash := buildHash(key)
k := utils.Bytes2string(khash) // khash.B58String()
c.Data.Delete(k)
}
//根据hash id 获取原始数据DATA
func (c *Cache) GetByHash(id *utils.Multihash) []byte {
k := utils.Bytes2string(id) //id.B58String()
data, ok := c.Data.Load(k)
if ok {
b := data.([]byte)
cd, err := Parse(b)
if err != nil {
return nil
}
return cd.Value
}
return nil
}
//根据原始key获取cachedata
func (c *Cache) GetCacheData(key []byte) *CacheData {
khash := buildHash(key)
k := utils.Bytes2string(khash) //khash.B58String()
data, ok := c.Data.Load(k)
if ok {
b := data.([]byte)
cd, err := Parse(b)
if err != nil {
return nil
}
return cd
}
return nil
}
//根据hash id 获取cachedata
func (c *Cache) GetCacheDataByHash(id *utils.Multihash) *CacheData {
k := utils.Bytes2string(id) //id.B58String()
data, ok := c.Data.Load(k)
if ok {
b := data.([]byte)
cd, err := Parse(b)
if err != nil {
return nil
}
return cd
}
return nil
}
func (c *Cache) UpTime() {
c.Time = time.Now()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。