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">
    <constructor-arg>
        <value>sockIOPool</value>
    </constructor-arg>
</bean>
<!-- Memcached Connection pool -->
<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
    <constructor-arg>
        <value>sockIOPool</value>
    </constructor-arg>
    <property name="servers">
        <list>
            <value>192.168.200.149:11211</value>
        </list>
    </property>
    <property name="weights">
        <list>
            <value>1</value>
        </list>
    </property>
</bean>

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 -->  
<aop:config>  
    <!-- 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:aspect>  
</aop:config>  

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

@Autowired  
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);  
    System.out.println(cacheKey);  
    //If Memcached doesn't connect  
    if(memCachedClient.stats().isEmpty()){  
        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();  
    //ergodic  
    for(Entry<String, Object> entry : entrySet){  
        if(entry.getKey().startsWith(packageName)){  
            memCachedClient.delete(entry.getKey());  
        }  
    }  
}  





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

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

    ObjectMapper  om = new ObjectMapper();  
    om.setSerializationInclusion(Inclusion.NON_NULL);  

    for(Object arg : args){  

        //flow  
        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  
            e.printStackTrace();  
        }  
        //parameter  
        key.append(".").append(str);  
    }  

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

}

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