go内存缓存BigCache封装Entry源码解读
bigcache存储
在bigcache存储中,数据值存储的形式为[]byte。
我们通过一个,存储的时候,同时会把 hash值,key长度以及值,时间戳,entry同时存起来。
我们可以简称为 header + entry
header的存储大小为 20字节 [20]byte
每个entry由5部分组成,分别是时间戳(8byte)、key的hash值(8byte)、key的长度(2byte)、key的值本身以及value的值本身。
这里通过小端字节序来存储,所以后续的反编译也应该指定这种模式。从PutUint64、PutUint16也可以对应到字节的大小。
对应源码
binary.LittleEndian.PutUint64(blob, timestamp) binary.LittleEndian.PutUint64(blob[timestampSizeInBytes:], hash) binary.LittleEndian.PutUint16(blob[timestampSizeInBytes+hashSizeInBytes:], uint16(keyLength)) copy(blob[headersSizeInBytes:], key) copy(blob[headersSizeInBytes+keyLength:], entry)
通过wrapEntry()函数封装
const ( timestampSizeInBytes = 8 // Number of bytes used for timestamp hashSizeInBytes = 8 // Number of bytes used for hash keySizeInBytes = 2 // Number of bytes used for size of entry key headersSizeInBytes = timestampSizeInBytes + hashSizeInBytes + keySizeInBytes // Number of bytes used for all headers ) func wrapEntry(timestamp uint64, hash uint64, key string, entry []byte, buffer *[]byte) []byte { keyLength := len(key) blobLength := len(entry) + headersSizeInBytes + keyLength if blobLength > len(*buffer) { *buffer = make([]byte, blobLength) } blob := *buffer binary.LittleEndian.PutUint64(blob, timestamp) binary.LittleEndian.PutUint64(blob[timestampSizeInBytes:], hash) binary.LittleEndian.PutUint16(blob[timestampSizeInBytes+hashSizeInBytes:], uint16(keyLength)) copy(blob[headersSizeInBytes:], key) copy(blob[headersSizeInBytes+keyLength:], entry) return blob[:blobLength] }
以上就是go内存缓存BigCache封装Entry源码解读的详细内容,更多关于go内存缓存BigCache封装Entry的资料请关注脚本之家其它相关文章!
相关文章
Goland 的安装及激活教程(window、linux下安装)
这篇文章主要介绍了Golang Goland 的安装及激活详细教程,包括window下安装goland和linux下安装goland,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-10-10
最新评论