Memcached as a high concurrent memory cached system is used by many large companies, has also been using memcached project has accumulated some relevant experience.
Install
The installation of the memcached is simple and is done in the following line of Ubuntu command.
Copy Code code as follows:
sudo apt-get install memcached
Start
When used, memcached needs to adjust the memory size and port number, and so on, the use of the start of the command on the line.
Copy Code code as follows:
Usr/local/bin/memcached-p 11211-m 64m-d
-p Specifies the port number
-m Specifies memory size
-D as Daemon started in the background
If you want to trouble shotting, then add-VV.
Test
Test is also very simple, the installation of the official website command on the line
Copy Code code as follows:
Inside you can get and set.
Use in Java
Because the project is Java, so the Java as an example to introduce the use of Java memcached.
Java under the memcached client a lot, here is a description of spymemcached
The use of spymemcached is simple, under the MAVEN project, we add the following dependencies.
Copy Code code as follows:
<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.9.1</version>
</dependency>
So we can operate on the memcached.
Copy Code code as follows:
public class Memcached {
private static memcachedclient memcached_client;
static {
try {
Memcached_client = new Memcachedclient (
Addrutil.getaddresses ("localhost1:11211 localhost2:11211"));
catch (Exception e) {
SYSTEM.ERR.PRINTLN ("Cannot init MEMCACHED MEMCACHED Client");
}
}
public static void Set (String key, String Value,integer expiretime) throws Exception {
Memcached_client.set (Key, expiretime, value);
}
public static string get (String key) throws Exception {
Object value = Memcached_client.get (key);
if (null!= value)
return value.tostring ();
return null;
}
}
Monitor
We can learn memcached's health by using the following simple commands, which can be implemented as a simple memcached detection shell.
Copy Code code as follows:
Watch "Echo Stats | NC 127.0.0.1 11211 "
Conclusion
Memcached is actually used and easy to use, each platform also has a corresponding client.
However, sometimes high concurrency may occur timeout, resulting in the buffer can not hit the situation, the specific reasons for further investigation.
Memcahed's author recently wrote in the Go language Groupcache is worth paying attention to.