This is a creation in Article, where the information may have evolved or changed.
1.go Open Source Groupcache Project--groupcache Introduction
The code download address is as follows:
Https://github.com/golang/groupcache
The goal is to replace memcached in many scenarios.
1 memcached
Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a HashMapthat stores key/value pairs. Its daemon (daemon) is written in C , but the client can write in any language and communicate with the daemon through the memcached protocol.
2 Groupcache Brief Introduction
Groupcache is a cache and cache filter library written using the Go language as an alternative to many memcached scenarios.
2.1 Differences with memcached
First, Groupcache is similar to memcached: by Key sharding, and by key to query the response peer.
Second, the difference between Groupcache and memcached:
1. There is no need for separate settings for the server, which will significantly reduce the amount of deployment and configuration. Groupcache is both the client library and the server library, and connects to your peer.
2 a cache filtering mechanism. It is well known that when the memcached "Sorry,cache miss (cache loss)" occurs, the database (or other components) is often caused by a "surprise swarm effect" (thundering herd) due to the non-control of the number of users requested; Groupcache The cache population is reconciled, only one of the duplicate calls is placed in the cache, and the processing results are sent to all the same callers.
3. Multiple versions of values are not supported. If the "Foo" key corresponds to a value of "bar", then the value of the key "foo" is Always "bar". There is neither a cache expiration nor a clear cache recovery mechanism, so there is no CAs or increment/decrement.
4. Based on the previous change, Groupcache has automatic backup "super-hot" items for multiprocessing, which avoids overloading the machine CPU or NIC in memcached for excessive access to certain key values.
The biggest difference with memcached is that "there is no change or deletion," and once written it will not change. After giving up the characteristics of the update/delete, the following is the change:
L ability of Cluster.
L ability to deal with hot spots.
There was no intersection between memcached servers, and in Groupcache it was cluster up. In addition, in the memcached will be the same key at the same time to create a single CPU overloading problem, groupcache through the auto-mirror mechanism to solve.
3 Testing
Download required: Go Getgithub.com/golang/protobuf/proto
3.1 Code
Package Main
Import (
"FMT"
"Log"
"Net/http"
"OS"
"strings"
groupcache"Github.com/golang/groupcache"
)
func Main () {
//Usage:./test_groupcachePort
me:=":"+os. args[1]
Peers:=groupcache. Newhttppool ("http://localhost"+me)
peers. Set ("http://localhost:8081","http://localhost:8082","http://localhost:8083")
Helloworld:=groupcache. NewGroup ("HelloWorld",1024x768* 1024x768 *, Groupcache. Getterfunc (
func(ctxgroupcache. Context,keystring, Destgroupcache. Sink) Error {
Log. Println (Me)
dest. SetString (Me)
returnnil
}))
FMT. Println ("GroupName:", HelloWorld. Name ())
http. Handlefunc ("/xbox/",func(whttp). Responsewriter,r*http. Request) {
parts:=strings. SPLITN (R.url. path[len("/xbox/"):],"/",1)
Iflen(parts)! =1{
http. Error (W,"badRequest", http.) Statusbadrequest)
return
}
vardata[]byte
HelloWorld. Get (nil, parts[0],groupcache. Allocatingbyteslicesink (&data))
w.write (data)
Log. Println ("Gets:", HelloWorld. Stats.Gets.String ())
Log. Println ("Load:", HelloWorld. Stats.Loads.String ())
Log. Println ("localload:", HelloWorld. Stats.LocalLoads.String ())
Log. Println ("Peererror:", HelloWorld. Stats.PeerErrors.String ())
Log. Println ("peerload:", HelloWorld. Stats.PeerLoads.String ())
})
http. Listenandserve (Me,nil)
}
3.2 Perform the following
Need to add parameters
./test.exe 1989
Enter a port number to start the run.