java interface calls memcached

Keywords: Java Spring Database JSON

1. Before connecting to the memcache server, set up the memcache server in advance, remember the ip and port number of the created server, and use the ip and port number to connect using java code.

2. Start using java links

2.1 introduce the jar files memcached-2.0.1.jar and memcached-1.1.jar into the project

2.2 configure to load memcache into the spring configuration file

<!-- Memcached To configure -->
<bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
<!-- Memcached Connection pool -->
<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
    <property name="servers">
    <property name="weights">

When using memcache, when users query data, they first go to the memcache cache server to obtain data. If not, they will go to the service layer and call the dao layer method to query the database, and then synchronize to the memcache server. An aop needs to be configured to intercept the query method with get *, as follows:

[html] view plain copy

<!-- Spring  Aop To configure   get* Configure orbit -->  
    <!-- Noodles -->  
    <aop:aspect ref="cacheInterceptor">  
        <!-- spot -->  
        <aop:around method="doAround" pointcut="execution(* cn.itcast.core.service.*.*.get*(..))"/>  
        <!-- change  -->  
        <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.update*(..))"/>  
        <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.add*(..))"/>  
        <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.delete*(..))"/>  

aop method
[html] view plain copy
[java] view plain copy
*Caching tangent objects of data in Memcached
* aroud
* after
* @author lx
public class CacheInterceptor {

private MemCachedClient memCachedClient;  

//Time cache time  
public static final int TIMEOUT = 360000;//second  

private int expiry = TIMEOUT;  

//Configure wrapping method  
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{  
    //Go to Memcached to see if there is our package name + class name + method name + parameter (multiple)  
    String cacheKey = getCacheKey(pjp);  
    //If Memcached doesn't connect  
        System.out.println("Memcached The server may not exist or may not be connected");  
        return pjp.proceed();  

    //Return value  
    if(null == memCachedClient.get(cacheKey)){  
        //Back to Service  
        Object proceed = pjp.proceed();  
        //Put it in Memcached first  
        memCachedClient.set(cacheKey, proceed,expiry);  
    return memCachedClient.get(cacheKey);  
//Post clean up get due to database data changes*  
public void doAfter(JoinPoint jp){  
    //Package name + class name + method name + parameters (multiple) generate Key  
    //Package name + class name   
    String packageName = jp.getTarget().getClass().getName();  

    //Clean up the package name + class name  
    Map<String, Object> keySet = MemCachedUtil.getKeySet(memCachedClient);  
    Set<Entry<String, Object>> entrySet = keySet.entrySet();  
    for(Entry<String, Object> entry : entrySet){  

//Package name + class name + method name + parameters (multiple) generate Key  
public String getCacheKey(ProceedingJoinPoint pjp){  
    StringBuilder key = new StringBuilder();  
    //Package name + class name cn.itcast.core.service.product.productserviceimpl.productlist  
    String packageName = pjp.getTarget().getClass().getName();  
    // Method name  
    String methodName = pjp.getSignature().getName();  

    //Parameters (multiple)  
    Object[] args = pjp.getArgs();  

    ObjectMapper  om = new ObjectMapper();  

    for(Object arg : args){  

        StringWriter str = new StringWriter();   

        //The process of object to Json is character stream  
        try {  
            om.writeValue(str, arg);  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  

    return key.toString();  
public void setExpiry(int expiry) {  
    this.expiry = expiry;  


Posted by ineedhelp on Sat, 02 May 2020 12:42:36 -0700