First, memcached installationDownload memcached zip package, cmd switch to the extracted directoryAdministratorIdentity run: Installation: memcached.exe-d Install start: memcached.exe-d start Close: memcached.exe-d Stop uninstall: memcached.exe-d Uninstall
third, the use of memcached
1, import a few jar packages under the Java_memcached-release_x.x.x.zip package (the specific package to download first, give the address later) 2, the Code
Package Com.liupeng.controller;import Java.util.date;import Com.danga.memcached.memcachedclient;import Com.danga.memcached.sockiopool;import Com.liupeng.domain.person;public class Testmemcached {public static Memcachedclient MCC = new Memcachedclient (); public static testmemcached TC = new testmemcached (); Set the connection pool with the cache server static{//server list and its weight string[] servers={"127.0.0.1:11211"}; Integer[] Weights={3}; Sockiopool Pool=sockiopool. GetInstance (); Pool.setservers (servers); Set server array information pool.setweights (weights); Sets the server weights array pool.setinitconn (5); Number of initial connections pool.setminconn (5); Minimum number of connections Pool.setmaxconn (250); Maximum number of connections Pool.setmaxidle (1000*60*60*6); Maximum processing time pool.setmaintsleep (30); Set the main thread's sleep time//Set TCP parameters, connection timeout, etc. pool.setnagle (false); Pool.setsocketto (3000); Pool.setsocketconnectto (0); Initializing the connection pool pool.iNitialize (); Compression setting, data that exceeds the specified size (in K) is compressed//mcc.setcompressenable (true); Mcc.setcompressthreshold (64*1024); }/** * Protected construction method, not allowed to be instantiated */protected testmemcached () {}/** * get unique instances * @return * * Pub Lic static testmemcached getinstance () {return TC; }/* Insert *//** * Add data to Cache * @param key * @param value * @return */public Boolean Add ( String Key,object value) {return Mcc.add (key, value); /** * Add data to the cache and set the timeout time * @param key * @param value * @param expiry * @return */Public Boo Lean Add (String key, Object value, Date expiry) {return Mcc.add (key, value, expiry); }/* Set *//** * Set a key value in the cache * @param key * @param value * @return */public boolean S ET (String key,object value) {return Mcc.set (key, value); /** * Set the value of a key in the cache and set the timeout time * @param key * @param value * @param exPiry * @return * */public Boolean set (String key, Object value, Date expiry) {return Mcc.set (key, value, expiry); }/* Replace *//** * Change the value of a key to a new value * @param key * @param value * @return */public Boolean Replace (String key, Object value) {return Mcc.replace (key, value); /** * Replace the value of a key with a new value and set the timeout time * @param key * @param value * @param expiry * @return */Public Boolean replace (String key, Object value, Date expiry) {return Mcc.replace (key, value, expiry); }/* Gets *//** * Gets the value of a key from the cache * @param key * @return */public Object get (String key) { return Mcc.get (key); } public static void Main (string[] args) {/********** simple test ***********/testmemcached TM = Testmemcache D. getinstance (); Tm.add ("Hello", "1234", new Date (new Date (). GetTime () + 10000)),//10 second expires while (true) {try { Thread. SLeep (1000); } catch (Exception e) {e.printstacktrace (); } System. Out.println (Tm.get ("Hello")); }/*******java Bean Test ********///entity class must be implemented serializable interface person person = new Person ("Liupeng" ); testmemcached TM = Testmemcached.getinstance (); Tm.add ("bean", person); Person P = (person) tm.get ("Bean"); System. Out.println (P.getname ()); Modifying the name does not affect the cache median value p.setname ("Xiaoliu"); p = (person) tm.get ("Bean"); System. Out.println (P.getname ()); P.setname ("Xiaoliu"); Tm.replace ("Bean", p); System. Out.println (P.getname ()); } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Memcached Java Usage