Spring Integrated memcached Three ways (i)

Source: Internet
Author: User
Tags socket blocking

Reprint: http://blog.csdn.net/u013725455/article/details/52102170

Memcached client currently has 3 types:
Memcached Client for Java
Spymemcached
Xmemcached

These three types of clients have been controversial:
Memcached Client for Java is more stable, earlier and wider than spymemcached;
Spymemcached is more efficient than Memcached Client for Java;
Xmemcached is more effective than spymemcache concurrency.

Mode one: Memcached Client for Java
Jar Pack Preparation: Java_memcached-release_2.6.6.jar

Memcached.properties configuration file: Configure server address, number of connections, time-out, etc.

###################### #Memcached配置 #######################  #服务器地址  memcached.server1= 127.0.0.1memcached.port1=11211#memcached.server=127.0.0.1:11211# the number of connections established for each server during initialization  memcached.initconn=20  #每个服务器建立最小的连接数  memcached.minconn=10  #每个服务器建立最大的连接数  memcached.maxconn=50  #自查线程周期进行工作, Its sleep time  memcached.maintsleep=3000  #Socket的参数, if true when writing data is not buffered, immediately send out  memcached.nagle=false  # Timeout time for socket blocking read Data  memcached.socketto=3000  # #pool. setservers (servers);  # #pool. setweights (weights);  # #pool. Sethashingalg (Sockiopool.consistent_hash);  # #pool. Setinitconn (5);  # #pool. Setminconn (5);  # #pool. Setmaxconn (+);  # #pool. Setmaxidle (* 6);  # #pool. Setmaintsleep (+);  # #pool. Setnagle (false);  # #pool. Setsocketto (+);  # #pool. Setsocketconnectto (0);  

Configuration Bean

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx"    xmlns:task= "Http://www.springframework.org/schema/task" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsd Http://www.springframework.org/schema/task Http://www.springframeworK.org/schema/task/spring-task-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.O Rg/schema/aop/spring-aop-3.0.xsd "> <!--properties config--<bean class=" Org.springframework.bean S.factory.config.propertyplaceholderconfigurer "> <property name=" Order "value=" 1 "/> <property na             Me= "Ignoreunresolvableplaceholders" value= "true"/> <property name= "Locations" > <list> <!--<value>classpath:/com/springmvc/config/memcached.properties</value>--> <value&      gt;/web-inf/config/memcached.properties</value> </list> </property> </bean> <!--MemCached configuration--<bean id= "Memcachedpool" class= "Com.danga.MemCached.SockIOPool" Factory-me          Thod= "getinstance" init-method= "Initialize" destroy-method= "ShutDown" > <property name= "Servers" > <!--${memcached.seRVer}-<list> <value>${memcached.server1}:${memcached.port1}</value> </list> </property> <property name= "Initconn" > <value>$ {memcached.initconn}</value> </property> <property name= "Minconn" > <va              lue>${memcached.minconn}</value> </property> <property name= "Maxconn" >              <value>${memcached.maxConn}</value> </property> <property name= "Maintsleep" > <value>${memcached.maintSleep}</value> </property> <property name= "Nagle "> <value>${memcached.nagle}</value> </property> <property name=" so Cketto "> <value>${memcached.socketTO}</value> </property> </bean> & Lt;/beans>

Configure App-context-memcached.xml to App-context.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx"    xmlns:task= "Http://www.springframework.org/schema/task" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsd Http://www.springframework.org/schema/task Http://www.springframeworK.org/schema/task/spring-task-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.O Rg/schema/aop/spring-aop-3.0.xsd "> <import resource=" app-context-memcached.xml "/><!--memcached Cache configuration-- > <!--<import resource= "App-context-xmemcached.xml"/>--<!--<import resource= "app-context- Spymemcached.xml "/>-<!--@Component and @Resource--and <bean class=" org.springframework.c Ontext.annotation.CommonAnnotationBeanPostProcessor "/> <!--scan all classes in a COM package to complete bean creation and automatic dependency injection-<co Ntext:component-scan base-package= "Com.springmvc.imooc"/> <!--Timer-<!--<task:annotation-driven />-<!--mvc-<mvc:annotation-driven/> <!--Aspect--<!--<aop:aspectj- Autoproxy/>--></beans>

Memcached Tool Class:

/* * File name: Memcachedutils.java * Copyright 2007-2016 517na Tech. Co. Ltd.  All rights Reserved. * Description: Memcachedutils.java * Modified by: Peiyu * Modified: August 2, 2016 * Modified content: Added */package com.springmvc.imooc.util;import Java.io.PrintW Riter;import java.io.stringwriter;import java.util.date;import com.danga.memcached.memcachedclient;/** * @author     Peiyu */public Final class Memcachedutils {/** * cachedclient.    */private static memcachedclient cachedclient;        static {if (cachedclient = = null) {cachedclient = new memcachedclient ("Memcachedpool");     }}/** * constructor. */Private Memcachedutils () {}/** * Adds a new key-value pair to the cache.     If the key already exists, the previous value will be replaced.        * @param key * @param value * @return Boolean */public static Boolean set (String key, Object value) {    Return Setexp (key, value, NULL); }/** * Adds a new key-value pair to the cache.     If the key already exists, the previous value will be replaced. * @param key * @param value * @param expire expiration date New date (1000*10): Expires after 10 seconds * @retuRN Boolean */public static Boolean set (String key, Object value, Date expire) {return Setexp (key, value, E    Xpire); }/** * Adds a new key-value pair to the cache.     If the key already exists, the previous value will be replaced. * @param key * @param value * @param expire expiration date New date (1000*10): Expires after 10 seconds * @return Boolean */Priva        Te Static Boolean setexp (String key, Object value, Date expire) {Boolean flag = false;        try {flag = Cachedclient.set (key, value, expire);        } catch (Exception e) {memcachedlog.writelog ("Memcached set method Error, Key value:" + key + "\ r \ n" + exceptionwrite (e));    } return flag;     /** * The Add command adds a key-value pair to the cache only if the key does not exist in the cache.        * @param key * @param value * @return Boolean */public static Boolean add (String key, Object value) {    Return Addexp (key, value, NULL);     /** * The Add command adds a key-value pair to the cache only if the key does not exist in the cache. * @param key * @param value * @param expire expiration date New date (1000*10): Expires after 10 seconds * @return Boolean     */public static Boolean add (String key, Object value, Date expire) {return Addexp (key, value, expire);     /** * The Add command adds a key-value pair to the cache only if the key does not exist in the cache. * @param key * @param value * @param expire expiration date New date (1000*10): Expires after 10 seconds * @return Boolean */Priva        Te Static Boolean addexp (String key, Object value, Date expire) {Boolean flag = false;        try {flag = Cachedclient.add (key, value, expire);        } catch (Exception e) {memcachedlog.writelog ("Memcached Add method Error, Key value:" + key + "\ r \ n" + exceptionwrite (e));    } return flag;     /** * The Replace command replaces the key in the cache only if the key already exists. * @param key * @param value * @return Boolean */public static Boolean replace (String key, Object value    {return Replaceexp (key, value, NULL);     /** * The Replace command replaces the key in the cache only if the key already exists.    * @param key * @param value * @param expire expiration date New date (1000*10): Expires after 10 seconds * @return Boolean */public static Boolean replace (String key, Object value, Date expire) {return Replaceexp (key, value, expire)    ;     /** * The Replace command replaces the key in the cache only if the key already exists. * @param key * @param value * @param expire expiration date New date (1000*10): Expires after 10 seconds * @return Boolean */Priva        Te Static Boolean replaceexp (String key, Object value, Date expire) {Boolean flag = false;        try {flag = Cachedclient.replace (key, value, expire); } catch (Exception e) {memcachedlog.writelog ("Memcached replace method error, Key value:" + key + "\ r \ n" + exceptionwrite (e))        ;    } return flag;     The/** * Get command is used to retrieve the value associated with the previously added key-value pair.        * @param key * @return Boolean */public static object get (String key) {object obj = null;        try {obj = Cachedclient.get (key);        } catch (Exception e) {memcachedlog.writelog ("Memcached get method Error, Key value:" + key + "\ r \ n" + exceptionwrite (e));    } return obj;}/** * Delete any existing values in the memcached. * @param key * @return Boolean */public static Boolean delete (String key) {return Deleteexp (key, nul    L);     }/** * Delete any existing values in the memcached. * @param key * @param expire Expiration Time New date (1000*10): Expires after 10 seconds * @return Boolean/public static Boolean dele    Te (String key, Date expire) {return deleteexp (key, expire);     }/** * Delete any existing values in the memcached. * @param key * @param expire Expiration Time New date (1000*10): Expires after 10 seconds * @return Boolean */@SuppressWarnings ("Depreca        tion ") private static Boolean deleteexp (String key, Date expire) {Boolean flag = false;        try {flag = Cachedclient.delete (key, expire);         } catch (Exception e) {memcachedlog.writelog ("Memcached Delete method Error, Key value:" + key + "\ r \ n" + exceptionwrite (e));    } return flag;     }/** * Clears all key/value pairs in the cache. * @return Boolean */public static Boolean Flashall () {boolEAN flag = false;        try {flag = Cachedclient.flushall ();        } catch (Exception e) {memcachedlog.writelog ("Memcached Flashall method error \ r \ n" + exceptionwrite (e));    } return flag;     }/** * Returns exception stack information, String type. * @param e Exception * @return Boolean */private static String Exceptionwrite (Exception e) {Stringwrit        er sw = new StringWriter ();        PrintWriter pw = new PrintWriter (SW);        E.printstacktrace (PW);        Pw.flush ();    return sw.tostring (); }}

  

Test:

System.out.println ("AA", "BB", New Date (Memcachedutils.set)); Object obj = Memcachedutils.get ("AA");  System.out.println ("***************************");  System.out.println (Obj.tostring ());  

Spring Integrated memcached Three ways (i)

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.