一、概念
Memcached是danga.com开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能。
二、原理
Memcached有两个核心组件组成:服务端(ms)和客户端(mc)。首先mc拿到ms列表,并对key做hash转化,根据hash值确定kv对所存的ms位置。然后在一个memcached的查询中,mc先通过计算key的hash值来确定kv对所处在的ms位置。当ms确定后,客户端就会发送一个查询请求给对应的ms,让它来查找确切的数据。因为ms之间并没有护卫备份,也就不需要互相通信,所以效率较高。
三、适用场合
1.分布式应用。由于memcached本身基于分布式的系统,所以尤其适合大型的分布式系统。
2.数据库前段缓存。数据库常常是网站系统的瓶颈。数据库的大并发量访问,常常造成网站内存溢出。当然我们也可以使用Hibernate的缓存机制。但memcached是基于分布式的,并可独立于网站应用本身,所以更适合大型网站进行应用的拆分。
3.服务器间数据共享。举例来讲,我们将网站的登录系统、查询系统拆分为两个应用,放在不同的服务器上,并进行集群,那这个时候用户登录后,登录信息如何从登录系统服务器同步到查询系统服务器呢?这时候,我们便可以使用memcached,登录系统将登录信息缓存起来,查询系统便可以获得登录信息,就像获取本地信息一样。
四、客户端版本
Memcached Client目前有一下四种:
Memcached Client for Java,比 SpyMemcached更稳定、更早、更广泛;
SpyMemcached,比 Memcached Client for Java更高效;
XMemcached,比 SpyMemcache并发效果更好。
alisoft-xplatform-asf-cache阿里软件的架构师岑文初进行封装的。里面的注释都是中文的,比较好
五、服务器端
安装
这里介绍windows环境的安装。
1.下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached
2.在cmd下输入 'c:\memcached\memcached.exe -d install' 安装
3.再输入: 'c:\memcached\memcached.exe -d start' 启动。
以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
内存分配
默认情况下,ms是用一个内置的叫“块分配器”的组件来分配内存的。舍弃c++标准的malloc/free的内存分配,而采用块分配器的主要目的 是为了避免内存碎片,否则操作系统要花费更多时间来查找这些逻辑上连续的内存块(实际上是断开的)。用了块分配器,ms会轮流的对内存进行大块的分配,并 不断重用。当然由于块的大小各不相同,当数据大小和块大小不太相符的情况下,还是有可能导致内存的浪费。
同时,ms对key和data都有相应的限制,key的长度不能超过250字节,data也不能超过块大小的限制 --- 1MB。
因为 mc所使用的hash算法,并不会考虑到每个ms的内存大小。理论上mc会分配概率上等量的kv对给每个ms,这样如果每个ms的内存都不太一样,那可能 会导致内存使用率的降低。所以一种替代的解决方案是,根据每个ms的内存大小,找出他们的最大公约数,然后在每个ms上开n个容量=最大公约数的 instance,这样就等于拥有了多个容量大小一样的子ms,从而提供整体的内存使用率。
缓存策略
当ms的hash表满了之后,新的插入数据会替代老的数据,更新的策略是LRU(最近最少使用),以及每个kv对的有效时限。Kv对存储有效时限是在mc端由app设置并作为参数传给ms的。
同时ms采用是偷懒替代法,ms不会开额外的进程来实时监测过时的kv对并删除,而是当且仅当,新来一个插入的数据,而此时又没有多余的空间放了,才会进行清除动作。
六、范例
1.加载commons-pool-1.5.6.jar、java_memcached-release_2.6.6.jar、slf4j-api-1.6.1.jar、slf4j-simple-1.6.1.jar
2.创建memcached工具类:
public class MemcachedUtil {
/** * memcached客户端单例 */ private static MemCachedClient cachedClient = new MemCachedClient(); /** * 初始化连接池 */ static { //获取连接池的实例 SockIOPool pool = SockIOPool.getInstance(); //服务器列表及其权重 String[] servers = {"127.0.0.1:11211"}; Integer[] weights = {3}; //设置服务器信息 pool.setServers(servers); pool.setWeights(weights); //设置初始连接数、最小连接数、最大连接数、最大处理时间 pool.setInitConn(10); pool.setMinConn(10); pool.setMaxConn(1000); pool.setMaxIdle(1000*60*60); //设置连接池守护线程的睡眠时间 pool.setMaintSleep(60); //设置TCP参数,连接超时 pool.setNagle(false); pool.setSocketTO(60); pool.setSocketConnectTO(0); //初始化并启动连接池 pool.initialize(); //压缩设置,超过指定大小的都压缩
// cachedClient.setCompressEnable(true);
// cachedClient.setCompressThreshold(1024*1024);
}
private MemcachedUtil(){
}
public static boolean add(String key, Object value) {
return cachedClient.add(key, value);
}
public static boolean add(String key, Object value, Integer expire) {
return cachedClient.add(key, value, expire);
}
public static boolean put(String key, Object value) {
return cachedClient.set(key, value);
}
public static boolean put(String key, Object value, Integer expire) {
return cachedClient.set(key, value, expire);
}
public static boolean replace(String key, Object value) {
return cachedClient.replace(key, value);
}
public static boolean replace(String key, Object value, Integer expire) {
return cachedClient.replace(key, value, expire);
}
public static Object get(String key) {
return cachedClient.get(key);
}
}3. 创建需要缓存的对象:
public class UserBean implements Serializable {
private static final long serialVersionUID = 9174194101246733501L; private String username; private String password; public UserBean(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; UserBean other = (UserBean) obj; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; } @Override public String toString() { return "username:" + username + ",password:" + password; }
}4.创建测试用例:
public class MemcachedUtilTest {
@Test public void testMemcached() { MemcachedUtil.put("hello", "world", 60); String hello = (String) MemcachedUtil.get("hello"); Assert.assertEquals("world", hello); for(int i = 0; i < 10000000; ++i) { UserBean userBean = new UserBean("Jason" + i, "123456-" + i); MemcachedUtil.put("user" + i, userBean, 60); Object obj = MemcachedUtil.get("user" + i); Assert.assertEquals(userBean, obj); } }
}
5.通过spring注入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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> <property name="servers"> <list> <value>127.0.0.1:11211</value> </list> </property> <property name="initConn"> <value>20</value> </property> <property name="minConn"> <value>10</value> </property> <property name="maxConn"> <value>50</value> </property> <property name="nagle"> <value>false</value> </property> <property name="socketTO"> <value>3000</value> </property> </bean> <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> </bean>
</beans>
6.创建测试用例:
public class MemcachedSpringTest {
private MemCachedClient cachedClient; @Before public void init() { ApplicationContext context = new ClassPathXmlApplicationContext("com/luo/config/beans.xml"); cachedClient = (MemCachedClient)context.getBean("memcachedClient"); } @Test public void testMemcachedSpring() { UserBean user = new UserBean("luo", "hi"); cachedClient.set("user", user); UserBean cachedBean = (UserBean)user; Assert.assertEquals(user, cachedBean); }
}
七、注意点
第一、memcached是在服务器端的内存中缓存对象的,不是缓存或硬盘;
第二、memcached的pool可以关联多个server,
String[] servers = {"10.20.185.12:11001","10.20.185.25:11001"};
Integer[] weights = {3,7};
该配置表示30%的缓存在放在第一台服务器,70%的将放在第二台服务器,这样便可以充分利用不同服务器的内存了;
第三、我最困惑的是client是如何得到相应的pool的,后然看了点源码才知道是这样的。client是通过pool的name关联到某个pool的,上面的例子中在SockIOPool pool = SockIOPool.getInstance(); 和MemCachedClient client=new MemCachedClient();虽然都没写poolName,但就是新建了一个”default“的pool,然后client关联到了这个”default“的pool。当然我们在新建这两个对象时可以给定具体的poolName。
下一篇:memcached真实项目中的应用http://blog.youkuaiyun.com/sup_heaven/article/details/32728477
参考文章:
http://snowolf.iteye.com/blog/1471805
http://my249645546.iteye.com/blog/1420061
http://blog.youkuaiyun.com/loujinhe/article/details/8491673?reload
http://hzp.iteye.com/blog/1872664