• Spring集成memcached的详细介绍


    前提条件:工程需要引入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");
        }
    }
  • 相关阅读:
    sls异常日志钉钉告警
    vue3.0学习
    docker安装mysql5.7
    封装quarz,实现自己的job集群
    封装hangfire,实现自己的job集群
    ts+vue3学习
    cellreport集成iis
    ocelot网关设置
    asp.net core 跨域
    高性能的MQBUS
  • 原文地址:https://www.cnblogs.com/mistor/p/6260690.html
Copyright © 2020-2023  润新知