前提条件:工程需要引入jar包java_memcached-release_2.0.1.jar

第一步:添加memcached的配置文件。

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="false" />
        <property name="locations">
            <list>
                <value>classpath:memcache.properties</value>
            </list>
        </property>
</bean>

配置文件内容如下:
memcache.server=xxx.xxx.xxx.xxx:11111
memcache.weights=1
memcache.initConn=1
memcache.minConn=1
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000

第二步:添加memcached的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="weights"><list><value>${memcache.weights}</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>

下面看一下com.danga.MemCached.SockIOPool的源代码,重点是SockIOPool构造函数:

public static synchronized SockIOPool getInstance(String poolName)
{
        if (pools.containsKey(poolName))
            return pools.get(poolName);

        SockIOPool pool = new SockIOPool();
        pools.put(poolName, pool);

        return pool;
}
<bean id="memCacheClient" class="com.danga.MemCached.MemCachedClient">
        <constructor-arg><value>memCachedPool</value></constructor-arg>
</bean>

下面看一下com.danga.MemCached.MemCachedClient的源代码,重点是MemCachedClient的构造函数:

public MemCachedClient(String poolName)
{
        this.poolName = poolName;
        init();
}

第三步:测试memcached的功能。

public class MemcacheTest {
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MemCachedClient memCachedClient=(MemCachedClient)context.getBean("memCacheClient");
        memCachedClient.set("hello", "swiftlet");
        memCachedClient.get("hello");
    }
}

Spring集成memcached的详细介绍的更多相关文章

  1. 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)

    硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...

  2. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  3. struts2 + spring + mybatis 框架整合详细介绍

    struts2 + spring + mybatis  框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...

  4. (2)Spring集成Quartz定时任务框架介绍和Cron表达式详解

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  5. Spring集成Quartz定时任务框架介绍和Cron表达式详解

    原文地址:http://www.cnblogs.com/obullxl/archive/2011/07/10/spring-quartz-cron-integration.html 在JavaEE系统 ...

  6. Spring集成Quartz定时任务框架介绍

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  7. Spring 相关jar包详细介绍

    文章转自:http://blog.csdn.net/farawayhome/article/details/6623946 aspectj目录下是在Spring框架下使用aspectj的源代码和测试程 ...

  8. Spring集成Memcached三种方式(一)

    转载:http://blog.csdn.net/u013725455/article/details/52102170 Memcached Client目前有3种: Memcached Client ...

  9. Spring @Component的作用详细介绍

    @component 作用 1.@controller 控制器(注入服务)2.@service 服务(注入dao)3.@repository dao(实现dao访问)4.@component (把普通 ...

随机推荐

  1. windows下使用pthreads

    pthread-win32在Windows上实现了线程相关的Posix标准,接口一模一样 包含: thread mutex cond swlock spin sem barrier https://s ...

  2. DG - dataguard trouble shooting的相关视图

    •V$DATAGUARD_STATS:显示dataguard统计信息 备库 SQL> select * from v$dataguard_stats; NAME VALUE UNIT TIME_ ...

  3. Singlton设计模式

    单例定义: 确保一个类只有一个实例,并提供全局访问点. 适用场景: 1.) 当系统中某个类必须仅有一个实例对象,同时访问该系统的所有访问者必须访问同一个实例对象时,且该对象实例自身占用资源又不大时. ...

  4. PG sys function

    The System Catalogs of PostgreSQLscott=# \dS List of relations Schema | Name | Type | Owner -------- ...

  5. SQL查询 练习题

    设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...

  6. Java基础(53):内部类(转)

    java中的内部类总结 内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行 ...

  7. Server.Transfer,Response.Redirect用法点睛

    Server.Transfer,Response.Redirect的区别 如果你读过很多行业杂志和 ASP.NET 示例,你会发现,大多数人使用 Response.Redirect 将用户引导到另一个 ...

  8. org.openqa.selenium.WebDriverException: f.QueryInterface is not a function Command duration or timeout:

    今天偶遇一个问题,运行项目时,发现这个问题: org.openqa.selenium.WebDriverException: f.QueryInterface is not a functionCom ...

  9. linux第13天 生产者与消费者

    pthread_cond_t   my_condition = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INIT ...

  10. Cookie—的使用

    编写 Cookie 浏览器负责管理用户系统上的 Cookie.Cookie 通过 HttpResponse 对象发送到浏览器,该对象公开称为 Cookies 的集合. 可以将 HttpResponse ...