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. SQLServer存储过程返回值总结

    1.  存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)  用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况  (1)假如通 ...

  2. C#程序集系列11,全局程序集缓存

    全局程序集缓存(GAC:Global Assembly Cache)用来存放可能被多次使用的强名称程序集.当主程序需要加载程序集的时候,优先选择到全局程序集缓存中去找寻需要的程序集. 为什么需要全局程 ...

  3. datagrid在MVC中的运用10-勾选

    本文体验与勾选有关的特性. 需要加载的books.json 展开{ "total": 4, "rows": [ { "productid": ...

  4. php Function split() is deprecated 的解决办法

    原文地址: http://www.cnblogs.com/mfryf/archive/2012/05/31/2527307.html php升级为5.3后,程序会报 Function split() ...

  5. arcgis python添加几何属性

    import arcpy import numpy import math def AddGeometryAttributes(fc, geomProperties, lUnit, aUnit, cs ...

  6. Vi 编辑

    1.vi的基本概念  基本上vi可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下: 1) ...

  7. 【c语言】模拟实现库函数的atof函数

    // 模拟实现库函数的atof函数 #include <stdio.h> #include <string.h> #include <assert.h> #incl ...

  8. C错误异常处理,异常处理

    预处理器标识#error的目的是什么啊? 指令 用途 # 空指令,无任何效果 #include 包含一个源代码文件 #define 定义宏 #undef 取消已定义的宏 #if 如果给定条件为真,则编 ...

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

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

  10. scala 学习笔记二 方法与函数

    1.介绍 Scala 有方法与函数,二者在语义上的区别很小.Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量.换句话来说在类中定义的函数即是方法. Scala 中的方法跟 Java 的 ...