Build a memcached Environment

Source: Internet
Author: User

The previous article mentioned some ideas about memcached in the project. Before micro-innovation, we will first perform a test. Here we will give a summary.
Step 1: Install the memcached Server
Because memcached uses the libevent module, you should install libevent before this.
Sudo apt-get install libevent-devel
Download the memcached server. I installed the latest version 1.4.0.
$ Wget http://www.danga.com/memcached/dist/memcached-1.4.0.tar.gz
$ Tar zxf memcached-1.4.0.tar.gz
$ Memcached-1.4.0 cd
$./Configure
$ Make
$ Sudo make install
By default, memcached is installed in/usr/local/bin.
Then, start the memcached server in the/usr/local/bin directory and start it on the console to facilitate log observation.
$/Usr/local/bin/memcached-p 11211-m 64 m-vv
Now the memcached server has been started. If you want to use memcached in the project, you need to use
Client api. I started out using a lightweight client written by Kevin Lynx, an idol of my website, to support the windows version.
You can find it here. Later, I went to the official website to download a libmemcached
Step 2: Install libmemcached
$ Wget http://launchpad.net/libmemcached/1.0/1.0.2/+download/libmemcached-1.0.2.tar.gz
$ Tar zxf libmemcached-1.0.2.tar.gz
$ Libmemcached-1.0.2 cd
$./Configure
$ Make
$ Sudo make install
Some netizens mentioned that the memcached server path points to the problem during configure, and some solved the problem using soft connections. here our memcached
It has been installed in the path of/usr/local, so don't worry. No problem is found here.
At this point, libmemcached has been installed. You can see a bunch of files under/usr/local/bin. I only care about the two files that I need, and I have not looked at them in depth. You can also see the header file provided by libmemcached under/usr/local/include. The so file has also been generated, but you need to manually load it again.
$ Sudo ldconfig-v
The libmemcached so file is successfully loaded.
Next, we need to write a simple code for testing. The testing code is very simple, that is, the official test code, which has been slightly modified. please correct me if there is any error, after all, memcached is not a new technology. # Include <stdio. h>
# Include <stdlib. h>
# Include <libmemcached/memcached. h>
 
Int main ()
{
Memcached_st * memc;
Uint32_t flags;
Memcached_return rc;
Memcached_server_st * servers;
Memc = memcached_create (NULL );
Servers = memcached_servers_parse ("Wagner. 0.0.1: 11211 ");
Memcached_server_push (memc, servers );
Memcached_server_list_free (servers );
Memcached_behavior_set (memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0 );
Char * result_str;
Const char * pkey = "TestSet ";
// Const char * pkey = "this is a key";/* wrong !!! No space shoshould appear in the key */
Int result_str_length;
Result_str = memcached_get (memc, pkey, strlen (pkey ),
& Result_str_length, & flags, & rc );
If (rc = MEMCACHED_SUCCESS)
{
Printf ("% s \ n", result_str );
Free (result_str );
}
Else
{
// Insert into the memcached;
Printf ("we shoshould insert the data into cache \ n ");
Char * pdata = "this is a data ";
Rc = memcached_set (memc, pkey, strlen (pkey ),
Pdata, strlen (pdata), 0, 0 );
}
Memcached_free (memc );
 
Return 0;
} One Of Them encountered an episode: During the test, a random key was written as above,
The key string contains spaces. Later, the server reported an error and checked the protocol.
The key must not include control characters or whitespace.
Just remove the space.
Then compile the connection.
$ Gcc-g test. c-o test-lmemcached
It seems that there is a type conversion error during g ++ compilation. For now, no matter whether gcc can pass.
Run and check the debugging information on the server. The data is normal.
Close the work!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.