This is a creation in Article, where the information may have evolved or changed.
9.go Open Source Groupcache Project Note--LRU Code
LRU is the least recently used algorithm for leastrecently used.
A page replacement algorithm for memory management, in which data blocks (memory blocks) that are not used in memory are called LRU, the operating system frees up space to load additional data based on what data belongs to the LRU and moves it out of memory.
LRU is used to implement the LRU cache.
1 LRU Files
1.1 Cache structure
A cache structure is defined first.
maxentries int is the maximum cache capacity.
onevicted func (key key, Value interface{}) is an optional callback function that is called when an item is purged from the cache.
ll *list. List is a pointer to a linked list
Cache map[interface{}]*list. element is a map type that is a pointer to an element in the list.
ps:interface{}, in go it can point to any object, also the arbitrary object is first this interface, you can think of it as object in the java/c#, void*. list. Element the type to which it points is the entry type
1.2 Other definitions
Defines the key as an interface, which means that key can be any value.
Defines entry as a struct, containing key and value. Where key is the key type and value is the interface.
2 new function
Returns a cache struct.
The entry parameter is an integer, which is the maximum capacity supported by the cache, and if 0 indicates no on-line
Create a new linked list.
3 Add function
The entry parameter is Key key,value interface{}.
First, determine if the current cache is nil, and if nil, the new one comes out first.
If the key already exists, move the element pointer to the front of the list and update the corresponding value, such as EE. Value. (*entry). value= value then.
If the key does not exist, the new value is added to the top of the list, the cache is added to the cache structure, and then the maximum number of caches is exceeded, and removeoldest is called to delete the oldest entry.
4 Get function
The entry parameter is the key type. Returns the value and whether it is OK.
If no cache is present, swap back directly.
If present means a hit, move the element pointer to the front of the list and return the element and true
5 Remove function
If the cache does not exist, it is returned directly.
If present, call removeelement to remove the pointer.
6 Removeoldest function
If the cache does not exist, it is returned directly.
If there is a cache, call Removeelement to start the deletion from the back end of the list.
7 removeelement function
The entry parameter is a list pointer.
The Remove function of the direct call list removes the pointer.
Then delete the corresponding item from map (c.cache) using the Delete command
Call the callback function if there is a callback function.
8 Len function
Returns the length of the list, if the map in the cache struct is nil directly returns 0.
Ps:go implement a member method of a class, after the Func add class, the implementation of the interface in Go is not the same as in Java, but as long as a class to implement all the methods of an interface, you can assume that the class implements the interface.
Go in the beginning of the letter capitalized variable name, class name, method name is known to the external, lowercase beginning of the expression is not exposed. Another kind of code ele. Value. (*entry). Value, where (*entry) indicates that value is converted to *entry type access.
9 Lru_teset
Used to test the LRU.
Defines two structural bodies simplestruct,complexstruct
then define a variable gettests, as follows
vargettests=[]struct{
name string
keytoadd interface{}
keytoget interface{}
Expectedokbool
}{
{"String_hit","MyKey","MyKey",true},
{"String_miss","MyKey","nonsense",false},
{"Simple_struct_hit", simplestruct{1,"both"},simplestruct{1," Both "},true},
{"Simeple_struct_miss", simplestruct{1,"both"},simplestruct{0, "NoWAY"},false},
{"Complex_struct_hit", complexstruct{1, simplestruct{2,"three" }},
complexstruct{1, simplestruct{2,"three"}},true},
}
The main two use cases are as follows
9.1 Testget
From the variable gettests loop fetch, first call new to create an LRU.
Then according to Gettests. Keytoadd to increase the cache. Each key value is 1234.
Then, when the increment is completed, the key value is satisfied.
The test also ends after the loop is finished.
9.2 Testremove
Add an LRU first.
Then add a key value, test OK
Then delete the key values. Check whether the deletion was successful.
9.3 Test Results
= = = RUN Testget
---pass:testget (0.00s)
= = = RUN Testremove
---pass:testremove (0.00s)
PASS
OK Test 0.135s