前提条件:工程需要引入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任务管理器中的工作设置内存,内存专用工作集,提交大小详解

    windows任务管理器中的工作设置内存,内存专用工作集,提交大小详解 http://shashanzhao.com/archives/832.html 虽然是中文字,但是理解起来还是很困难,什么叫工 ...

  2. iOS 上拉刷新和下拉加在更多(第三方框架EGOTableViewPullRefresh)

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  3. ios之无限图片轮播器的实现

    首先实现思路:整个collectionView中只有2个cell.中间始终显示第二个cell. 滚动:向前滚动当前cell的脚标为0,向后滚动当前的cell脚标为2.利用当前cell的脚标减去1,得到 ...

  4. javascript 作用域

    作用域:可以理解为在一定范围内对数据进行读.写操作 域:空间.范围.区域... 作用:读.写 浏览器内部,可以暂时把专门用来读取JS的那部分叫做“js解析器”, “js解析器”工作流程: “js解析器 ...

  5. git ignore不生效删除缓冲

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: 1 2 3 ...

  6. oracle pl sql 解锁表

    select   p.spid,a.serial#, c.object_name,b.session_id,b.oracle_username,b.os_user_name   from   v$pr ...

  7. Ways to access Oracle Database in PostgreSQL

    Today, organizations stores information(data) in different database systems. Each database system ha ...

  8. Better PostgreSQL datacenter schema

  9. [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  10. HDU 3308 LCIS(线段树)

    Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (in ...