Implementing caching using Memcached

Source: Internet
Author: User
Tags memcached value store

What is memcached

Memcached is a free and open source, high performance, distributed memory object caching system.

Memcached is a software developed by Brad Fitzpatric, a Danga interactive company in LiveJournal. It has become an important factor in improving Web application extensibility in many services such as Mixi, Hatena, Facebook, Vox, and LiveJournal.

Memcached is a memory-based Key-value store used to store arbitrary data (strings, objects) of small chunks. This data can be a result of database calls, API calls, or page rendering.

Memcached is simple and powerful. Its simple design facilitates rapid development, reduces the difficulty of development, and solves many problems of large data volume caching. Its API is compatible with most popular development languages.

Essentially, it is a concise key-value storage system.

The general purpose is to reduce the number of database accesses by caching database query results to improve the speed and scalability of dynamic Web applications.

Install memcached under Windows

1. Download server memcached software

32bit: Download memcached-win32-1.4.4-14.zip (directly below) contains 6 files, put the extracted folder in any place (for example: D:\memcached).
Memcached-win32-1.4.4-14.zip download page: http://blog.couchbase.com/memcached-144-windows-32-bit-binary-now-available

2. Run cmd.exe as Administrator and go to the folder where Memcached is located

Like what:
CD D:\memcached
Note: If you do not run as an administrator, you will get an error "failed to install service or service already installed"

3, installation memcached

d:\memcached memcached.exe-d Install
(After the screen does not have any prompts, you can again [Control Panel]->[management tool]->[service] to see memcached)

4. Start memcached (1) Start Method 1:

D:\memcached> memcached.exe-d Start
After the screen does not have any prompts, but in the task Manager check "Show all user Processes", you can see that the Memcached.exe process is running
By default port 11211, external access needs to open the port, otherwise the connection cannot be successful.

(2) Start Method 2:

[Control Panel]->[management Tool]->[Service] Find the memcached service, double-click it to open as

Startup type: Select "Auto"
Service Status: Click "Start" below
Note: There are some differences between the path of the executable and the path I mentioned above can be ignored

5. memcached Basic Parameter setting

-P Listening Port
-L connected IP address, default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown Close the running memcached service
-D Install memcached service
-d Uninstall Uninstall memcached service
-U Run as (only valid when running as root)
-m maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-c Maximum number of simultaneous connections, default is 1024
-F Block size growth factor, default is 1.25
-N Minimum allocated space, key+value+flags default is 48
-H Display Help
When setting parameters, you need to stop memcached and then use the command line settings, such as:
D:\memcached> memcached.exe-m 1-d Start

6. Stop memcached

D:\memcached> memcached.exe-d Stop

7, uninstall memcached

d:\memcached> memcached.exe-d Uninstall

8. Modify Parameters

Windows needs to be set by modifying registry information, open the registry, find
Hkey_local_machine\system\currentcontrolset\services\memcached
There is a "ImagePath" entry in which the value is:
"D:\memcached\memcached.exe"-D runservice
Add "-M 1024-c 2048-p 11210" at the back. Wait a minute. Effective after restarting service

Working with the Java language memcached

Comparison of three APIs

(1) The Memcached Java Client API, which was launched earlier, is widely used and stable.

(2) A simple, asynchronous, single-threaded memcached client written in Java. Support asynchronous, single-threaded memcached client, using the java1.5 version of concurrent and NIO, access speed will be higher than the former, but the stability is not good, test often reported timeout and other related anomalies.

(3) xmemcached is also a Java NIO-based client, and Java NiO has the advantage of being more efficient (especially at high concurrency) and less resource-intensive than the traditional blocking IO model. Traditional blocking IO in order to increase efficiency, a certain number of connections need to be created to form a connection pool, and NIO requires only one connection (of course, NIO can also be pooled), which in contrast reduces the overhead of thread creation and switching, which is particularly noticeable in high concurrency. So xmemcached and spymemcached in the performance are very good, in some aspects (stored data relatively small case) xmemcached than spymemcached performance is more excellent, specifically can see this Java Memcached Clients Benchmark.

Example code for using xmemcached

1. Add Maven Dependencies

 <Dependency>          <groupId>Com.googlecode.xmemcached</groupId>          <Artifactid>Xmemcached</Artifactid>          <version>2.4.0</version>      </Dependency>

2. Sample Code

Memcachedclientbuilder Memcachedclientbuilder=NewXmemcachedclientbuilder ("localhost:11211"); Memcachedclient Client=Memcachedclientbuilder.build (); //Deposit A string, the first parameter is key, the second argument is the expiration time, in seconds, and 0 for a month//The third parameter is a specific valueClient.add ("Stringkey", 0, "This is my string"); //gets a string value from the memcached server based on a keyString s = client.get ("Stringkey");        System.out.println (s); //deposit to a list collectionList List =NewArrayList (); List.add ("AA"); List.add ("BB"); Client.add ("List", 0, list); //gets a list based on keyList = Client.get ("list");        SYSTEM.OUT.PRINTLN (list); //The object must be serializable if it needs to be stored in an object.//in Java, by implementing interface java.io.Serializable to represent the class serializable//class Student implements Java.io.SerializableStudent stu =NewStudent (); Stu.setid (12); Stu.setname ("Zhang San"); Client.add ("Obj", 0, Stu); System.out.println (Client.get ("obj"));

Implementing caching using Memcached

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.