1. Introduction
bigcache is a memory cache system that stores key-value pair data. No gc operation. When using it, serialization (inverse).
The source code of bigcache is/allegro/bigcacheSeveral features are stored through []byte, without expiration time.
2. Installation
We install the latest v3 version
go get -u /allegro/bigcache/v3
After the installation is complete, we can use bigcache in the go language. Here are some simple examples.
3. Use examples
func TestSetGet(t *) { // new a bigCache object cache, _ := ((), (10*)) // get a valueless key vNil, err := ("key") (vNil, err) // [] Entry not found [] byte slice with empty value // set ("key", []byte("value")) // get v, _ := ("key") (v) // Output [118 97 108 117 101] (string(v)) // Output value}
Let's take a look at the source code of Set and Get methods
// Set saves entry under the key func (c *BigCache) Set(key string, entry []byte) error { hashedKey := .Sum64(key) shard := (hashedKey) return (key, hashedKey, entry) } // Get reads entry for the key. // It returns an ErrEntryNotFound when // no entry exists for the given key. func (c *BigCache) Get(key string) ([]byte, error) { hashedKey := .Sum64(key) shard := (hashedKey) return (key, hashedKey) }
In the Set() method, the stored value is[]byte
Byte slice type, so when we save it, we need to serialize the data into[]byte
。
And in the Get() method, the obtained value is[]byte
,We need to deserialize[]byte
To the original type.
3. 1 string type storage, acquisition and modification
The string type can be converted using []byte and type, which is convenient for each other.
func TestSetGet(t *) { // new a bigCache object cache, _ := ((), (10*)) // get a valueless key vNil, err := ("key") (vNil, err) // [] Entry not found [] byte slice with empty value // set ("key", []byte("value")) // get v, _ := ("key") (v) // Output [118 97 108 117 101] (string(v)) // Output value}
3.2 Storage, acquisition and modification of non-string types
It is not a string type, converted to []byte, which is quite complicated. Among the built-in libraries in go, the only json library provides such a method. You can convert any type that is not string to []byte, as follows.
func Marshal(v any) ([]byte, error) // Serializationfunc Unmarshal(data []byte, v any) error // Deserialization
Here we also use json's serialization (anti) method to convert it with []byte.
3. 2. 1. Storage, acquisition and modification of slice types
// TestSetGetSlice slice typefunc TestSetGetSlice(t *) { // new a bigCache object cache, _ := ((), (10*)) // slice store data s := []string{"1", "2"} bs, _ := (s) ("s", bs) // get the value bsv, _ := ("s") var s2 []string = make([]string, 0) _ = (bsv, &s2) (s2) // Modify data, modify s2 to see if it will affect the value in memory // The answer is that it will not affect s2 = append(s2, "3") (s2) var s3 []string = make([]string, 0) _ = (bsv, &s3) (s3) }
3. 2. 2. 2. 2 struct structure type storage, acquisition and modification
// TestSetGetStruct Struct Pointerfunc TestSetGetStruct(t *) { // new a bigCache object cache, _ := ((), (10*)) // Structure struct stores data type st struct { Value string } s := &st{ Value: "v1", } bs, _ := (s) ("s", bs) // get the value bsv, _ := ("s") var s2 st _ = (bsv, &s2) (s2) // {v1} // Modify data, modify s2 to see if it will affect the value in memory // The answer is that it will not affect = "v2" (s2) // {v2} var s3 st _ = (bsv, &s3) (s3) // {v1} }
in conclusion
Bigcache can be used safely, and after modification and acquisition, it will not affect the original value in memory.
The above is the detailed explanation of the introduction to the use of go memory cache BigCache. For more information about go memory cache BigCache, please follow my other related articles!