xmemcached spring 配置方法 添加AuthInfo,進行登錄的方法,和pool的測試

本文介绍了如何在Spring中配置xmemcached,并添加AuthInfo进行登录操作。同时,提供了两种测试方法,包括通过pool获取连接以及使用单独的client进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

spring 配置信息如下:

	<bean
        name="memcachedClientBuilder"
        class="net.rubyeye.xmemcached.XMemcachedClientBuilder" >
        <constructor-arg value="membase url:membase port"/>
        <property
            name="authInfoMap">
            <map>
                <entry key-ref="addrUtil" value-ref="authInfo"/>    
            </map>
        </property>
        <property
            name="connectionPoolSize"
            value="2" >
        </property>
        <property name="commandFactory" >
            <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" >
            </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>
    
    <bean id="addrUtil" class="net.rubyeye.xmemcached.utils.AddrUtil"  scope="prototype" factory-method="getOneAddress">
		<constructor-arg value="membase url:membase port"/>
	</bean>
	
    <bean id="authInfo" class="net.rubyeye.xmemcached.auth.AuthInfo"  scope="prototype" factory-method="plain">
		<constructor-arg value="username"/>
		<constructor-arg value="password"/>
	</bean>


兩種方法進行測試:

1.使用pool來獲取:

@Test
	public void xmemcacheSpringComplexGetClientEveryTime() {

		final ClassPathXmlApplicationContext xbf = new ClassPathXmlApplicationContext(
				"membase.xml");
		final XMemcachedClientBuilder builder = (XMemcachedClientBuilder) xbf
				.getBean("memcachedClientBuilder");
		for (int i = 0; i < 10; i++) {
			final int j = i;
			new Thread(new Runnable() {
				@Override
				public void run() {
					for (int i = 0; i < 10000; i++) {
						File file = new File("D:/test"+j+".txt");
						
						if(!file.exists()){
							try {
								file.createNewFile();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						
						FileOutputStream fos = null;
						PrintStream ps = null;
						
						MemcachedClient memcachedClient = null;
						try {
							fos = new FileOutputStream(file,true);
							ps = new PrintStream(fos,true);
							
							ps.println("Thread " + j + " ," + i + " count , get memcachedClient");

							memcachedClient = builder.build();
							
							memcachedClient.set("Thread " + j + " ," + i
									+ " count", 60000, "Thread " + j + " ," + i
									+ " count");
							
							ps.println("Thread " + j + " ," + i + " count , set function");
							
							System.out.println(memcachedClient.get("Thread "
									+ j + " ," + i + " count"));
							
							ps.println("Thread " + j + " ," + i + " count , get function");
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 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();
						} finally {
							try {
								if (memcachedClient != null) {
									memcachedClient.shutdown();
									ps.println("Thread " + j + " ," + i + " count , shut down function");
									
									ps.println("Thread " + j + " ," + i + " count , end =============");
								}
							} catch (IOException e) {
								System.err
										.println("Shutdown MemcachedClient fail");
								e.printStackTrace();
							}
							
							try {
								fos.close();
								ps.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}
				}
			}).start();
		}

		while (true) {
		}
	}


2.使用一個client:

@Test
	public void xmemcacheSpringComplexGetClientOneTime() throws IOException {

		final ClassPathXmlApplicationContext xbf = new ClassPathXmlApplicationContext(
				"membase.xml");
		XMemcachedClientBuilder builder = (XMemcachedClientBuilder) xbf
				.getBean("memcachedClientBuilder");
		
		

		final MemcachedClient memcachedClient = builder.build();
		
		
		for (int i = 0; i < 200; i++) {
			final int j = i;
			new Thread(new Runnable() {

				@Override
				public void run() {
					for (int i = 0; i < 1000; i++) {
						File file = new File("D:/test"+j+".txt");
						
						if(!file.exists()){
							try {
								file.createNewFile();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						
						FileOutputStream fos = null;
						PrintStream ps = null;
						
						
						try {
							fos = new FileOutputStream(file,true);
							ps = new PrintStream(fos,true);
														
							memcachedClient.set("Thread " + j + " ," + i
									+ " count", 60000, "Thread " + j + " ," + i
									+ " count");
							
							ps.println("Thread " + j + " ," + i + " count , set function");
							
							System.out.println(memcachedClient.get("Thread "
									+ j + " ," + i + " count"));
							
							ps.println("Thread " + j + " ," + i + " count , get function");
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 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();
						} finally {
							/*try {
								if (memcachedClient != null) {
									memcachedClient.shutdown();
									ps.println("Thread " + j + " ," + i + " count , shut down function");
									
									ps.println("Thread " + j + " ," + i + " count , end =============");
								}
							} catch (IOException e) {
								System.err
										.println("Shutdown MemcachedClient fail");
								e.printStackTrace();
							}*/
							
							try {
								fos.close();
								ps.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}
				}
			}).start();
		}

		while (true) {
		}
	}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值