Memcached简要理解

来源:互联网 发布:淘宝服装店铺页头图片 编辑:程序博客网 时间:2024/05/17 07:50

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

什么是Memcached的?

免费和开源的,高性能的,分布式内存对象缓存系统,通用性的,但拟用于减少数据库负载,加快动态Web应用程序。

Memcached是从数据库调用,API调用,或网页渲染的结果任意数据的小块(字符串,对象)在内存中的键值存储。

Memcached是简单而强大的。其简单的设计加强了快速部署,易于开发,解决面临的大量数据高速缓存很多问题。它的API可用于最流行 ​​的语言。

许多Web 应用程序都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示。但随着数据量的增大,访问的集中,就会出现REBMS的负担加重,数据库响应恶化,网站显示延迟等重大影响。Memcached是高性能的分布式内存缓存服务器。一般的使用目的是通过缓存数据库查询结果,减少数据库的访问次数,以提高动态Web 应用的速度、提高扩展性。

linux上测试操作步奏

在腾讯云,阿里云或者UC云获得一台CentOS的云服务器,登陆账户后可以进行Memcached的安装了

Linux系统安装memcached,首先要先安装libevent库,期间注意操作提示选择y(yes/No)。

$ yum install libevent libevent-deve (自动下载安装 - - Redhat/Fedora/Centos - - )

$ yum install memcached (自动安装Memcached)

Memcached命令的运行:

$ memcached -h (命令帮助)

启动选项:

-p <num>      TCP port number to listen on (default: 11211)(设置Memcache监听的端口,最好是1024以上的端口)-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)(监听的服务器IP地址,可以有多个地址)-d            run as a daemon(启动一个守护进程)-r            maximize core file limit-u <username> assume identity of <username> (only when run as root)(运行Memcache的用户)-m <num>      max memory to use for items in megabytes (default: 64 MB)(分配给Memcache使用的内存数量)-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(设置保存Memcache的pid文件)-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)

1)作为前台程序运行:

从终端输入以下命令,启动memcached:
$ memcached -p 11211 -m 64m -vv

这里显示了调试信息。这样就在前台启动了memcached,监听TCP端口11211,最大内存使用量为64M。调试信息的内容大部分是关于存储的信息。

2)作为后台服务程序运行:

$ memcached -p 11211 -m 64m -d

或者

$ memcached -d -m 64M -u root -l 10.104.171.65 -p 11211 -c 256 -P /tmp/memcached.pid

(安装Telnet服务)

$ yum install telnet -y

$ yum install telnet-server -y

云服务器的linux系统很多都没有装这个服务,因为默认是ssh登陆的,telnet并不安全。

现在可以存东西取东西啦

set runoob 0 900 9memcached
get runoob

输出信息说明:
STORED:保存成功后输出。
ERROR:在保持失败后输出。

0 0
原创粉丝点击