Java programs use memcached configuration with example

Source: Internet
Author: User

Memcached as a very powerful distributed cache, often used in large-scale Internet applications, such as Sina Weibo, such as the use of memcached cache.

Memcached also often does data caching with MySQL combination.

For details, please refer to the official website: www.memcached.org


This is done by installing configuration memcached and using memcached to store and read cached data through a Java client.


Environment:

Ubuntu10.04

Java1.6


If Libevent is not installed, first install

(1) Installation libevent

(Apt-get install: Libevent does not seem to be 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 and install 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 the boot fails and the 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


(4) Download Java Client

Http://memcached.googlecode.com/files/memcached-1.6.0_beta1.tar.gz

Download and add to Java project as library


(5) Example

Write a simple example for caching an object (User)


import net.spy.memcached.MemcachedClient;


import java.io.IOException;

import java.io.Serializable;

import java.net.InetSocketAddress;


/*

* Spymemcached also comes with a few examples, we can see

* Http://code.google.com/p/spymemcached/wiki/Examples

*/


class User Implements serializable{//object must be serialized to be saved

public String userName;

public String password;

}


Public class memcachedtest {


public static void main (string[] args) throws IOException {

Memcachedclient C = new memcachedclient (new inetsocketaddress (

"192.168.1.106", 11211));


//Access a simple integer

//Store a value (async) for one hour

C.set ("Somekey", 3600, New Integer (4));

//Retrieve a value ( synchronously).

Object MyObject = C.get ("Somekey" );

integer result = (integer) myObject;

System. out. println (result);

//access to a serialized object

System. out. println ("time before Deposit:" + System.currenttimemillis ());

User User1 = new user ();

user1. userName = "Zhangsan";

user1 . Password = "Alongpasswordhere";

C.set ("user1", 3600, user1);

System. out. println ("time before Fetch:" + System.currenttimemillis ());

user MyUser1 = (user) (C.get ("user1") );

System. out. println (myUser1. UserName + "" + myUser1. password);

System. out. println ("Time after taking:" + System.currenttimemillis ());

}


}


Test results performed:

4

Time prior to deposit: 1310259282865

Time before fetch: 1310259282872

Zhangsan Alongpasswordhere

Time after Pickup: 1310259282877



The memcached and the client are installed on different machines during the test.

From the results, the access took 7 milliseconds and the read took 5 milliseconds. Is it fast or slow?


Another: MySQL Cluster 7.2 seems to have supported the memcached, do not need to configure memcached alone.


Resources:

Nice video about memcached.

Http://mysql.com/news-and-events/on-demand-webinars/display-od-158.html

Java programs use memcached configuration with example

Related Article

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.