前提条件:工程需要引入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. C#线程系列讲座(4):同步与死锁

    虽然线程可以在一定程度上提高程序运行的效率,但也会产生一些副作用.让我们先看看如下的代码:     class Increment     {         private int n = 0;   ...

  2. mantis 安装问题

    1. 邮件 修改 config_defaults_inc.php $g_administrator_email = 'mantis@pooy.net'; $g_webmaster_email = 'm ...

  3. apache allow和deny

    转自 http://www.cnblogs.com/top5/archive/2009/09/22/1571709.html 影响最终判断结果的只有两点: 1. order语句中allow.deny的 ...

  4. 详细讲解Quartz.NET

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  5. Silverlight TabItem选中,未选中样式设置

    <Application.Resources> <LinearGradientBrush x:Key="TabItemBackground" EndPoint=& ...

  6. 类似 select 选择框效果及美化

    网上有各种各样的关于 select 选择框的美化,找了很多,并没有好的样式效果.所以就找了一个利用 ul li 做的类似 select 选择框的效果,不废话了,先上图,效果如下: 对于上图的箭头效果, ...

  7. redhat vim编辑器永久添加行号

    cd ~ vim .vimrc 第一行加入: set nu :wq 保存退出,即可 如果想取消设置,同理删除set nu即可

  8. Dictionary中的结构体转出来

    CGRect keyBoardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

  9. PostgreSQL Replication之第十一章 使用Skytools(4)

    11.4 使用 londiste 复制数据 pgq是一个叫做londiste的复制工具的核心.londiste 的核心是有一个比如比Slony 更加简单,容易使用的机制.如果您在一个大的安装中使用Sl ...

  10. Codeforce Round #225 Div2

    这回的C- -,弄逆序,我以为要弄个正的和反的,没想到是等价的,弄两个还是正确的,结果我又没注意1和0只能指1个方向,结果弄了4个,取了4个的最小值就错了,自己作死没弄出来...,后面又玩去了...哎 ...