您的位置:首页 > 数据库 > Memcache

memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用

2017-07-09 15:13 871 查看
这个是官网:

www.memcached.org

打开,然后点击:

LiveJournal

这个公司,这个公司在发展过程中,

发现数据越来越大,于是他的下面的一个员工

实际上是他的一个子公司www.danga.com,就开发了memcached

这样一个软件.这个公司已经被收购了.

-------------------------

可以去看看:http://www.danga.com/

这个公司除了提供

Some of the software born at Danga:

memcached [contributing]
Distributed caching. Hit your database less. Speed up your website.

MogileFS [contributing]
Distributed (meta) file system. Spray files across cheap disks on your network. Pay less for storage. No proprietary on-disk file formats.

Perlbal [contributing]
Sexy reverse proxy load balancer and webserver, full of tricks.

DJabberd [contributing]
High-performance, low-overhead, everything-is-a-plugin extensible Jabber/XMPP server.

Gearman [contributing]
Low-latency remote function execution. Run code remotely, in parallel, load balanced, in other languages, etc.

OpenID [contributing]
Decentralized identity.

可以看到还有其他项目对吧.

----------------------------

好,咱们去

www.memcached.org

看看他的介绍:

What is Memcached?

Free & open source, high-performance, distributed memory object caching system,

1.1 memcached  是什么?

free & open source, high-performance, distributed memory object caching system

自由&开放源码, 高性能 ,分布式的内存对象缓存系统

由 livejounal 旗下的 danga 公司开发的老牌 nosql 应用.

------------------------------

1.2  什么是 NoSQL?

nosql,指的是非关系型的数据库。

关系型数据库是一行一行的存储对吧.

相对于传统关系型数据库的"行与列",NoSQL 的鲜明特点为 k-v 存储(memcached,redis),

或基于文档存储(mongodb)

注:

nosql -- not only sql , 不仅仅是关系型数据库,

显著特点: key-value 键值对存储,如 memcached, redis,

或基于文档存储 如,mongodb

--------------------------------

1.3  谁在用 memcached?

Facebook,赶集,新浪,搜虎等等..开心网等等

-----------------

memcached  基本使用

首先去下载:

去官网:
http://www.memcached.org/
可以看到提供的都是Linux版本的,需要大家在Linux环境

下编译,使用:

2.1 linux  下编译 memcached

2.1.1: 准备编译环境

在 linux 编译,需要 gcc,make,cmake,autoconf,libtool 等工具,

这几件工具, 以后还要编译 redis 等使用,所以请先装.

在 linux 系统联网后,用如下命令安装

#yum install gcc make cmake autoconf libtool

2.1.2:  编译 memcached

memcached 依赖于 libevent 库,因此我们需要先安装 libevent.

分别到 libevent.org 和 memcached.org 下载最新的 stable 版本(稳定版).

先编译 libevent ,再编译 memcached,

编译 memcached 时要指定 libevent 的路径.

过程如下: 假设源码在/usr/local/src 下, 安装在/usr/local 下

# tar zxvf libevent-2.0.21-stable.tar.gz

# cd libevent-2.0.21-stable

# ./configure --prefix=/usr/local/libevent

# 如果出错,读报错信息,查看原因,一般是缺少库

# make && make install

# tar zxvf memcached-1.4.5.tag.gz

# cd memcached-1.4.5

#./configure--prefix=/usr/local/memcached \

--with-libevent=/usr/local/libevent

# make && make install

注意: 在虚拟机下练习编译,一个容易碰到的问题---虚拟机的时间不对,

导致的 gcc 编译过程中,检测时间通不过,一直处于编译过程.

解决:

# date -s ‘yyyy-mm-dd hh:mm:ss’

# clock -w # 把时间写入 cmos

----------------------------------

好,这里咱们可以先使用二进制版本.

也就是windows下的版本对吧.

好,咱们可以在网上找到,windows版本,

memcached.exe

memcached-win64-1.4.4-14.zip

解压,找到memcached.exe,libgcc_s_sjlj-1.dll,

,pthreadGC2.dll直接放到一个比如:

E:\mem

文件夹下.

-------------------------------

好,看看如何使用:

打开cmd:

C:\Users\Administrator>e:

E:\>cd mem

E:\mem>dir

 驱动器 E 中的卷是 办公

 卷的序列号是 0007-2E19

 E:\mem 的目录

2017/06/04 周日  18:13    <DIR>          .

2017/06/04 周日  18:13    <DIR>          ..

2009/12/17 周四  00:47           507,640 memcached.exe

               1 个文件        507,640 字节

               2 个目录 25,952,534,528 可用字节

E:\mem>memcached.exe -h

E:\mem>

查看帮助:

E:\mem>memcached.exe -h

memcached 1.4.4-14-g9c660c0

-p <num>      TCP port number to listen on (default: 11211)

-U <num>      UDP port number to listen on (default: 11211, 0 is off)

-s <file>     UNIX socket path to listen on (disables network support)

-a <mask>     access mask for UNIX socket, in octal (default: 0700)

-l <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses)

-s <file>     unix socket path to listen on (disables network support)

-a <mask>     access mask for unix socket, in octal (default 0700)

-l <ip_addr>  interface to listen on, default is INADDR_ANY

-d start          tell memcached to start

-d restart        tell running memcached to do a graceful restart

-d stop|shutdown  tell running memcached to shutdown

-d install        install memcached service

-d uninstall      uninstall memcached service

-r            maximize core file limit

-u <username> assume identity of <username> (only when run as root)

-m <num>      max memory to use for items in megabytes (default: 64 MB)

-M            return error on memory exhausted (rather than removing items)

-c <num>      max simultaneous connections (default: 1024)

-k            lock down all paged memory.  Note that there is a

              limit on how much memory you may lock.  Trying to

              allocate more than that would fail, so be sure you

              set the limit correctly for the user you started

              the daemon with (not for -u <username> user;

              under sh this is done with 'ulimit -S -l NUM_KB').

-v            verbose (print errors/warnings while in event loop)

-vv           very verbose (also print client commands/reponses)

-vvv          extremely verbose (also print internal state transitions)

-h            print this help and exit

-i            print memcached and libevent license

-P <file>     save PID in <file>, only used with -d option

-f <factor>   chunk size growth factor (default: 1.25)

-n <bytes>    minimum space allocated for key+value+flags (default: 48)

-L            Try to use large memory pages (if available). Increasing

              the memory page size could reduce the number of TLB misses

              and improve the performance. In order to get large pages

              from the OS, memcached will allocate the total item-cache

              in one large chunk.

-D <char>     Use <char> as the delimiter between key prefixes and IDs.

              This is used for per-prefix stats reporting. The default is

              ":" (colon). If this option is specified, stats collection

              is turned on automatically; if not, then it may be turned on

              by sending the "stats detail on" command to the server.

-t <num>      number of threads to use (default: 4)

-R            Maximum number of requests per event, limits the number of

              requests process for a given connection to prevent

              starvation (default: 20)

-C            Disable use of CAS

-b            Set the backlog queue limit (default: 1024)

-B            Binding protocol - one of ascii, binary, or auto (default)

-I            Override the size of each slab page. Adjusts max item size

              (default: 1mb, min: 1k, max: 128m)

E:\mem>

----------------------------------

介绍一下,各个参数的作用:

-p <num> tcp port number to listen on (default: 11211) 

// 监听的端口,默认是11211

-u <num> udp port number to listen on (default: 0, off)

-s <file> unix socket path to listen on (disables network support)

-a <mask> access mask for unix socket, in octal (default 0700)

-l <ip_addr> interface to listen on, default is indrr_any

-d start tell memcached to start

-d restart tell running memcached to do a graceful restart

-d stop|shutdown tell running memcached to shutdown

-d install install memcached service 

// 把 memcached 注册成服务

-d uninstall uninstall memcached service

-r maximize core file limit

-u <username> assume identity of <username> (only when run as root)

//-u在Linux的时候会用.windows的时候可以不用理会

-m <num> max memory to use for items in megabytes, default is 64

mb //分配给 memcached 的最大内存

-m return error on memory exhausted (rather than removing

items)

-c <num> max simultaneous connections, default is 1024 

// 最大的连接数,比如用php或java来

//连接,能连接多少啊..

-k lock down all paged memory. note that there is a

limit on how much memory you may lock. trying to

allocate more than that would fail, so be sure you

set the limit correctly for the user you started

the daemon with (not for -u <username> user;

under sh this is done with 'ulimit -s -l num_kb').

-v verbose (print errors/warnings while in event loop) //

输出错误信息

-vv very verbose (also print client commands/reponses) //

输出所有信息

-h print this help and exit

-i print memcached and libevent license

-b run a managed instanced (mnemonic: buckets)

-p <file> save pid in <file>, only used with -d option

-f <factor> chunk size growth factor, default 1.25 

//增长因子这个讲到存储原理的时候再说.

-n <bytes> minimum space allocated for key+value+flags, default 48

--------------------

然后启动:

在 win 下启动

>memcached -m 64 -p 11211 -vvv

分64m内存,走11211端口,然后输出所有信息

-------------------------------------------------

举个例子:

怎么以最快的速度查询学生的成绩.

getFen.php

<?php

  //1. 可以直接查询数据库

  //2. 使用真静态---把对应的学生成绩

  //   放在磁盘上的html页面中

  //3. 直接操作内存

  //第一次我访问数据库,然后讲该学员的成绩

  //直接放入memached

------------------------------

memcached服务维护了一张内存表(hahstable)

key    value

100     90

memcached 的 key一般是 字串, 不能重复

value可以放入(字符串,数值,数组,对象,布尔,

二进制数据[图片/视频])

---------------------------------------

mysql的内存表,mysql可以把表存在内存中

可以把频繁变化的数据,但是丢了也无所谓的

数据放到内存中.

内存表就是key value的,就是咱们说的hashtable

--------------------

介绍:

memcached是一种缓存技术, 他可以把你的数据放入内存,

从而通过内存访问提速,因为内存最快的, memcached技术的

主要目的提速,在memachec 中维护了一张大的hashtable表 ,该表是在内存,表的结构是

key    value

字串  (字串,数值,数组,对象,布尔,二进制数据,null)

-----------------------------------

总结安装过程:

 安装并使用memcached

安装步骤

(1) 下载memcached软件

(2) 安装

进入cmd ,切换到 memcached.exe 文件所在目录

memcached.exe –d install

(3) 启动memcached

第一种,可以到服务点击启动

第二种, 命令行

memcached.exe –m 200MB –d start  

【-m的意思是以deamon方式启动,默认64M】

如果你在启动时,win7启动不成功, 则可以使用如下方法

memcached.exe –p 端口号

启动方法不要关闭控制台.

端口号的范围 : 0-65535  , 因为端口号是用两个字节来

表示有名端口: 0-1024 已经用程序使用 , apache 80  ,

mysql 3306 , ftp 21 , ssh 22

oracle: 1521, stmp: 25 

使用netstat –an 如果看到 11211端口在监听,说明启动ok

netstat –anb 可以看这个端口是哪个程序监听, 这个指令

还可以看到有哪些用户连接到我们的服务器.

这样 netstat –anb 192.168.1.100

可以看到哪些电脑连接到了192.168.1.100

------------------------------------------

如果没有安装好,原因可能

1. 如果你是win7, win7对安全性高,所有,

必须以adminstartor 身份来安装.你切换成adminstrator , 

去安装,在启动

2.你的memcached.exe 目录有中文,或特殊字符, 保证目录

没有中文和特殊字符.

---------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐