Memcache Client Utils Class

Source: Internet
Author: User

0. Personal tagsMy csdn blog address:Http://blog.csdn.net/caicongyang
1. Dependent jar Packages

Commons-pool-1.5.6.jar

Slf4j-api-1.6.1.jar

Java_memcached-release_2.6.6.jar

Slf4j-simple-1.6.1.jar

2.dangamemcacheclient.java
Package Com.ccy.comm.utils;import Java.io.unsupportedencodingexception;import Java.net.urldecoder;import Java.util.arraylist;import java.util.date;import java.util.iterator;import Java.util.list;import java.util.Map; Import Java.util.concurrent.locks.reentrantlock;import Com.danga.memcached.memcachedclient;import com.danga.memcached.sockiopool;/** * <p> * Title:DangaMemCacheClient.java * Package * </p> * <p> * Description:memcacheclient Utils class * <p> * @author tom.cai * @created 2015-7-16 pm 3:49:24 * @version V1.0 * */pub Lic class Dangamemcacheclient {private static dangamemcacheclient instance = null;private static Reentrantlock lock = new Reentrantlock ();p rivate memcachedclient mc;private static int exptime = 3600 * 30;private static String address= "192. 168.253.132 ";p rivate static String port=" 12000 ";p ublic static Dangamemcacheclient getinstance () {if (instance = = null) {L Ock.lock (); try {if (instance = = null) {instance = new Dangamemcacheclient ();}} catch (Exception e) {e.printstacktrace ();} finally {Lock.unlock ();}} return instance;} Private dangamemcacheclient () {try {mc = new memcachedclient (); String[] Servers = {address+ ":" +port};integer[] weights = {10}; Sockiopool pool = sockiopool.getinstance ();p ool.setservers (servers);p ool.setweights (weights);p ool.setinitconn (5); Pool.setminconn (5);p Ool.setmaxconn (+);p ool.setmaxidle (exptime);p ool.setmaintsleep (+);p Ool.setnagle (false); Pool.setsocketto (+);p ool.setsocketconnectto (0);p ool.initialize (); catch (Exception e) {e.printstacktrace ();}} Public Object GetObject (String key) {return mc.get (key);} public boolean setobject (String key, Object value) {return Mc.set (key, value);} public boolean setobject (String key, Object value, long time) {return Mc.set (key, value, new Date (Time));} public boolean removeobject (String key) {return mc.delete (key);} /** * Loop to get all keys in all memcache * @return */public list<string> getkeys () {list<string> keylist = new Arraylist&lt ; String> (); Map Slabs = Mc.statsitems (); Iterator itemsitr = Slabs.keyset (). Iterator (); while (Itemsitr.hasnext ()) {String ServerInfo1 = Itemsitr.next (). toString ();//Get the various status of this server [Itemname:number:field=value]map itemnames = (Map) slabs.get ( SERVERINFO1) Iterator Itemnameitr = Itemnames.keyset (). Iterator ();//To Loop while (Itemnameitr.hasnext ()) in Status key value { String itemname = Itemnameitr.next (). toString ();//Disassemble status linked fields bit//itematt[0] = itemname//itematt[1] = Cachedump's parameters//ItemA TT[2] = Field:number or agestring[] Itematt = Itemname.split (":"), if (Itematt[2].startswith ("number")) {Map Chchedump = MC . Statscachedump (Integer.parseint (itematt[1]), 0); Iterator itr = Chchedump.keyset (). Iterator (); int i = 0;while ( Itr.hasnext ()) {String ServerInfo2 = Itr.next (). toString (); Map items = (map) chchedump.get (ServerInfo2), Iterator keyitr = Items.keyset (). Iterator (); while (Keyitr.hasnext ()) { String key = Keyitr.next (). toString (); String Memkey = key;i++;try {key = Urldecoder.decode (key, "UTF-8"); String value = ((String) Items.get (Memkey)); value = Value.substring (Value.indexof (";") + 2, Value.indexof ("s"));D ate Date = new Date ();d Ate.sett IME (long.valueof (value + ")"); Keylist.add (key);} catch (Exception ex) {}}}}}}return keylist;} public static void Main (string[] args) throws Unsupportedencodingexception {Dangamemcacheclient DMCC = Dangamemcacheclient.getinstance ();d mcc.setobject ("Ccy0", "Test0");d mcc.setobject ("Ccy1", "test1"); System.out.println (String) dmcc.getobject ("Ccy0"); System.out.println (String) dmcc.getobject ("Ccy1"); list<string> list = Dmcc.getkeys (); for (String key:list) {System.out.println (key);}}}

3. Note that I wrote it as a tool to apply to only the cache does not save the database, of course, can write a service for spring to call!


My csdn blog address:Http://blog.csdn.net/caicongyang



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Memcache Client Utils Class

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.