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. datagrid在MVC中的运用02-结合搜索

    本文接着上一篇,来体验给datagrid加上搜索功能.主要涉及到: ※ 把一个div与datagrid相关起来 ※ datagrid接收查询参数 ※ 查询参数的封装 效果图: 查询参数封装 分页相关的 ...

  2. mybatis配置文件,注意标签配置顺序。否则报错The content of element type "configuration" must match "(properties?,settings?,...怎么解决

    感谢原作者http://www.cnblogs.com/zhoumingming/p/5417014.html 注意每个标签必须按照顺序写,不然就会提示错误 顺序是 <?xml version= ...

  3. cron表达式举例

    1.quartz定时任务时间设置描述(2011-03-03 16:23:50)转载▼标签: quartz时间it 分类: 凌乱小记 这些星号由左到右按顺序代表 : * * * * * * * 格式: ...

  4. Spring与web MVC的整合——Spring的应用上下文管理

    问题1 如何让web容器加载你的web MVC框架 对于基于servlet的web容器来说,遵循的是servlet规范,入口配置文件是web.xml.这类web容器会在启动的时候会而且仅会加载如下三种 ...

  5. Python 基础学习 总结篇

    Python 基础学习总结 先附上所有的章节: Python学习(一)安装.环境配置及IDE推荐 Python学习(二)Python 简介 Python学习(三)流程控制 Python学习(四)数据结 ...

  6. python各个模块循环引用问题解决办法

    当项目中的模块过多,或功能划分不够清晰时会出现循环引用的问题,如下 有两个模块moduleA 和 moduleB: #moduleA from moduleB import b def a(): pr ...

  7. NLP 依存分析

    NLP 依存分析 https://blog.csdn.net/sinat_33741547/article/details/79258045

  8. asm rebalance 原理

    详见原文博客链接地址: asm rebalance 原理

  9. Android Studio体验(二)--创建项目和Genymotion试用

    上周日已经体验了一把Android Studio顺便没事点了点其他功能,不过还是从自己创建项目开始说吧,首先我们要熟悉Android Studio中的Project 和 Module 两个概念.And ...

  10. 【java】jvm查看当前虚拟机堆大小限制

    #############################################################jinfo -flag MaxHeapSize 6461#linux: jav ...