memcached搭建与简单java连接测试

安装需要下面这两个包,注意下载的位数要跟虚拟机的位数一致

http://www.rpmfind.net/linux/rpm2html/search.php?query=libevent

http://rpmfind.net/linux/rpm2html/search.php?query=memcached

上传到虚拟机

rpm -ivh 后面跟上安装包名称,分别执行安装就好了,要是一块安装不好的话,分开安装

 rpm -qa |grep memcached 查询这个软件装好没

rpm -ql memcached     查看安装到哪儿了

more /etc/sysconfig/memcached查看memcached的配置信息

port是端口号

USER是用户

MAXCONN是1024个最大连接数

CACHESIZE是存储64兆最大缓存数据

options设置谁可以连接  参数为""控的话就是谁都可以连 ,也可以指定ip地址,指定哪个ip可以连,设置多个ip的话逗号隔开就行了 例如 OPTIONS="192.168.117.1,192.168.117.2"

memcached是没有用户名密码的谁都可以连,就是在这个配置文件里修改它的参数信息

 service memcached start  启动memcached

然后使用telnet命令客户端远程连接它,先开启一下telnet

输出telnet ip 11211  进行连接  按一下回车就会黑屏,再按一下就会出现个error,然后就可以进行命令操作了

然后get set 命令测试通过了,其它命令去官网查看吧

java连接memcached,先导入jar包,2.4.2和2.0.1任选一个就可以,2.0.1的有连接池,比较稳定2.4.2没有但是速度快,不稳定,我这里用的2.0.1的

加入memcached的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- Memcached 配置 -->
	<bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
		<constructor-arg>
			<value>sockIOPool</value>
		</constructor-arg>
	</bean>
	<!-- Memcached连接池 -->
	<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
		<constructor-arg>
			<value>sockIOPool</value>
		</constructor-arg>
		<property name="servers">
			<list>
				<value>192.168.200.149:11211</value>
			</list>
		</property>
		<property name="weights">
			<list>
				<value>1</value>
			</list>
		</property>
	</bean>
	</beans>

 

 

写测试类进行测试,如果要存入一个实体对象的话,改实体类需要实现序列化接口

package cn.itcast;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import cn.itcast.common.web.aop.MemCachedUtil;
import cn.itcast.core.bean.user.Buyer;

import com.danga.MemCached.MemCachedClient;

/**
 * 测试
 * @author lx
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:memcached.xml")

public class TestMemcached{

	@Autowired
	private MemCachedClient memCachedClient;
	@Test
	public void testAdd() throws Exception {
		Buyer buyer = new Buyer();
		buyer.setUsername("范冰冰");
		
		
		//memCachedClient.set("fbb2",buyer);
		
		Map<String, Object> keySet = MemCachedUtil.getKeySet(memCachedClient);
		
		
		Set<Entry<String, Object>> entrySet = keySet.entrySet();
		for(Entry<String, Object> entry : entrySet){
			System.out.println(entry.getKey());
		}
		
		
/*		Object object = memCachedClient.get("fbb");
		
		System.out.println(object);*/
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值