go內(nèi)存緩存BigCache封裝Entry源碼解讀
bigcache存儲
在bigcache存儲中,數(shù)據(jù)值存儲的形式為[]byte。
我們通過一個,存儲的時候,同時會把 hash值,key長度以及值,時間戳,entry同時存起來。
我們可以簡稱為 header + entry
header的存儲大小為 20字節(jié) [20]byte
每個entry由5部分組成,分別是時間戳(8byte)、key的hash值(8byte)、key的長度(2byte)、key的值本身以及value的值本身。
這里通過小端字節(jié)序來存儲,所以后續(xù)的反編譯也應(yīng)該指定這種模式。從PutUint64、PutUint16也可以對應(yīng)到字節(jié)的大小。
對應(yīng)源碼
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()函數(shù)封裝
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內(nèi)存緩存BigCache封裝Entry源碼解讀的詳細內(nèi)容,更多關(guān)于go內(nèi)存緩存BigCache封裝Entry的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
golang基礎(chǔ)之字符串與int、int64類型互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于golang基礎(chǔ)之字符串與int、int64類型互相轉(zhuǎn)換的相關(guān)資料,在Go語言中string轉(zhuǎn)int是一項常見的操作,需要的朋友可以參考下2023-07-07