CentOS installation memcached and Java operating examples

Source: Internet
Author: User
Tags object object

Prepare to build a memcached cache cluster. Here's a simple record of the building process.

Since the memcached installation requires the use of the Libevent class library, the Libevent is installed first http://www.monkey.org/~provos/libevent/

(a) I downloaded the libevent-1.4.14b-stable.tar.gz

2| entering the file
CD libevent-1.4.14b-stable
3. Compile the installation ( install to/usr/local/lib/directory by default
./configure Make make Install

(ii) Installation of memcached

Installing memcached
memcached Download URL: http://www.danga.com/memcached/download.bml


1. Unzip the tar XZFV memcached-1.2.6.tar.gz 2. Go to memcached-1.2.6 directory CD memcached-1.2.6    3. Compile, install    ./configure--prefix=/local/memcached    make    make Install

This may prompt you for permission issues when you install it, so first
chmod +x Configure

After the installation is complete, the bin and share directories will appear in the/local/memcached

Make Bin directory, start memcache
Here's how:
./memcached-d-U nobody-m 127.0.0.1-p 11211
At this point, an exception is reported
error while loading shared Libraries:libevent-1.4.so.2:cannot open Shared object file:no such file or DIRECTORY

The reason is that the Libevent-1.4.so.2 class library is not found and the workaround is as follows:


Use Ld_debug=help./memcached-v to determine which class library path to load by using the following method:
Ld_debug=libs./memcached-v 2>&1 >/dev/null | Less

The system will display:

Linux:/local/memcached/bin # ld_debug=libs./memcached-v 2>&1 >/dev/null | Less 20421:find library=libevent-1.4.so.2; Searching 20421:search Cache=/etc/ld.so.cache 20421:search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/ tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/       Tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (System search path) 20421: Trying file=/lib/tls/i686/sse2/libevent-1.4.so.2 20421:trying file=/lib/tls/i686/libevent-1.4.so.2 20421       : Trying file=/lib/tls/sse2/libevent-1.4.so.2 20421:trying file=/lib/tls/libevent-1.4.so.2 20421:       Trying file=/lib/i686/sse2/libevent-1.4.so.2 20421:trying file=/lib/i686/libevent-1.4.so.2 20421: Trying file=/lib/sse2/libevent-1.4.so.2 20421:trying file=/lib/libevent-1.4.so.2 20421:trying file=/ Usr/lib/tls/i686/sse2/libevent-1.4.so.2 20421:trying file=/usr/lib/tls/i686/libevent-1.4.so.2 20421:trying file=/usr/lib/ Tls/sse2/libevent-1.4.so.2 20421:trying file=/usr/lib/tls/libevent-1.4.so.2 20421:trying file=/usr/l Ib/i686/sse2/libevent-1.4.so.2 20421:trying file=/usr/lib/i686/libevent-1.4.so.2 20421:trying file=/ Usr/lib/sse2/libevent-1.4.so.2 20421:trying file=/usr/lib/libevent-1.4.so.2 20421:./memcached:error WH Ile loading shared Libraries:libevent-1.4.so.2:cannot Open Shared object file:no such file or directory

We see, memcached will go to a lot of places to find, so according to other request, we just need to build a soft link, designated to our installed class library can be as follows: Ln-s/usr/local/lib/libevent-1.4.so.2/lib/ Libevent-1.4.so.2 can now start memcached normally./memcached-d-u nobody-m 127.0.0.1-p 11211
memcache Start parameter description:
The installation is ready to use memcached, and below I use the simplest official method Java_memcached_releas

can go here to download https://github.com/gwhalin/Memcached-Java-Client/downloads

Tool class

Package Org.memcached.xiezhaodong.test;import Java.util.date;import Com.danga.memcached.memcachedclient;import com.danga.memcached.sockiopool;/** * * <pre><b> Function Description: </b>memcached Tool class * * @author Xie) <br> * * & lt;b> Modify History:</b> (modify person, modify time, change reason/content) * * </pre> */public Final class Memcachedutil {/** * <b> Constructors: Tool class, Disallow instantiation </b> * */Private Memcachedutil () {}//create unique instance of global private static MEMCACHEDCL        Ient MCC = new Memcachedclient ();        /** * Self instance */private static Memcachedutil Memcachedutil = new Memcachedutil (); Set the connection pool with the cache server static {//server list and its weight string[] servers = {"192.168.80.100:11211"};//IP address and port number//                Weight integer[] weights = {3};                Gets the instance object of the socket connection pool Sockiopool pool = sockiopool.getinstance ();        Set Server information pool.setservers (servers);                Pool.setweights (weights);      Set the 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);                Initialize the connection pool pool.initialize ();        Compression setting, data that exceeds the specified size (in K) will be compressed//mcc.setcompressenable (TRUE);                Mcc.setcompressthreshold (64 * 1024);     Mcc.setprimitiveasstring (TRUE);//Set Serialization}/** * * <pre><b> function Description:</b> gets a unique instance. * @author: Xiezhaodong * <b> Date Created: </b>2012-4-25 morning 10:57:41 * * @return * * &LT;B&GT Modify history:</b> (modify person, modify time, change reason/content) * * </pre> */public static Memcachedutil getinstance () {R    Eturn Memcachedutil; }/** * * <pre><b> function Description:</b> New CACHE Data * * @author: Xiezhaodong * <b> created: </b>2012-4-25 Morning 10:55:15 * * @param key Cached Key * @param value Cached values * @return operation result * * <b> Modify History:</b> (modify person, modify time, change reason/content) * * </pre> * */public Boolean Add (String key        , Object value) {//will not be cached in return Mcc.add (key, value);    Return Mcc.set (key, value); }/** * * <pre><b> function Description:</b> New CACHE Data * * @author: Xiezhaodong * <b> Creation Date Period: </b>2012-4-25 a.m. 10:56:15 * * @param key Cached Key * @param value Cached values * @param expiry cache Expiration Time * @return Operation Result * * <b> Modify history:</b> (modifier, modified time, reason/content) * * </pre> */public Boolean Add (        String key, Object value, Date expiry) {//will not be cached in return Mcc.add (key, value, expiry);    Return Mcc.set (key, value, expiry); }/** * <pre><b> function Description:</b> Replace existing cache data * * @author: Xiezhaodong * <b> Date Created: &lt ;/b>2012-4-25 Morning 10:55:34 * * @param key to set the object's key * @return object to set the value of the objects * @return if the replacement succeeds * * &LT;B&G        t; Modify history:</b> (modify person, modify time, modify reason/content) * * </pre> * * Public boolean replace (String key, Object value) {    Return Mcc.replace (key, value); }/** * * <pre><b> function Description:</b> Replace existing cache data * * @author: Xiezhaodong * <b> Create     Date: </b>2012-4-25 am 10:43:17 * * @param key to set the object's key * @return object to set the value of objects * @param expiry expiration Time * @return Replace Success * * <b> Modify History:</b> (modify person, modify time, change reason/content) * * </pre> */Public Boolea    N Replace (String key, Object value, Date expiry) {return Mcc.replace (key, value, expiry); /** * * <pre><b> function Description:</b> gets the object according to the specified keyword * * @author: Xiezhaodong * <b&gt ; Date Created: </b>2012-4-25 am 10:42:49 * * @param key gets the object's key * @return Object Object Value * * <b> history: & Lt/b> (modifier, modification time, reason/content) * * </pre> * * Public Object get (String key) {return mcc.get (key); }}

object, you must implement the serialization interface

Package Org.memcached.xiezhaodong.test;import java.io.serializable;/** * * <pre><b> Function Description:</b> employee class, Must be serialized, otherwise the cache operation will be an error * * @author:xiezhaodong<br> * * <b> Modify the history:</b> (modifier, modified time, change reason/content) * * </pre> */p Ublic class Employee implements Serializable {/** * serialversionuid * * Private static final long seria        Lversionuid = -6487397580836471048l;        /** * Employee Name */private String empname;        /** * Department Name */private String deptname;        /** * Company Name * * Private String companyName;  /** * * <b> Constructor:</b> * */Public Employee () {}/** * Access method for the    EmpName Property * * @return The EmpName * */Public String Getempname () {return empname; }/** * Sets the value of EmpName the property * * @param empname the EmpName to set */public    void Setempname (String empname) {empname = EmpName;   }     /** * Access method for the Deptname property * * @return The Deptname */public String Getdeptna    Me () {return deptname; }/** * Sets the value of Deptname the property * * @param deptname the Deptname to set */Publ    IC void Setdeptname (String deptname) {this.deptname = Deptname;  /** * Access method for the CompanyName property * * @return The CompanyName */public String    Getcompanyname () {return companyName; }/** * Sets the value of CompanyName the property * * @param companyName the companyName to set *    /public void Setcompanyname (String companyName) {this.companyname = CompanyName; }}


Test class

Package Org.memcached.xiezhaodong.test;import Com.danga.memcached.memcachedclient;public class MemcachedUtilTest { public static void Main (string[] args) {//add ();p rint ();} private static void print () {Memcachedutil cache = memcachedutil.getinstance (); Employee emp= (employee) cache.get ("emp"); System.out.println ("Name:" +emp.getcompanyname ()); System.out.println ("DEP:" +emp.getdeptname ()); SYSTEM.OUT.PRINTLN ("emp" +emp.getempname ());} private static void Add () {Memcachedutil cache = memcachedutil.getinstance (); Employee EMP = new Employee (), Emp.setcompanyname ("Kevin's Company"), Emp.setdeptname ("R/R Dept"); Emp.setempname (" Kevin "); Cache.Add (" EMP ", EMP);}}


A simple stand-alone environment has been built. There are some other plugins, such as spring integrated memcached plug-ins and so on, you can use it later!

Reprint Please specify http://blog.csdn.net/a837199685


CentOS installation memcached and Java operating examples

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.