Memcached Learning Notes--windows initial use (combined with Java)

Source: Internet
Author: User
Tags connection pooling memcached win32
Turn from: http://blog.sohu.com/people/!bWFiaXFpYW5nQHNvaHUuY29t/70343632.html

has been doing a project early design work, considering the late system expansion and performance problems also found a lot of solutions, one is to use the Database Cache tool memcached (of course, the tool is not only limited to the database cache). Let's briefly introduce what is memcached.

Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications. Memcached is developed by Danga Interactive to enhance the speed of livejournal.com access. LJ's dynamic page hits thousands of times per second, user 7 million. Memcached reduces database load significantly, allocates resources better, and accesses more quickly.

The internet Baidu a lot of things, almost all the same, and based on Java is very little, all only in the study of various other language classes and then try to do a simple operation in Java. First from the memcached, the latest version of Memcached is developed and designed in C language, it is said that the old version was developed in the Perl language, and it is an application software, as the server side of the cache server running on the server, You need to write clients in a specific language to communicate with them for caching and fetching data. Typically, we're running the memcached installation on a Web server, then by caching the required data, as far as I know, all the data cache settings and access operations, as well as the update of the data after the replacement operation of all the need for the program to do, rather than automatic (automatically do not know whether the success, hehe). The following is a practical example to apply memcached.

First go to http://danga.com/memcached/to download the Windows version of memcached and Java client jar packs, the latest version being memcached-1.2.1-win32.zip and Java_ Memcached-release_1.6.zip, respectively after decompression can be. First is to install the running Memcached Server, we will extract the Memcached-1.2.1-win32.zip, enter its directory, and then run the following command:

c:>memcached.exe-d Install
C:>memcached.exe-l 127.0.0.1-m 32-d Start

The first line is to install memcached to become a service, so that you can run properly, otherwise the run fails. The first line is to start memcached, as a test we will simply allocate 32M of memory, and then listen to the native port and to the daemon to run. Once executed, we can see the memcached.exe process in the Task Manager. OK, our server is already working properly, we will write Java client-side connection program below.

We copy the Java_memcached-release_1.6.jar files from the Java_memcached-release_1.6.zip unpacked directory to the Lib directory of the Java project, and then we write the code, For example, one of the application classes I provide is as follows:

Package Utils.cache;

Import Java.util.Date;

Import com.danga.MemCached.MemCachedClient;
Import Com.danga.MemCached.SockIOPool;


/**
* Use the memcached cache utility class.
*
* @author Ironwood Box
*
*/
public class MemCached
{
Create a unique instance of the global
protected static Memcachedclient MCC = new Memcachedclient ();

protected static MemCached MemCached = new MemCached ();

Set up a connection pool with the cache server
static {
Server list and its weights
String[] Servers = {"127.0.0.1:11211"};
Integer[] weights = {3};

Get instance object for Socke connection pool
Sockiopool pool = sockiopool.getinstance ();

Setting Up Server information
Pool.setservers (servers);
Pool.setweights (weights);

Set number of initial connections, minimum and maximum connections, and maximum processing time
Pool.setinitconn (5);
Pool.setminconn (5);
Pool.setmaxconn (250);
Pool.setmaxidle (1000 * 60 * 60 * 6);

Set the main thread's sleep time
Pool.setmaintsleep (30);

Set TCP parameters, connection timeout, etc.
Pool.setnagle (FALSE);
Pool.setsocketto (3000);
Pool.setsocketconnectto (0);

Initializing connection pooling
Pool.initialize ();

Compression settings, data that exceeds the specified size (k) is compressed
Mcc.setcompressenable (TRUE);
Mcc.setcompressthreshold (64 * 1024);
}

/**
* Protected construction method, no instantiation allowed.
*
*/
Protected MemCached ()
{

}

/**
* Get a unique instance.
* @return
*/
public static MemCached getinstance ()
{
return memCached;
}

/**
* Add a specified value to the cache.
* @param key
* @param value
* @return
*/
Public boolean Add (String key, Object value)
{
Return Mcc.add (key, value);
}

Public boolean Add (String key, Object value, Date expiry)
{
Return Mcc.add (key, value, expiry);
}

public boolean replace (String key, Object value)
{
Return Mcc.replace (key, value);
}

public boolean replace (String key, Object value, Date expiry)
{
Return Mcc.replace (key, value, expiry);
}

/**
* Gets the object based on the specified keyword.
* @param key
* @return
*/
Public Object get (String key)
{
return Mcc.get (key);
}

public static void Main (string[] args)
{
MemCached cache = Memcached.getinstance ();
Cache.Add ("Hello", 234);
System.out.print ("Get Value:" + cache.get ("Hello"));
}
}

So we can save a variable in a simple way, just like the one in the main method, and then take it out and see it, and we can look at the call to add and then get, and once we run it, the value of 234 has been stored in the memcached cache, After we have commented out the red line in the main method, we can still see that the value of get to is 234, that is, we already have the data in the cache.

We can operate on basic data, and for ordinary Pojo, if you want to store it, So for example, let it implement the Java.io.Serializable interface, because the memcached is a distributed caching server, multiple servers for data sharing needs to serialize the object, so must implement the interface, otherwise it will be an error. For example, we write a simple test bean as follows:

Class Tbean implements Java.io.Serializable
{
Private static final long serialversionuid = 1945562032261336919L;

private String name;

Public String GetName ()
{
return name;
}

public void SetName (String name)
{
THIS.name = name;
}
}

We then add the following lines of code to the Main method:

Tbean TB = new Tbean ();
Tb.setname ("ironwood box");
Cache.Add ("Bean", TB);
Tbean tb1 = (Tbean) cache.get ("Bean");
System.out.print ("name=" + tb1.getname ());
Tb1.setname ("Ironwood Box _ Modified");
TB1 = (Tbean) cache.get ("Bean");
System.out.print ("name=" + tb1.getname ());

We first put an instance of Tbean into the cache and then take it out and make a name change, and then we pick up the object, we look at its name, we find that the modified object is not the object in the cache, but a serialized instance object. So this means that the object retrieved from the cache is stored in a copy of the object, and the modifications to the object are not really modifying the data in the cache, but should be modified using the Replace method provided by it.

The above is I under Windows to memcached a little bit of learning and practice, in the future project development process will be more in-depth study and application of this caching tool, but also want to discuss with interested colleagues to learn the use of the tool ~ ~

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.