Use of the memcached cache framework

Source: Internet
Author: User

Memcach What is Memcache

    1. What is Memcache?

Memcache is a high-performance distributed memory object caching system that can be used to store data in a variety of formats, including images, videos, files, and database retrieval results, by maintaining a unified, huge hash table in memory. The simple thing is to call the data into memory and then read it from memory, which greatly improves the reading speed. Memcached is a high-concurrency memory-key-value-to-cache system that accelerates Web application responsiveness by caching database query results, content, and other time-consuming calculations into system memory.

    1. The difference between memcache and memcached?
      In fact Memcache is the name of this project, and memcached is its server side of the main program file name, know what I mean it. One is the project name, one is the main program file name, on the internet to see a lot of people do not understand, so mixed.
    2. Installation of memcached

Prior to version 1.4.5, memcached could be installed as a service, but later versions of the feature were removed. Therefore, the installation of memcached can be divided into two categories, the first is the previous version of 1.4.5, and the other is the version after 1.4.5.

1) install memcached < 1.4.5:

    1. Unzip the downloaded file to any directory.
    2. 2.1.4.5 the previous version of memcached will be installed as a service to administrator open the console and run the following command:

C:\memcached\memcached.exe-dinstall

* Note Replace the path C:\memcached\memcached.exe with your local installation path.

    1. Then use the following command to start or stop the memcached service:

C:\memcached\memcached.exe-dstart

C:\memcached\memcached.exe-dstop

    1. For example, if you want to increase the maximum memory limit used by memcached, you can modify the value of ImagePath:

"C:\memcached\memcached.exe"-D runservice-m 512

* In addition to the parameter '-M 512 ', you can also use other parameters. All available parameters can be viewed with "c:\memcached\memcached.exe-h".

    1. If you want to uninstall the Memcached service, you can use the following command:

C:\memcached\memcached.exe-duninstall

2) install memcached >= 1.4.5

    1. Unzip the downloaded file to any directory.
    2. 1.4.5 later versions of memcached cannot be run as Windows services, you can start memcache on the command line. Open cmd as an administrator and enter the following command:

Memcached-x86–p 11211–m 128–VV

The memcached startup options are as follows:

Options

Description

-P

TCP port used, default is 11211

-M

Maximum memory size defaults to 64m

-vv

Start with very verbose mode, debug information and error output to console

-D

Start in the background as a daemon

    1. How to use Memcache in Java
      1. To add a jar package:

Commons-pool-1.5.6.jar,

Java_memcached-release_2.6.6.jar,

Slf4j-api-1.6.1.jar,

    1. Add Auxiliary class Memcachedutil.java

Import Java.util.Date;

Import com.danga.MemCached.MemCachedClient;

Import Com.danga.MemCached.SockIOPool;

public class Memcachedutil {

/**

* Memcached Client Single case

*/

private static Memcachedclient cachedclient = new Memcachedclient ();

/**

* Initialize Connection Pool

*/

static {

Get an instance of a connection pool

Sockiopool pool = sockiopool.getinstance ();

Server list and its weights

String[] Servers = {"127.0.0.1:11211"};

Integer[] weights = {3};

Setting Up Server information

Pool.setservers (servers);

Pool.setweights (weights);

Set number of initial connections, minimum number of connections, maximum connections, maximum processing time

Pool.setinitconn (10);

Pool.setminconn (10);

Pool.setmaxconn (1000);

Pool.setmaxidle (1000*60*60);

Set the sleep time of the connection pool daemon thread

Pool.setmaintsleep (60);

Set TCP parameters, connection timed out

Pool.setnagle (FALSE);

Pool.setsocketto (60);

Pool.setsocketconnectto (0);

Initialize and start the connection pool

Pool.initialize ();

Compression settings, compressed beyond the specified size

Cachedclient.setcompressenable (TRUE);

Cachedclient.setcompressthreshold (1024*1024);

}

Private Memcachedutil () {

}

public static Boolean Add (String key, Object value) {

Return Cachedclient.add (key, value);

}

public static Boolean Add (String key, Object value, Date expiredate) {

Return Cachedclient.add (key, value, expiredate);

}

public static Boolean Add (String key, Object value, Integer millseconds) {

Return Cachedclient.add (key, value, new Date (new Date (). GetTime () +millseconds));

}

public static Boolean put (String key, Object value) {

Return Cachedclient.set (key, value);

}

public static Boolean put (String key, Object value, Date expiredate) {

Return Cachedclient.set (key, value, expiredate);

}

public static Boolean put (String key, Object value, Integer millseconds) {

Return Cachedclient.set (key, value, new Date (new Date (). GetTime () +millseconds));

}

public static Boolean replace (String key, Object value) {

Return Cachedclient.replace (key, value);

}

public static Boolean replace (String key, Object value, Date expiredate) {

Return Cachedclient.replace (key, value, expiredate);

}

public static Boolean replace (String key, Object value, Integer millseconds) {

Return Cachedclient.replace (key, value, new Date (new Date (). GetTime () +millseconds));

}

public static Object get (String key) {

return Cachedclient.get (key);

}

public static Object Delete (String key) {

return Cachedclient.delete (key);

}

}

    1. Note that the class that is added to the cache must implement the interface serializable.

Use of the memcached cache framework

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.