:first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

在一个高并发的web应用中,数据库存取瓶颈一直是个大问题,一旦达到某个极限,数据库很容易崩溃,但是如果我们把常用的数据放到内存中,在需要的时候从内存中取,不光读取速度快,而且节约数据库IO。


memcache简介

Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。

memcache的mem是内存(memory),cache是缓存,结合是内存缓存的意思。我们应用memcache时,读取数据先从memcache内读取,若查找不到再去数据库里查找,并将数据存入memcache,待下次查找时便能轻易找到。

需要注意:

  • memcache是内存型的数据库 ,因为内存的关闭释放的特性,memcache也无法持久化存储内容;
  • memcache内部是分块存储,所以大于1M的数据也无法存储。
  • memcache依赖libevent库,安装前需确认已经安装了libevent库。
  • memcache是一个轻量级的内存型数据库,只支持key-value型的存储。
  • memcache中没有关于用户,密码的设置,所以在配置时要配置防火墙的端口限制连接,以达到安全的目的。
  • 使用repcached也能轻易实现memcache的单master单slave主从复制。

memcache的应用场景

  • 存储大量不需要持久存储或数据库内已存在不会变动的数据。
  • 读取数据非常频繁数据,要求小于1M。
  • 数据类型简单的key-value型数据。
  • 计算好的结果和渲染后的网页模板文件。
  • 因其原子递增性,可以用来计数。
  • 因为可以设置数据过期时间的特性,存储期限数据。不过需要注意,memcache会在分配的内存不足时以最近最少使用原则(LRU)重用内存,可能会导致信息提前被删除。
  • 用memcache存储session信息,以达到多服务器session共享。需要配置:php.ini:

session.save_handler = memcache //设置session的储存方式为memcache
memcache.hash_strategy = "consistent"//设置memcache的hash算法为一致性哈希算法。
session.save_path = "tcp:/ip:port" //设置session储存的位置,多台memcache用逗号隔开。

memcache服务器的安装

memcache的安装简单,服务器可以在其官网http://www.memcached.org/下载,解压后在其目录下运行./configure -prefix=/path编译,然后make / make test / make install 得到可以直接执行的二进制文件。

使用./memcached命令即可开启服务器,其常用参数如下:

  • -p port 监听端口 (默认: 11211)
  • -d 以后台方式运行Memcached
  • -u username 运行Memcached的账户
  • -m n 最大的内存使用, 单位是MB,默认 64 MB
  • -c connections 最大连接数量, 默认是 1024

memcache的常用命令

用memcache客户端或telnet连接到memcache后,就可以对memcache进行操作了。

memcache数据结构简单,所以命令行命令也很少,下面以一条常用命令来简析一下命令格式:

add key flags expire_time length \r\n value

flags:是否压缩/序列化,通常为0。

expire_time:从存储后多久过期。以秒(s)为单位,最大为30天的长度,超过30天的长度被视为时间戳表示"到什么时候过期",若设为0表示永不过期。

length:value长度,输入长度回车之后,命令行会读取你接下来输入的length个字符。

set key flags expire_time length    //如果有值则覆盖原值,没有则新增,add在有值时会存储失败
get key //获取key的值
replace key flags expire_time length// 替换一个已存在的key
append/preappend key flags expire_time length// 给key的value后面/前面添加新内容。
preappend key flags expire_time length // 给key的value前面添加新内容。
inc/dec key [n] //key的值递增/递减1/[n]
delete key //删除一个key
flush_all [n] //[在n秒后]删除全部数据
stats [options] //获取memcache[有关某一项]的详细信息

PHP的memcache扩展及应用

在https://pecl.php.net/index.php搜索获取到所需的memcache扩展包。

linux系统,直接挑选版本(推荐最新stable稳定版)下载,解压后在解压目录下用phpize工具产生configure文件,并用它安装,安装完成后在/php.ini中添加extension。具体可看我的博文linux下的PHP中的最后一节

windows下要点击链接右边的“windows logo DLL”链接,并在新打开的页面中,按照版本、32位/64位、线程安全/非线程安全来选择自己所需要的扩展,具体选项可以在phpinfo();页面看到。下载完成后,将其放入phppath/ext/目录下,然后在php.ini中添加extension=php_memcacache.dll;重启服务器完成安装。

在phpinfo()页面中看到memcache扩展后,说明安装成功,我们就可以在php脚本中使用关于memcache的类函数库了。

在手册中我们可以找到许多关于php的memcache扩展的使用,以下是一个典型的memcache使用流程。

$m=new Memcache();
$m->connect($host,$port);
$m->add($key,$value[,flags,$expire_time]);
$content=$m->get($key);
$m->close();

这是一个简单的memcache连接程序,在进行memcache分布式存储时,还需要用到$memcache->addServer()向memcache集群中添加服务器。

此外,还有get(),set(),flush(),delete()等方法,用法都大同小异,在手册上也能找得到,而且十分清晰。说到手册,推荐一个我正在用的,很不错。地址:http://pan.baidu.com/s/1mgCkvIo

如果您觉得本博文对您有帮助,您可以推荐或关注我,如果您有什么问题,可以在下方留言讨论,谢谢。

最新文章

  1. PP常用bapi
  2. :first与:first-child的区别
  3. Pair project(刘昊岩11061156 黄明源11061186)
  4. [Forward]Use the SharePoint My Tasks Web Part outside of My Sites
  5. 【转】BLE开发的各种坑
  6. NSFileHandle编写json数据格式
  7. SVN 让项目某些文件不受版本控制
  8. java线程例子登山
  9. display flex 和a标签不行
  10. [maven] 新建项目一直提示loading archetype list
  11. iOS切圆角的几个方法
  12. 2017-07-06(grep man apropos )
  13. <url-pattern>/</url-pattern>和<url-pattern>/*</url-pattern>区别
  14. git命令 高级
  15. CG-ctf WP
  16. 超级有用的15个mysqlbinlog命令
  17. Kay and Snowflake CodeForces - 685B (重心, 好题)
  18. pyDes介绍
  19. 配置postgres9.3间的fdw——实现不同postgres数据库间的互访问
  20. iOS:WKWebView(19-01-31更)

热门文章

  1. -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法
  2. 提高(Android)开发效率的工具与网站
  3. CSS系列:CSS常用样式
  4. Android开发之基于AndroidStudio环境搭建和工程创建
  5. Vertica增加一个数据存储的目录
  6. javaScript笔记
  7. 3.JAVA之GUI编程Frame窗口
  8. Wizard Framework:一个自己开发的基于Windows Forms的向导开发框架
  9. Bloom Filter:海量数据的HashSet
  10. LinqToDB 源码分析——轻谈Linq查询