php-memcache

一、memcache概述

memcache=memory+cache(内存,缓存):是一个高性能的分布式的内存对象缓存系统。通过在内存里维护一个巨大的hash表。 key=value

Hash表

key value
mystr  "abc"
myarr  Array("aa","cc")
object  Object值

图像、视频、数据库检索结果都可以。被很多大负载网站使用,分担数据库压力。

它是一个软件(服务软件)c/s软件,用来维护内存的,将数据在内存中使用,减少I/O输入输出。这个软件简洁高效,大概150k,是一个开源系统。

它也会监听IP地址,和端口。

二、Memcache工作原理

Memcache 软件, memcached(进程)

就像http ,有  httpd进程

vsftp,有   vsftpd

c/s软件  

memcached是以守护程序方式运行于一个或多个服务器中,随时会接收客户端的连接和操作。

客户端使用各种语言去编写 ,例如:PHP/java/c/c++/perl/python/ruby

客户端去连接Memcache之后,通过键值,获取值。保存在Memcache的数据并不是永久的,Memcache服务器关闭后,会丢失,因为数据是在内存中的。

用C语言(libevent库)实现的,基于事件的服务。

三、为什么要在WEB中使用Memcache

 大型网站,会吃不消。需要Memcache提高网站的访问速度。

四、安装Memcache服务器(LinuxWindow上分别安装)

基于libevent事件-跨平台的处理事件的一个封装。

Linux

  安装libevent

  ./configure –with-libevent=/usr

  Make && make install

  安装memcached

  ./configure –with-libevent=/usr

   Make && make install

  启动:Memcahced –d –m 128 –l 192.168.1.111 –p 11211 –u root

  -u 以root用户启动

  -l 以192.168.1.111的11211端口监听

  停止: kill `cat /tmp/memcached.pid`;

  Killall  memcached

Windows

  Memcahced.exe  -d  install [uninstall]

  Memcached.exe –d  -m 50 –l 127.0.0.1  -p 11211 start

五、Memcached服务器的管理(启动)

-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB ,最大好像2G
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

六、操作Memcached (命令行方式telnet作为客户端)

telnet localhost 11211

常用命令

Command

Description

Example

get

Reads a value

get mykey

set

Set a key unconditionally

set mykey 0 60 5

add

Add a new key

add newkey 0 60 5

replace

Overwrite existing key

replace key 0 60 5

append

Append data to existing key

append key 0 60 15

prepend

Prepend data to existing key

prepend key 0 60 15

incr

Increments numerical key value by given number

incr mykey 2

decr

Decrements numerical key value by given number

decr mykey 5

delete

Deletes an existing key

delete mykey

flush_all

Invalidate specific items immediately

flush_all

Invalidate all items in n seconds

flush_all 900

stats

Prints general statistics

stats

Prints memory statistics

stats slabs

Prints memory statistics

stats malloc

Print higher level allocation statistics

stats items

stats detail

stats sizes

Resets statistics

stats reset

version

Prints server version.

version

verbosity

Increases log level

verbosity

quit

Terminate telnet session

quit

原文地址:https://www.cnblogs.com/nana135/p/6430391.html