What ' s memcached?
Memcached is a caching system that caches data in a key-value form. Increase the speed of data acquisition by caching data into memory.
Memcached to save the data in the form of Key-value, you can associate a key for each piece of data, and then you can get the data from this key later.
is memcached a library or something? Memcached is actually a separate Web server program. Its network base is based on libevent, you can run it on a server in the network, through the network, in accordance with the Memcached protocol based on communication with the memcached server.
What do we want to wrap?
What do we need to do? We only need to follow the Memcached protocol (see the document), encapsulate the communication at the network level, so that the upper layer can cache the data to the memcached server and fetch the data by invoking an interface such as Add/get. Top programmers have no idea that the data exists on the Web.
This thing, which is Memcached's official "client" APIs. You can use the Out-of-the-box client library, but you can also take this job of reinventing the wheel as an exercise in network programming. It ' s up to you:D
Where to start?
Unfortunately, for Windows users, memcached does not give an executable download (if you are a VC user) that can be executed or can be F7 directly. Fortunately, someone has already done this conversion work.
You can download the Windows version of memcached from http://jehiah.cz/projects/memcached-win32/, including executable programs and source code.
We can directly run Memcached.exe to install/Open the memcached server, the specific steps mentioned in the above page:
Installation: Memcached.exe-dinstall, which adds a memcached service to the Windows service
Run: Memcached.exe-dstart, you can also run through Windows Service management.
You can then see a ' memcached ' process in the Task Manager, which takes up a lot of memory because it's memcached.
So, here we go ...
Memcached, which runs through the steps above, defaults to 11211-Port listening (a TCP connection that can be viewed through netstat). Next, we can connect to the port and then send/recv the data. Send/Receive data just follow the Memcached protocol format, everything is simple.
Use the simplest blocking socket to connect to the memcached server:
SOCKET s = socket( AF_INET, SOCK_STREAM, 0 );
struct sockaddr_in addr;
memset( &addr, 0, sizeof( addr ) );
addr.sin_family = AF_INET;
addr.sin_port = htons( 11211 );
addr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
ret = connect( s, (struct sockaddr*) &addr, sizeof( addr ) );
if( ret == 0 )
{
printf( "connect ok\n" );
}