memcached是danga.com的一个项目。它是一款开源的高性能的分布式内存对象缓存系统。最早是给LiveJournal提供服务的。后来逐渐被越来越多的大型站点所採用。用于在应用中减少对数据库的訪问。提高应用的訪问速度。并减少数据库的负载。

 为了在内存中提供数据的快速查找能力,memcached使用key-value形式存储和訪问数据。在内存中维护一张巨大的HashTable。使得对数据查询的时间复杂度减少到O(1),保证了对数据的高性能訪问。内存的空间总是有限的,当内存没有很多其它的空间来存储新的数据是,memcached就会用LRU算法将近期不常訪问的数据淘汰掉,以腾出空间来存放新的数据。

memcached存储支持的数据格式也是灵活多样的。通过对象的序列化机制。能够将更高层抽象的对象转换成为二进制数据,存储在缓存服务器中,当前端应用须要时,又能够通过二进制内容反序列化,将数据还原成原对象。


 因为memcached使用了libevent来进行高效的网络连接处理,因此在安装memcached之前,须要先安装libevent。

 全部的安装包能够在这里下载。

安装libevent

版本号 libevent-2.0.21-stable.tar.gz(如果放在/root文件夹下)

[root@zzh ~]# mkdir libevent
[root@zzh ~]# tar -zvxf libevent-2.0.21-stable.tar.gz
[root@zzh ~]# cd libevent-2.0.21-stable
[root@zzh libevent-2.0.21-stable]# ./configure --prefix=/root/libevent
[root@zzh libevent-2.0.21-stable]# make
[root@zzh libevent-2.0.21-stable]# make install

測试libevent是否成功安装(没有必定性。仅仅供參考)

[root@zzh libevent-2.0.21-stable]# ls -al /root/libevent/lib | grep libevent
lrwxrwxrwx. 1 root root 21 4月 5 22:05 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 968722 4月 5 22:05 libevent-2.0.so.5.1.9
-rw-r--r--. 1 root root 1571586 4月 5 22:05 libevent.a
lrwxrwxrwx. 1 root root 26 4月 5 22:05 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 585265 4月 5 22:05 libevent_core-2.0.so.5.1.9
-rw-r--r--. 1 root root 978314 4月 5 22:05 libevent_core.a
-rwxr-xr-x. 1 root root 980 4月 5 22:05 libevent_core.la
lrwxrwxrwx. 1 root root 26 4月 5 22:05 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx. 1 root root 27 4月 5 22:05 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 404884 4月 5 22:05 libevent_extra-2.0.so.5.1.9
-rw-r--r--. 1 root root 593344 4月 5 22:05 libevent_extra.a
-rwxr-xr-x. 1 root root 987 4月 5 22:05 libevent_extra.la
lrwxrwxrwx. 1 root root 27 4月 5 22:05 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 945 4月 5 22:05 libevent.la
lrwxrwxrwx. 1 root root 30 4月 5 22:05 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x. 1 root root 18462 4月 5 22:05 libevent_pthreads-2.0.so.5.1.9
-rw-r--r--. 1 root root 18662 4月 5 22:05 libevent_pthreads.a
-rwxr-xr-x. 1 root root 1008 4月 5 22:05 libevent_pthreads.la
lrwxrwxrwx. 1 root root 30 4月 5 22:05 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx. 1 root root 21 4月 5 22:05 libevent.so -> libevent-2.0.so.5.1.9

安装memcached

[root@zzh ~]# mkdir memcached
[root@zzh ~]# tar -zvxf memcached-1.4.7.tar.gz
[root@zzh ~]# cd memcached-1.4.7/
[root@zzh memcached-1.4.7]# ./configure --prefix=/root/memcached --with-libevent=/root/libevent
[root@zzh memcached-1.4.7]# make
[root@zzh memcached-1.4.7]# make install

启动memcached

[root@zzh ~]# cd memcached/bin/
[root@zzh bin]# memcached -d -m 10 -u root -l 10.10.195.112 -p 11211 -c 32 -P /tmp/memcached.pid

參数描写叙述:

-d :启动一个守护进程,

-m:分配给Memcache使用的内存数量。单位是MB,默认是64MB,

-u :执行Memcache的用户

-l :监听的服务器IP地址

-p :设置Memcache监听的端口。默认是11211 注:-p(p为小写)

-c :设置最大并发连接数,默认是1024

-P :设置保存Memcache的pid文件 注:-P(P为大写)

-f :块大小增长因子。默认是1.25

-n :最小分配空间。key+value+flags默认是48

-h :显示帮助

查看memcached进程

[root@zzh bin]# ps -aux | grep memcached
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 13904 0.0 0.0 326708 912 ? Ssl 22:26 0:00 ./memcached -d -m 10 -u root -l 10.10.195.112 -p 11211 -c 32 -P /tmp/memcached.pid
root 13912 0.0 0.0 103248 852 pts/2 S+ 22:27 0:00 grep memcached

关闭memcached进程(採用kill pid的命令)

[root@zzh bin]# kill `cat /tmp/memcached.pid`

或者

[root@zzh bin]# kill 13904

连接memcached

telnet 127.0.0.1 11211

 memcache有一些经常使用的命令:set, add, replace,append, preappend, cas, get, incr, decr, delete等,具体能够查阅相关文档。


Javaclient与memcached

 memcached官方提供的Memcached-Java-Client工具包括了对memcached协议的Java封装,使用它能够比較方便地与环城服务端进行通讯。

 案例

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.schooner.MemCached.MemcachedItem; public class MemcachedTest
{
public static void init()
{
String servers[] = {
"10.10.195.112:11211"
};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(servers);
pool.setFailover(true);
pool.setInitConn(10);
pool.setMinConn(5);
pool.setMaxConn(25);
pool.setMaintSleep(30);
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setAliveCheck(true);
pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
pool.initialize();
}
public static void main(String[] args)
{
init();
MemCachedClient mem = new MemCachedClient();
mem.add("UserName", "zzzzh");
mem.prepend("UserName", "hello ");
mem.append("UserName", "!");
System.out.println(mem.get("UserName")); mem.set("key", 1);
System.out.println(mem.get("key"));
MemcachedItem item = mem.gets("key");
mem.cas("key", (Integer)item.getValue()+1,item.getCasUnique());
System.out.println(mem.get("key"));
}
}

输出结果:

hello zzzzh!
1
2

參考资料

1. 《大型分布式站点架构设计与实践》陈康贤著

Memcached安装与配置的更多相关文章

  1. Memcached安装及配置

    一.Memcached介绍 1.Memcached是国外社区网站LiveJournal团队开发,通过缓存数据库查询结果,减少数据库访问次数,从而提高动态web站点性能. 2.官方站点http://me ...

  2. Windows和Linux环境下Memcached安装与配置(转)

    一.memcached安装配置 windows平台安装 1.memcached-1.2.6-win32-bin.zip下载地址: http://code.jellycan.com/memcached/ ...

  3. Linux CentOS 7下Memcached 安装与配置

    前言 本篇文章记录一下Linux CentOS 7中关于Memcached的安装与配置. 安装 安装memcached之前首先需要安装libevent,我这里用的版本是: •libevent-2.0. ...

  4. ubuntu memcached安装与配置

    转载请注明来源:https://www.cnblogs.com/hookjc/ 关于Memcache与memcachedMemcache是项目名,memcached是服务名.让很多初接触的人感觉很是莫 ...

  5. Windows下Memcached安装与配置实例

    环境声明: 服务器: Windows Server 2008r2: Memcached: Memcached 64-bit for Windows(64位) From: http://www.urie ...

  6. Memcached 安装及配置

    下载Memcached.exe 保存到c:\memcached 运行command: 输入 c:\memcached\memcached.exe -d install 回车,安装memcached s ...

  7. windows php下memcache+memcached安装与配置

    环境声明: 服务器:Windows7 64-bit:Memcached:Memcached 64-bit for Windows(64位) 安装过程 解压刚刚下载的压缩包,得到两个文件:memcach ...

  8. Memcached安装配置及访问

    1.Memcached键值对访问,对于网页来说,key需要使用uri. 2.Memcached的相关配置 memcached:缓存服务器,但本身无法决定缓存任何数据 一半依赖于客户端,一半依赖于服务端 ...

  9. linux下安装及配置和启动memcached

    一.下载文件: 下载memcached和libevent,放到/hom/zwl/目录下 # wget http://www.danga.com/memcached/dist/memcached-1.2 ...

随机推荐

  1. C 常量的类型

    http://bbs.csdn.net/topics/380028485 整型常量的类型是下列相应表中第一个能表示其值的类型: int --> long int --> long long ...

  2. 在Powerdesigner中创建概念数据模型

    点击菜单“File”---->“New Model” 点击[OK]按钮后,将进入如下的画面 系统将出现一个工具栏如下,用于在设计面板中设计模型

  3. 1、Cocos2dx 3.0游戏开发找小三之前言篇

    尊重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 前言 Cocos2d-x 是一个通用 ...

  4. jQuery 二级菜单,一次显示一个小类 鼠标点击显示小类

    jQuery 二级菜单,一次显示一个小类 鼠标点击显示小类 本例有另外2个关联案例,演示地址分别为2.php,3.php 演示 XML/HTML Code <div class="ar ...

  5. 为DropDownListFor设置选中项

    在MVC中,当涉及到强类型编辑页,如果有select元素,需要根据当前Model的某个属性值,让Select的某项选中.本篇只整理思路,不涉及完整代码. □ 思路 往前台视图传的类型是List< ...

  6. Spring3.1.2与Hibernate4.1.8整合

    整合Spring3.1.2 与 Hibernate 4.1.8 首先准备整合jar: Spring3.1.2: org.springframework.aop-3.1.2.RELEASE.jar or ...

  7. SharePoint 2013 升级

    原文地址:https://www.nothingbutsharepoint.com/sites/devwiki/articles/Pages/SharePoint-2013-Upgrade.aspx ...

  8. POJ 1753 Flip Game (枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26492   Accepted: 11422 Descr ...

  9. Same Tree leetcode java

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  10. 未知高度的图片在div设置垂直居中

    方法一: 该方法是将外部容器的显示模式设置成display:table,img标签外部再嵌套一个span标签,并设置span的显示模式为display:table-cell,这样就可以很方便的使用ve ...