Memcached 配置

目前系统中 Memcatch

 

    <!-- 缓存 -->
      <bean name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
                <!-- XMemcachedClientBuilder have two arguments.First is server list,and second is weights array. -->
                <constructor-arg>
                        <list>
                                <bean class="java.net.InetSocketAddress">
                                        <constructor-arg>
                                                <value>192.168.203.202</value>
                                        </constructor-arg>
                                        <constructor-arg>
                                                <value>11211</value>
                                        </constructor-arg>
                                </bean>
                                <!-- 
                                <bean class="java.net.InetSocketAddress">
                                        <constructor-arg>
                                                <value>localhost</value>
                                        </constructor-arg>
                                        <constructor-arg>
                                                <value>12001</value>
                                        </constructor-arg>
                                </bean>
                                 -->
                        </list>
                </constructor-arg>
                <constructor-arg>
                        <list>
                                <value>1</value>
                                <!--<value>2</value>  -->
                                
                        </list>
                </constructor-arg>
                <!-- 
                <property name="authInfoMap">
                        <map>
                                <entry key-ref="server1">
                                        <bean class="net.rubyeye.xmemcached.auth.AuthInfo"
                                                factory-method="typical">
                                                <constructor-arg index="0">
                                                        <value>cacheuser</value>
                                                </constructor-arg>
                                                <constructor-arg index="1">
                                                        <value>123456</value>
                                                </constructor-arg>
                                        </bean>
                                </entry>
                        </map>
                </property>
                 -->
                <property name="connectionPoolSize" value="2"></property>
                <property name="commandFactory">
                        <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
                </property>
                <property name="sessionLocator">
                        <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
                </property>
                <property name="transcoder">
                        <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
                </property>
        </bean>
        <!-- Use factory bean to build memcached client -->
        <bean name="memcachedClient" factory-bean="memcachedClientBuilder"
                factory-method="build" destroy-method="shutdown"/>
      
 

 

使用SPRING 管理的 。 。

 

引用JAR包 :

 

Maven 配置 : 

 

        <dependency>
            <groupId>com.googlecode.xmemcached</groupId>
            <artifactId>xmemcached</artifactId>
        </dependency>
 

使用的Java代码:

 

 

	private Brand getBrandById(long brandId){
		Brand brandres=null;
		String key="brand_cacheD_"+brandId;
		MemcachedClient memcachedClient=(MemcachedClient) SpringBeanProxy.getBean("memcachedClient");
		try {
			Object object=memcachedClient.get(key);
			if(object!=null){
				brandres=(Brand)object;
				log.debug(" memcached get Brand ");
			}else{
				BrandDao brandDao=(BrandDao)SpringBeanProxy.getBean("brandDao");
				brandres=brandDao.getById(brandId);
				memcachedClient.set(key, 30*60*1000, brandres);
				log.debug(" db get Brand ");
			}
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MemcachedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return brandres;
	}
	
 

可见,  基本就是像使用一个 HASHMAP 一样的来使用 。 。。 

 

 

 

 

 

你可能感兴趣的:(maven,bean,.net,配置管理,memcached)