发表于: 2016-12-19 13:24:14

2 2062


一.今天完成的事情

1从昨天晚上到今天2点吧。做了一个Spring整合memcache的小实验吧(调试了半天啊,总之返回的是空。看下面代码吧)

1-1.建立   memcached-content.xml     

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="locations">

            <list>

                <value>classpath:properties/memcache.properties</value>

            </list>

        </property>

    </bean>

    <bean id="memCachedPool" class="com.danga.MemCached.SockIOPool"

        factory-method="getInstance" init-method="initialize"

        destroy-method="shutDown">

        <constructor-arg>

            <value>memCachedPool</value>

        </constructor-arg>

        <property name="servers">

            <list>

                <value>${memcache.server}</value>

            </list>

        </property>

        <property name="initConn">

            <value>${memcache.initConn}</value>

        </property>

        <property name="minConn">

            <value>${memcache.minConn}</value>

        </property>

        <property name="maxConn">

            <value>${memcache.maxConn}</value>

        </property>

        <property name="maintSleep">

            <value>${memcache.maintSleep}</value>

        </property>

        <property name="nagle">

            <value>${memcache.nagle}</value>

        </property>

        <property name="socketTO">

            <value>${memcache.socketTO}</value>

        </property>

    </bean>

    <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">

        <constructor-arg>

            <value>memCachedPool</value>

        </constructor-arg>

    </bean>

2.memecache.properties

memcache.server=127.0.0.1:11211

memcache.initConn=20

memcache.minConn=10

memcache.maxConn=350

memcache.maintSleep=3000

memcache.nagle=false

memcache.socketTO=3000

3.testMemcache类

public class testMemcache {

MemCachedClient memCachedClient;

@Before

public void beforeTest() {

ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

memCachedClient = (MemCachedClient)context.getBean("memCachedClient");

}

@Test

public void TestMem() {

memCachedClient.set("wangsx", "wangjing");

System.out.println(memCachedClient.get("wangsx"));

}

}

2小实验中遇到的坑:

   坑1.昨天用Memcached-Java-Client  3.0的maven。这个可以直接在maven中配置。maven远程仓库有此jar包

      但昨天晚上无论怎么调试返回的都是Null值。

      今天我换成Memcached-Java-Client2.6的       

   <dependency>

        <groupId>com.danga</groupId>

        <artifactId>java-memcached</artifactId>

        <version>2.6.6</version>

    </dependency>

 Memcached-Java-Client2.6 maven远程仓库没有此包(说白了就是用坐标下载不了此包)

  在网上下载一个2.6的jar。用maven导入第三方jar包(自己百度一下)

  注意2.6的jar包有依赖的。我就是在项目中没有引用Commons-pool.jar一直报错,调了好半天才怀疑缺少依赖jar包

   坑2:memcached-content.xml :在使用spring整合Memcacheds时,如果在Spring配置文件中指明了poolname,则在初始化MemecachedClient时,需要在其构造函数中指明poolname.,如果没有声明poolname,则MemechachedClient则或获取名为default的Pool实例.

相应的链接:http://blog.csdn.net/l1028386804/article/details/48441815

特别提示:无论何时都不要忘记maven clean与maven install的力量。有时刚接触一个新事物,我们可能怀疑自己的配置不对,jar包不对。但往往会忽悠现有的错误,我严重怀疑我昨天晚上没有maven clean和install,所以没有调通。今天clean和install后证明我昨天的思路什么全是对的。

3.memcahe与spring整合不错的博客链接:(这几篇的博客质量挺好的,适合刚入门的同学)

http://www.cnblogs.com/cocohxq/archive/2012/08/07/2626267.html

http://blog.csdn.net/sup_heaven/article/details/32337711

http://blog.csdn.net/sup_heaven/article/details/32728477

http://www.tuicool.com/articles/Yf2ie2(此实验看完上面几篇,自己很容易调通)

(看完这些博客,我的代码全通啦)

4.StudentMapper\ClassesMapper\ProfessionMapper加入了缓存,有的方法没有加缓存:

  //部分代码...

public class StudentServiceImpl extends MemcachedSupport implements StudentService {

  @Autowired

  StudentMapper studentMapper;

  public Student selectStudentById(int id) {

    Student student = null;

    String studentKey = MemcachedKeyUtil.getStudentsKey(id);

    student = (Student)getDetailData(0, studentKey);

    if(student == null) {

      student = studentMapper.selectStudentById(id);

      if(student != null) {

          setDetailData(0, studentKey, student);

          System.out.println("huancun"+studentKey+ getDetailData(0, studentKey) );

      }

    }

   return student;

  }

 。。。。

}

有个疑问:上面的方法是按照ID查询的,如果按照Phone,studentName查询。是不是每个查询都要建立缓存,这不就相当于数据库中的数据冗余吗???

二.明天的计划

1.在新建数据的时候同时维护好缓存(没有新建数据接口就自己加上,可以分成是压测JSP和Json接口两种方式) ,确定数据没问题,重新压测服务器,测出90%的线在哪里.

2.停止Memcache进程,观察压测数据。部署两台WEB,使用Nginx的Upstream来做负载。重新压测

3.今天时间把握的挺好的,几乎都在这敲代码看博客。

   明天安心继续搞,争取把本地Win7中的memcache搞完

  后天在云服务器上做redis。在云服务器上就不搞memcache啦。。。

三.遇到的问题

1.当用set向Memcache存数据,比如: id:student类.  当用get(key)取数据时报错:大概是没有序列化student

   再建立Student类时,实现Serializable接口。

2.以前的错误啦。插入Student记录时,忘记:@Options(useGeneratedKeys=true, keyProperty="id")

问题是:mybatis插入数据获取自增长Id。

四.收获

1.maven怎样引入第三方jar包。



返回列表 返回列表
评论

    分享到