| 1 |
package fastcache |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"sync/atomic" |
| 6 |
|
| 7 |
xxhash "github.com/cespare/xxhash/v2" |
| 8 |
) |
| 9 |
|
| 10 |
// maxSubvalueLen is the maximum size of subvalue chunk. |
| 11 |
// |
| 12 |
// - 16 bytes are for subkey encoding |
| 13 |
// - 4 bytes are for len(key)+len(value) encoding inside fastcache |
| 14 |
// - 1 byte is implementation detail of fastcache |
| 15 |
const maxSubvalueLen = chunkSize - 16 - 4 - 1 |
| 16 |
|
| 17 |
// maxKeyLen is the maximum size of key. |
| 18 |
// |
| 19 |
// - 16 bytes are for (hash + valueLen) |
| 20 |
// - 4 bytes are for len(key)+len(subkey) |
| 21 |
// - 1 byte is implementation detail of fastcache |
| 22 |
const maxKeyLen = chunkSize - 16 - 4 - 1 |
| 23 |
|
| 24 |
// SetBig sets (k, v) to c where len(v) may exceed 64KB. |
| 25 |
// |
| 26 |
// GetBig must be used for reading stored values. |
| 27 |
// |
| 28 |
// The stored entry may be evicted at any time either due to cache |
| 29 |
// overflow or due to unlikely hash collision. |
| 30 |
// Pass higher maxBytes value to New if the added items disappear |
| 31 |
// frequently. |
| 32 |
// |
| 33 |
// It is safe to store entries smaller than 64KB with SetBig. |
| 34 |
// |
| 35 |
// k and v contents may be modified after returning from SetBig. |
| 36 |
func (c *Cache) SetBig(k, v []byte) { |
| 37 |
atomic.AddUint64(&c.bigStats.SetBigCalls, 1) |
| 38 |
if len(k) > maxKeyLen { |
| 39 |
atomic.AddUint64(&c.bigStats.TooBigKeyErrors, 1) |
| 40 |
return |
| 41 |
} |
| 42 |
valueLen := len(v) |
| 43 |
valueHash := xxhash.Sum64(v) |
| 44 |
|
| 45 |
// Split v into chunks with up to 64Kb each. |
| 46 |
subkey := getSubkeyBuf() |
| 47 |
var i uint64 |
| 48 |
for len(v) > 0 { |
| 49 |
subkey.B = marshalUint64(subkey.B[:0], valueHash) |
| 50 |
subkey.B = marshalUint64(subkey.B, uint64(i)) |
| 51 |
i++ |
| 52 |
subvalueLen := maxSubvalueLen |
| 53 |
if len(v) < subvalueLen { |
| 54 |
subvalueLen = len(v) |
| 55 |
} |
| 56 |
subvalue := v[:subvalueLen] |
| 57 |
v = v[subvalueLen:] |
| 58 |
c.Set(subkey.B, subvalue) |
| 59 |
} |
| 60 |
|
| 61 |
// Write metavalue, which consists of valueHash and valueLen. |
| 62 |
subkey.B = marshalUint64(subkey.B[:0], valueHash) |
| 63 |
subkey.B = marshalUint64(subkey.B, uint64(valueLen)) |
| 64 |
c.Set(k, subkey.B) |
| 65 |
putSubkeyBuf(subkey) |
| 66 |
} |
| 67 |
|
| 68 |
// GetBig searches for the value for the given k, appends it to dst |
| 69 |
// and returns the result. |
| 70 |
// |
| 71 |
// GetBig returns only values stored via SetBig. It doesn't work |
| 72 |
// with values stored via other methods. |
| 73 |
// |
| 74 |
// k contents may be modified after returning from GetBig. |
| 75 |
func (c *Cache) GetBig(dst, k []byte) (r []byte) { |
| 76 |
atomic.AddUint64(&c.bigStats.GetBigCalls, 1) |
| 77 |
subkey := getSubkeyBuf() |
| 78 |
dstWasNil := dst == nil |
| 79 |
defer func() { |
| 80 |
putSubkeyBuf(subkey) |
| 81 |
if len(r) == 0 && dstWasNil { |
| 82 |
// Guarantee that if the caller provided nil and this is a cache miss that |
| 83 |
// the caller can accurately test for a cache miss with `if r == nil`. |
| 84 |
r = nil |
| 85 |
} |
| 86 |
}() |
| 87 |
|
| 88 |
// Read and parse metavalue |
| 89 |
subkey.B = c.Get(subkey.B[:0], k) |
| 90 |
if len(subkey.B) == 0 { |
| 91 |
// Nothing found. |
| 92 |
return dst |
| 93 |
} |
| 94 |
if len(subkey.B) != 16 { |
| 95 |
atomic.AddUint64(&c.bigStats.InvalidMetavalueErrors, 1) |
| 96 |
return dst |
| 97 |
} |
| 98 |
valueHash := unmarshalUint64(subkey.B) |
| 99 |
valueLen := unmarshalUint64(subkey.B[8:]) |
| 100 |
|
| 101 |
// Collect result from chunks. |
| 102 |
dstLen := len(dst) |
| 103 |
if n := dstLen + int(valueLen) - cap(dst); n > 0 { |
| 104 |
dst = append(dst[:cap(dst)], make([]byte, n)...) |
| 105 |
} |
| 106 |
dst = dst[:dstLen] |
| 107 |
var i uint64 |
| 108 |
for uint64(len(dst)-dstLen) < valueLen { |
| 109 |
subkey.B = marshalUint64(subkey.B[:0], valueHash) |
| 110 |
subkey.B = marshalUint64(subkey.B, uint64(i)) |
| 111 |
i++ |
| 112 |
dstNew := c.Get(dst, subkey.B) |
| 113 |
if len(dstNew) == len(dst) { |
| 114 |
// Cannot find subvalue |
| 115 |
return dst[:dstLen] |
| 116 |
} |
| 117 |
dst = dstNew |
| 118 |
} |
| 119 |
|
| 120 |
// Verify the obtained value. |
| 121 |
v := dst[dstLen:] |
| 122 |
if uint64(len(v)) != valueLen { |
| 123 |
atomic.AddUint64(&c.bigStats.InvalidValueLenErrors, 1) |
| 124 |
return dst[:dstLen] |
| 125 |
} |
| 126 |
h := xxhash.Sum64(v) |
| 127 |
if h != valueHash { |
| 128 |
atomic.AddUint64(&c.bigStats.InvalidValueHashErrors, 1) |
| 129 |
return dst[:dstLen] |
| 130 |
} |
| 131 |
return dst |
| 132 |
} |
| 133 |
|
| 134 |
func getSubkeyBuf() *bytesBuf { |
| 135 |
v := subkeyPool.Get() |
| 136 |
if v == nil { |
| 137 |
return &bytesBuf{} |
| 138 |
} |
| 139 |
return v.(*bytesBuf) |
| 140 |
} |
| 141 |
|
| 142 |
func putSubkeyBuf(bb *bytesBuf) { |
| 143 |
bb.B = bb.B[:0] |
| 144 |
subkeyPool.Put(bb) |
| 145 |
} |
| 146 |
|
| 147 |
var subkeyPool sync.Pool |
| 148 |
|
| 149 |
type bytesBuf struct { |
| 150 |
B []byte |
| 151 |
} |
| 152 |
|
| 153 |
func marshalUint64(dst []byte, u uint64) []byte { |
| 154 |
return append(dst, byte(u>>56), byte(u>>48), byte(u>>40), byte(u>>32), byte(u>>24), byte(u>>16), byte(u>>8), byte(u)) |
| 155 |
} |
| 156 |
|
| 157 |
func unmarshalUint64(src []byte) uint64 { |
| 158 |
_ = src[7] |
| 159 |
return uint64(src[0])<<56 | uint64(src[1])<<48 | uint64(src[2])<<40 | uint64(src[3])<<32 | uint64(src[4])<<24 | uint64(src[5])<<16 | uint64(src[6])<<8 | uint64(src[7]) |
| 160 |
} |