1 Importjava.io.Serializable;2 ImportJava.text.DateFormat;3 Importjava.util.Date;4 ImportJava.util.Map;5 6 Importcom.danga.MemCached.MemCachedClient;7 ImportCom.pt.util.memcached.MemcachedTool;8 9 Ten Public classtestmemcached { One A /** - * @paramargs - */ the Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub -memcachedclient MC =memcachedtool.getinstance (); - //(1000 * 129 is the difference between local time and server-side time) +Date faildate =NewDate (System.currenttimemillis ()-1000 * 129 + 10000); - System.out.println (faildate); +Mc.set ("C", "V", faildate);//If you have the same key value, replace the original value with the expiration time (whichever is the server time) ABoolean add = Mc.add ("b", "110");//returns False if the same key value exists atString str = (string) mc.get ("C"); -String[] keys = {"A", "B"}; -Map STRs = Mc.getmulti (keys);//getting multiple values returns the result as an array -System.out.println ("Get value from memcached:" +str); -Mc.delete ("C");//Delete key - inSYSTEM.OUT.PRINTLN ("Test Add Method:" +add); -System.out.println ("Get params:" +STRs); to + //writing to an object -User Men =Newuser (); theMen.setid (1907); *Men.setname ("Pan Teng"); $ BooleanSetobj = Mc.set ("User", men);Panax NotoginsengUser Getmen = (user) mc.get ("User"); - System.out.println (getmen); the } + A } the + classUserImplementsserializable{ - String name; $ intID; $ PublicString GetName () { - returnname; - } the Public voidsetName (String name) { - This. Name =name;Wuyi } the Public intgetId () { - returnID; Wu } - Public voidSetId (intID) { About This. ID =ID; $ } - @Override - PublicString toString () { - //TODO auto-generated Method Stub A return"Name:" + name + "ID:" +ID; + } the -}
Testmain
1 Packagecom.pt.util.memcached;2 3 Importcom.danga.MemCached.MemCachedClient;4 ImportCom.danga.MemCached.SockIOPool;5 6 Public classMemcachedtool {7 Private StaticMemcachedclient memcacheclient =NULL;8 9 PrivateMemcachedtool () {Ten One } A - Public Staticmemcachedclient getinstance () { - if(Memcacheclient = =NULL){ theMemcacheclient =Newmemcachedclient (); -Memcacheclient.setdefaultencoding ("UTF-8");//encoding format for write cache - - } + returnmemcacheclient; - } + A Static{ atString[] Serversarray = {"192.168.65.221:13220"}; -integer[] Weight = {1}; -Sockiopool Connpool =sockiopool.getinstance (); -Connpool.setservers (Serversarray);//setting up the memcached server -Connpool.setweights (weight);//set individual server storage weights -Connpool.setminconn (3);//set the minimum number of connections for a connection pool inConnpool.setinitconn (3);//Initialize the number of available connections -Connpool.setmaxidle (10000);//Maximum available connection pool wait time toConnpool.setsocketto (10000);//read Wait timeout value +Connpool.setsocketconnectto (10000);//connection Wait Timeout value - //heartbeat detection, when set to true, each communication first detects whether the connection is available to increase IO and network overhead, and is generally set to False by default . theConnpool.setalivecheck (false); * /** $ * Alg=0 uses String.hashcode () to obtain hash code, which relies on the JDK and may not be compatible with other clients, it is recommended not to usePanax Notoginseng * Alg=1 uses original compatible hash algorithm, compatible with other clients - * alg=2 uses CRC32 compatible hash algorithm, compatible with other clients, performance is better than original algorithm the * alg=3 using MD5 hash algorithm + * When using the first three hash algorithms, find the cache server using the remainder method. Use the consistent method when finding the cache service using the last hash algorithm. A **/ theConnpool.sethashingalg (3); + //the connection continues to be available after setting the server down or when the connection has changed from an outage to a recovery -Connpool.setfailback (true); $ //Start Pool $ connpool.initialize (); - } -}
Memcachetool
Need to introduce: Java-memcached-release_2.5.2.jar package
Java memcache applications