Java use memcached simple tutorial _java

Source: Internet
Author: User

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:

telnet localhost 11211

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.

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.