Ubuntu memcached Installation and Java memcached using demo

Source: Internet
Author: User
Tags memcached

If Libevent is not installed, first install

(1) Installation libevent

(Apt-get install: Libevent seems not to have been recognized)

Download http://www.monkey.org/~provos/libevent-2.0.12-stable.tar.gz

Libevent-2.0.12-stable$configure

Libevent-2.0.12-stable$make

Libevent-2.0.12-stable$sudo make Install

(2) Installation memcached

Download Installation memcached1.4.5

Http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz

Memcached-1.4.5$./configure

Memcached-1.4.5$make

Memcached-1.4.5$sudo make Install

If startup fails and libevent is not found, connect

/usr/lib$ sudo ln-s/usr/local/lib/libevent-2.0.so.5 libevent-2.0.so.5

(3) Start

$memcached-D-m 128-p 11111-u Root

Here you need to explain the startup parameters for the memcached service:
-P Listening Port
The IP address of the-l connection, the default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown shut down the running memcached service
-D Install installation memcached service
-d Uninstall Uninstall memcached service
-U Run as (only valid when run as root)
-m maximum memory usage, in MB. Default 64MB
-Returns an error when M memory is exhausted instead of deleting the item
-C Maximum Simultaneous connection number, default is 1024
-F Block size growth factor, default is 1.25-n minimum allocation space, key+value+flags default is 48
-H Display Help 2 install Memcache client


4. Examples

Download jar package, introduce project: Http://cloud.github.com/downloads/gwhalin/Memcached-Java-Client/java_memcached-release_2.5.3.jar

Import Java.util.Date;
Import com.danga.MemCached.MemCachedClient;

Import Com.danga.MemCached.SockIOPool;

	public class Memcachedtest {//Create a unique instance of the global protected static memcachedclient MCC = new Memcachedclient ();

	protected static Memcachedtest memCached = new Memcachedtest ();
		Set the connection pool static {//server list and its weight string[] servers = {"127.0.0.1:11111"} to the cache server;

		Integer[] weights = {3};

		Gets the instance object of the Socke connection pool Sockiopool pool = sockiopool.getinstance ();
		Set up server information pool.setservers (servers);

		Pool.setweights (weights);
		Set initial connection number, minimum and maximum number of connections and maximum processing time pool.setinitconn (5);
		Pool.setminconn (5);
		Pool.setmaxconn (250);

		Pool.setmaxidle (1000 * 60 * 60 * 6);

		Set the main thread sleep time Pool.setmaintsleep (30);
		Set TCP parameters, connection timeout, etc Pool.setnagle (false);
		Pool.setsocketto (3000);

		Pool.setsocketconnectto (0);

		Initialize Connection pool pool.initialize ();
		Compression settings, data that exceeds the specified size (k) is compressed mcc.setcompressenable (true);
	Mcc.setcompressthreshold (64 * 1024); /** * Protection-type construction method, does not allowinstantiated.
	 * * */protected Memcachedtest () {}/** * gets a unique instance.
	* @return */public static memcachedtest getinstance () {return memCached;
	 /** * Adds a specified value to the cache. * @param key * @param value * @return/public boolean Add (String key, Object value) {return Mcc.add key, Valu
	e);
	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);
	Public Boolean exists (String key) {return mcc.keyexists (key);
	 /** * Gets objects based on the specified keyword.
	 * @param key * @return/Public Object get (String key) {return mcc.get (key);
		 public static void Main (string[] args) {memcachedtest cache = memcachedtest.getinstance ();
		 Long Startdate=system.currenttimemillis (); for (int i = 0; i < i++) {Cache.Add("Test" +i, "I Love You China" +i);
		 Long Enddate=system.currenttimemillis ();
		 Long nowdate= (enddate-startdate)/1000;
		 System.out.println (nowdate);
	 System.out.print ("Get Value:" + cache.get ("test99")); }
}



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.