zoukankan      html  css  js  c++  java
  • spring配置xmemcached及使用 东师理想

    原创文章,请尊重作者的劳动!

    本人其他的一些memcached的博客在这里是比较全的

    之前写了一些memcached的配置,但是那个和spring的配置是不能用在生产环境的,今天把spring和xmemcached的配置文件告诉一下大家如何配置,

    注释的部分是可以配置多个memcached服务器的

    <!-- 引入Memcached客户端开始-->
        <bean name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">        
            <constructor-arg>
                <list>
                    <bean class="java.net.InetSocketAddress">
                        <constructor-arg>
                            <value>192.168.100.102</value>
                        </constructor-arg>
                        <constructor-arg>
                            <value>11211</value>
                        </constructor-arg>
                    </bean>
                    <!-- 
                    <bean class="java.net.InetSocketAddress">
                        <constructor-arg>
                            <value>localhost</value>
                        </constructor-arg>
                        <constructor-arg>
                            <value>12001</value>
                        </constructor-arg>
                    </bean>
                     -->
                </list>
            </constructor-arg>
            <constructor-arg>
                <list>                 
                    <value>1</value>
                    <!--
                    <value>2</value>
                     -->
                </list>
            </constructor-arg>        
            <property name="connectionPoolSize" value="2"></property>
            <property name="commandFactory">
                <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
            </property>
            <property name="sessionLocator">
                <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
            </property>
            <property name="transcoder">
                <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
            </property>
        </bean>    
        <bean name="memcachedClient" factory-bean="memcachedClientBuilder"
            factory-method="build" destroy-method="shutdown" />    
        <!-- 引入Memcached客户端结束 -->    

    具体在java类中使用为,以下为xmemcached的使用

    package com.dsideal.demo.memcached;
    
    import java.lang.reflect.Method;
    import java.util.List;
    
    import javax.annotation.Resource;
    
    import net.rubyeye.xmemcached.MemcachedClient;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import com.alibaba.fastjson.JSON;
    import com.dsideal.common.Cache;
    import com.dsideal.common.CacheUpdate;
    import com.dsideal.common.Guid;
    import com.dsideal.common.MD5;
    import com.dsideal.demo.bean.SysLoginPersonBean;
    import com.dsideal.demo.service.ICacheLogService;
    
    @Aspect
    @Component
    public class CacheAopNoMysql {
        @Autowired
        private MemcachedClient memcachedClient;
        
        //aop拦截指向
        @Pointcut("execution (* com.dsideal.demo.service.impl.*.memcache*(..))")  
        public void pointcut(){}  
    
        /*    
        //方法执行前调用  
        //@Before("pointcut()")  
        public void before() {  
            System.out.println("before");  //2
        }  
        */
    
        @Resource
        private ICacheLogService cacheLogService;  
    
        //方法执行的前后调用   
        @Around("pointcut()")  
        //ProceedingJoinPoint 目标类连接点对象
        public Object doAround(ProceedingJoinPoint call) throws Throwable{  
            //返回最终结果
            Object result = null;
            //定义版本号
            String prefixValue = null;
            Method[] methods = call.getTarget().getClass().getDeclaredMethods();    
            Signature signature = call.getSignature();  
            MethodSignature methodSignature = (MethodSignature) signature;    
            Method method = methodSignature.getMethod();  
            for(Method m:methods)
            {
                //循环方法,找匹配的方法进行执行  
                if(m.getName().equals(method.getName()))
                {                
                    //增加,判断是不是Cache标签,如果是Cache标签,那么处理缓存的问题
                    if(m.isAnnotationPresent(Cache.class))
                    {  
                        Cache cache = m.getAnnotation(Cache.class);
                        //Object tempType = m.getGenericReturnType();
                        //如果memcached中存在
                        if(cache!=null)
                        {  
                            //获取注解前缀,这里为 sys,实际使用是为各个业务包名
                            String prefix = cache.prefix();  
                            //获取版本号
                            if(null != memcachedClient.get(prefix))
                            {
                                prefixValue = memcachedClient.get(prefix).toString();
                            }                        
                            else
                            {
                                prefixValue = Guid.getGuid();
                                memcachedClient.set(prefix,60*60*24*30, prefixValue);
                            }
                            
                            //获取方法名+参数类型+参数值+版本号 转 MD5
                            String key = this.getKey(method, call.getArgs(), prefixValue, prefix);  
                             
                            result =memcachedClient.get(key);  
                            if(null == result)
                            {  
                                try {  
                                    //执行aop拦截的方法
                                    result = call.proceed();  
                                    //获取注解配置memcached死亡时间
                                    int expiration = cache.expiration();
                                    //利用fastjson序列化list<bean>存入memcached中
                                    //具体fastjson使用方法请参考:http://www.cnblogs.com/cczhoufeng/archive/2013/04/03/2997871.html
                                    if(null == prefixValue)
                                    {
                                        memcachedClient.set(prefix,60*60*24*30,Guid.getGuid());
                                    } 
                                    memcachedClient.set(key, expiration,JSON.toJSONString(result));                                
                                } catch (Throwable e) {  
                                    e.printStackTrace();  
                                }  
                            }  
                            else {
                                //如果memcached中存在结果,需要将result反序列化后返回结果
                                String memresult = result.toString();
                                //反序列化
                                List<SysLoginPersonBean> list = JSON.parseArray(memresult, SysLoginPersonBean.class);
                                result = list;
                            }
    
                        }  
                    }
                    //如果是Update标签的话,处理更新问题
                    else     if(m.isAnnotationPresent(CacheUpdate.class))                    
                        {  
                            /********************************************************/
                            //如果修改操作时
                            CacheUpdate cUpdate = m.getAnnotation(CacheUpdate.class);  
                            if(cUpdate!=null)
                            {  
                                result = call.proceed();
                                String prefix = cUpdate.prefix();  
                                //获取当前版本号
                                if(null != memcachedClient.get(prefix))
                                {
                                    //修改后,版本号
                                    memcachedClient.replace(prefix,60*60*24*30, Guid.getGuid());
                                }
                                else
                                {
                                    memcachedClient.set(prefix,60*60*24*30, Guid.getGuid());
                                }                            
                            }
                            /********************************************************/
                        }
                    //找到这个方法就退出循环
                    break;  
                }  
            }
            return result;
        }
    
        /** 
         * 组装key值 
         * @param method 
         * @param args 
         * @return 
         */  
        private String getKey(Method method, Object [] args, String prefixValue, String prefix){  
            StringBuffer sb = new StringBuffer();   
            //获取方法名
            String methodName = method.getName();
            //获取参数类型
            Object[] classTemps = method.getParameterTypes();
            //包名前缀
            sb.append(prefix+"_");
            //存入方法名
            sb.append(methodName);
    
            for (int i = 0; i < args.length; i++) {
                sb.append(classTemps[i]+"&");
                if (null == args[i]) {
                    sb.append("null");
                } else if ("".equals(args[i])) {
                    sb.append("*");
                } else {
                    sb.append(args[i]);
                }
            }
            sb.append(prefixValue);
            return MD5.getMD5(sb.toString());  
    
        }  
    }
  • 相关阅读:
    几种存储器类型总结
    几种存储器类型总结
    JPEG2000开发SDK及其特点
    JPEG2000开发SDK及其特点
    测试代码的编写
    测试代码的编写
    关于opencv的几个小总结
    关于opencv的几个小总结
    假设检验与判决准则(一)
    Canny算法源码,欢迎交流
  • 原文地址:https://www.cnblogs.com/cczhoufeng/p/3048853.html
Copyright © 2011-2022 走看看