前提条件:工程需要引入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. 虚拟化技术比较 PV HVM

    很多人看到同样配置的VPS价格相差很大,甚是不理解,其实VPS使用的虚拟技术种类有很多,如OpenVZ.Xen.KVM.Xen和HVM与PV.在XEN中pv是半虚拟化,hvm是全虚拟化,pv只能用于L ...

  2. ios理解 -- Pro Mutlithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch, and Blocks

    Capturing automatic variables Next, you need to learn what the “together with automatic (local) vari ...

  3. Android中ScrollView嵌套GridView,解决GridView显示不全的问题

    /** * 自定义gridview,解决ScrollView中嵌套gridview显示不正常的问题(1行半) * */ public class MyGridView extends GridView ...

  4. JS函数(获得widn)

    //随机数生成器Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() ...

  5. How to debug PostgreSQL function with pgAdminIII

    How to debug plpgsql with pgAdminIII [root@localhost soft_bak]# git clone git://git.postgresql.org/g ...

  6. PostgreSQL Monitor pg_view

    PostgreSQL Monitor pg_view https://github.com/zalando/pg_view Requirements Linux 2.6, python 2.6, ps ...

  7. AngularJs Test demo &front end MVVM implementation conjecture and argue.

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...

  8. C++之路进阶——codevs1036(商务旅行)

    1036 商务旅行 题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇 ...

  9. ArrayList和LinkList区别

    ArrayList和LinkList区别 前者是数组的数据结构,后者是链表的数据结构 前者应用于排序和查找,后者应用于插入删除

  10. URAL 1416 Confidential(次小生成树)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416 Zaphod Beeblebrox — President of the Impe ...