SoFunction
Updated on 2025-03-01

Go memory cache BigCache Entry encapsulation source code reading

introduce

In bigcache storage, the data value storage is in the form of []byte.

When we store a, we will save the hash value, key value, timestamp, and entry at the same time.

We can simply call it header + entry

The storage size of the header is 18 bytes [18]byte

Encapsulation via wrapEntry() function

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
    .PutUint64(blob, timestamp)
    .PutUint64(blob[timestampSizeInBytes:], hash)
    .PutUint16(blob[timestampSizeInBytes+hashSizeInBytes:], uint16(keyLength))
    copy(blob[headersSizeInBytes:], key)
    copy(blob[headersSizeInBytes+keyLength:], entry)
    return blob[:blobLength]
}

The above is the detailed content of the Entry encapsulated source code of go memory cache BigCache. For more information about go memory cache BigCache Entry, please pay attention to my other related articles!