Http://blog.sina.com.cn/s/blog_6145ed810102vqu6.html
I used brew to install it, so let's review the whole process. If you do not have brew, look at step one first, otherwise look directly at step two.
Step One: Install Homebrew
First see if the following conditions are met:
Intel 的 CPUOS X 10.5 或者更高安装了XCode 或者 XCode命令行工具
Meet, you can install Homebrew, the command is as follows:
$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
The specific homebrew can be viewed below
Homebrew Online version A lot: I used a few are showing 404 Bad Request
Cannot now
There is a URL that gives the latest installation address: the URL is:http://brew.sh/index_zh-cn.html
At the bottom of the website is given the latest in downloading homebrew via terminal:
The most recent path I have downloaded is:
Ruby-e "$ (curl-fssl https://raw.githubusercontent.com/Homebrew/install/master/install)"
After a long download: Finally show the installation success
Step Two: Install memcached
Before installing, you can look up and see if there are:
$ brew search memcache
return Result:
libmemcached memcache-top memcached memcacheq
Description and keywords memcache
related to the above four, so confirm that there is something we need, the first is the client, the third is the server.
Then install it!
To install the server first:
$ brew install memcached
Installation log:
==> installing memcached dependency:libevent==> downloading https://github.com/downloads/libevent/libevent/ libevent-2.0.21-s######################################################################## 100.0%==>./ Configure--disable-debug-mode--prefix=/usr/local/cellar/libevent/2.0.21==> make==> make install?? /usr/local/cellar/libevent/2.0.21:48 files, 1.8M, built in seconds==> installing memcached==> downloading http: memcached.googlecode.com/files/memcached-1.4.15.tar.gz###################################################### ################## 100.0%==>./configure--prefix=/usr/local/cellar/memcached/1.4.15--disable-coverage==> Make install==> Caveatsto has launchd start memcached at Login:ln-sfv/usr/local/opt/memcached/*.plist ~/library/ Launchagentsthen to load memcached now:launchctl load ~/library/launchagents/homebrew.mxcl.memcached.plistor, if you D On ' t want/need launchctl, you can just run:/usr/local/opt/memcached/bin/memcached== Summary?? /usr/local/cellar/memcached/1.4.15:10 files, 176K, built in 8 seconds
From the above installation log, you can see:
- Install the Libevent library on which it depends before installing memcached
- Downloaded libevent and memcached, is installed under/usr/local/cellar, but also automatically under the/usr/local/bin to establish a soft connection, easy to use.
After installation, you can view the results of the installation:
$ which memcached/usr/local/bin/memcached$ memcached -hmemcached 1.4.15...
Step Two: Install libmemcached
To continue installing the client library:
$ brew install libmemcached==> Downloading https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemca######################################################################## 100.0%==> ./configure --prefix=/usr/local/Cellar/libmemcached/1.0.16==> make install?? /usr/local/Cellar/libmemcached/1.0.16: 110 files, 1.4M, built in 108 seconds
Step three: Start the server
Start with the default parameters first:
$ /usr/local/bin/memcached -d
Step Four: Test
Listing 3. Start memcached
./memcached-d-M 2048-l 10.0.0.40-p 11211 |
This launches memcached () as a daemon -d
, allocates 2GB of memory ( -m 2048
), and specifies a listening localhost, port 11211. You can modify these values as needed, but the above settings are sufficient to complete the exercises in this article. Next, you need to connect to memcached. You will use a simple Telnet client to connect to the memcached server.
Most operating systems provide a built-in Telnet client, but if you are using a Windows-based operating system, you need to download a third-party client. I recommend using PuTTy.
After the Telnet client is installed, execute the command in Listing 4:
Listing 4. Connect to Memcached
If everything is OK, you should get a telnet response that will indicate Connected to localhost (already connected to localhost). If you do not get this response, you should return to the previous steps and ensure that the source files for both libevent and memcached have been successfully generated.
You are now logged on to the memcached server. Thereafter, you will be able to communicate with memcached through a series of simple commands. 9 memcached Client commands can be divided into three categories:
Basic memcached Client Commands
You will perform the simplest operation with five basic memcached commands. These commands and actions include:
set
add
replace
get
delete
The first three commands are standard modification commands for manipulating key-value pairs stored in memcached. They are both very easy to use and are all using the syntax shown in Listing 5:
Listing 5. Modify command syntax
Table 1 defines the parameters and usages of the memcached Modify command.
Table 1. Memcached Modifying command parameters
Parameters |
usage |
Key |
Key to find cached values |
Flags |
You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs |
Expiration time |
The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache |
bytes |
The byte points stored in the cache |
Value |
Stored values (always on the second row) |
Now, let's take a look at the actual use of these commands.
Set
set
The command is used to add a new key-value pair to the cache. If the key already exists, the previous value will be replaced.
Note the following interaction, which uses the set
command:
Set userId 0 0 512345STORED |
If set
a key-value pair is correctly set using a command, the server responds with the word STORED . This example adds a key-value pair to the cache with a key userId
value of 12345
. and set the expiration time to 0, which notifies memcached that you want to store this value in the cache until you delete it.
Add
The add
command adds a key-value pair to the cache only if the key does not exist in the cache. If the key already exists in the cache, the previous value will remain the same, and you will get a response of not_stored.
The following are add
standard interactions using commands:
Set userId 0 0 512345STOREDadd userid 0 0 555555not_storedadd companyid 0 0 3564STORED |
Replace
replace
the command replaces the key in the cache only if the key already exists. If the key does not exist in the cache, you will receive a not_stored response from the memcached server.
The following are replace
standard interactions using commands:
Replace accountId 0 0 567890not_storedset accountId 0 0 567890STOREDreplace accountId 0 0 555555STORED |
The last two basic commands are get
and delete
. These commands are fairly easy to understand and use a similar syntax, as follows:
Next look at the application of these commands.
Get
get
The command is used to retrieve the value associated with the previously added key-value pair. You will use to get
perform most of the retrieval operations.
The following are get
typical interactions using commands:
Set userid 0 0 512345STOREDget useridvalue userId 0 512345ENDget bobend |
As you can see, the get
command is quite simple. You use a key to invoke get
, and if the key exists in the cache, the corresponding value is returned. If it does not exist, nothing is returned.
Delete
The last basic command is delete
. delete
the command is used to delete any existing values in the memcached. You will use a key call delete
and if the key exists in the cache, delete the value. If it does not exist, a not_found message is returned.
Here is delete
the client server interaction using the command:
Set userId 0 0 598765STOREDdelete bobnot_founddelete useriddeletedget useridend |
Advanced memcached Client Commands
The two advanced commands that can be used in memcached are the gets
and cas
. gets
and cas
commands need to be used in combination. You will use these two commands to ensure that the existing name/value pairs are not set to the new value if the value has been updated. Let's take a look at these orders separately.
gets
gets
The function of the command is similar to the basic get
command. The difference between the two commands is that the gets
information returned is slightly more: The 64-bit integer value is much like the "version" identifier of a name/value pair.
Here is gets
the client server interaction using the command:
Consider get
gets
the differences between the commands. The gets
command returns an extra value-in this case, the integer value 4, which identifies the name/value pair. If another command is executed on this name/value pair set
, the gets
extra value returned will change to indicate that the name/value pair has been updated. Listing 6 shows an example:
Listing 6. Set Update Version indicator
Did you see gets
the value returned? It has been updated to 5. Each time you modify a name/value pair, the value changes.
CAs
cas
(check and set) is a very handy memcached command that sets the value of a name/value pair if the name/value pair has not been updated since you last executed it gets
. It uses set
syntax similar to the command, but includes an extra value: gets
the extra value returned.
Note the following cas
interactions using commands:
As you can see, I use the extra integer value 6来 to invoke gets
the command, and the operation runs in a very sequential order. Now let's take a look at the series of commands in Listing 7:
Listing 7. Commands that use the old version designator cas
Note that I did not use gets
the most recently returned integer value, and the cas
command returned a EXISTS value in order to fail. In essence, simultaneous use gets
and cas
commands prevent you from using name/value pairs that have been updated since the last read.
Cache Management Commands
The last two memcached commands are used to monitor and clean memcached instances. They are stats
and flush_all
commands.
Stats
stats
The command functions as its name: the current statistics of the memcached instance to which the dump is connected. In the following example, the Execute stats
command displays information about the current memcached instance:
Statsstat PID 63STAT Uptime 101758STAT time 1248643186STAT version 1.4.11STAT pointer_size 32STAT rusage_user 1.177192STAT Rusage_system 2.365370STAT curr_items 2STAT total_items 8STAT bytes 119STAT curr_connections 6STAT total_connections 7STA T connection_structures 7STAT cmd_get 12STAT cmd_set 12STAT get_hits 12STAT get_misses 0STAT evictions 0STAT bytes_read 47 1STAT bytes_written 535STAT limit_maxbytes 67108864STAT Threads 4END |
Most of the output here is very easy to understand. Later, when we discuss caching performance, I'll explain in detail what these values mean. For now, let's take a look at the output and then use the new key to run some set
commands and run stats
the command again, noticing what's changed.
Flush_all
flush_all
is the last command to be introduced. This simplest command is only used to clean up all name/value pairs in the cache. If you need to reset the cache to a clean state, it flush_all
can be a great use. Here is an flush_all
example of use:
Set userid 0 0 555555STOREDget useridvalue userId 0 555555endflush_allokget useridend |
Cache Performance
At the end of this article, I'll discuss how to use the Advanced memcached command to determine the performance of the cache. stats
commands are used to tune the use of the cache. The two most important statistics to note are et_hits and get_misses. These two values indicate the number of times the name/value pair was found (get_hits) and the number of name/value pairs that were not found (get_misses).
By combining these values, we can determine how the cache is utilized. When you start the cache for the first time, you can see that get_misses will naturally increase, but after a certain amount of usage, these get_misses values should gradually stabilize-this means that the cache is primarily used for common read operations. If you see get_misses continue to increase rapidly, and get_hits gradually become smooth, then you need to determine what the cached content is. You may have cached the wrong content.
Another way to determine cache efficiency is to view the cache hit ratio. The cache Hit ratio represents the get
number of executions and the get
percentage of missed times. To determine this percentage, you need to run the stats
command again, as shown in Listing 8:
Listing 8. Calculate cache Hit Ratio
Statsstat PID 6825STAT Uptime 540692STAT time 1249252262STAT version 1.2.6STAT pointer_size 32STAT rusage_user 0.056003STA T rusage_system 0.180011STAT curr_items 595STAT total_items 961STAT bytes 4587415STAT curr_connections 3STAT Total_connec tions 22STAT connection_structures 4STAT cmd_get 2688STAT cmd_set 961STAT get_hits 1908STAT get_misses 780STAT Evictions 0 STAT bytes_read 5770762STAT bytes_written 7421373STAT limit_maxbytes 536870912STAT Threads 1END |
Now, divide the value of get_hits by Cmd_gets. In this case, your hit rate is about 71%. Ideally, you might want to get a higher percentage-the higher the ratio, the better. Viewing statistics and measuring them from time to times can be a good way to determine the efficiency of your caching strategy.
Commands are often as follows:
Start/End
Memcached-d-M 10-u root-l 192.168.0.122-p 11200-c 256-p/tmp/memcached.pid
The-D option is to start a daemon,
-M is the amount of memory allocated to Memcache, in megabytes, 10MB
-U is the user running memcache, this is root
-L is the server IP address of the listener, if there are multiple addresses, this specifies the IP address of the server 192.168.0.122
-P is the port that sets Memcache listening, which is set to 12000, preferably more than 1024 ports
The-c option is the maximum number of concurrent connections, the default is 1024, where 256 is set according to the load on your server
-P is a PID file that is set to save Memcache
Kill ' Cat/tmp/memcached.pid '
Get Run status
Echo Stats | NC 192.168.1.123 11200
Watch "Echo Stats | NC 192.168.1.123 11200 "(real-time status)
Mac installation Memcache