Memcached 可以轻松应对大量同时出现的数据请求,而且它拥有独特的网络结构,在工作机制方面,它还可以在内存中单独开辟新的空间,建立 HashTable,并对 HashTable 进行有效的管理。
Memcache 是这个项目的名称,而 Memcached 是它服务器端的主程序文件名。一个是项目名称,另一个是主程序文件名。
memcached是一种缓存技术,将数据放入内存,从而通过内存访问提速,因为内存是最快的,memcached的主要目的是提速
那么为啥要用memcached???
1 对数据库的高并发读写
2 对海量数据的处理
使用 Memcached 的主要目的是通过自身内存中缓存关系型数据库的查询结果,减少数据库自身被访问的次数,以提高动态 Web 应用的速度,增强网站架构的并发能力和可扩展性。
支持的语言:Perl PHP Python Ruby C# C/C++ Lua
特征:memcached作为高速运行的分布式缓存服务器,具有以下的特点。
1 协议简单
2 基于libevent的事件处理
3 内置内存存储方式
4 memcached不互相通信的分布式
---------------------memcached的机制------------------------
Slab Allocation 机制:整理内存以便重复使用
在默认情况下 Memcached 采用了名为 Slab Allocator 的机制分配、管理内存
Slab Allocator 的基本原理是按照预先规定的大小,将分配的内存分割成特定长度的块,以完全解决内存碎片问题。
Slab Allocation 的原理很简单,就是将分配的内存分割成各种尺寸的块(Chunk)、并把尺寸相同的块分成组(块的集合)
而且,Slab Allocator 还有重复使用已分配的内存的目的。也就是说,分配到的内存不会释放, 而是重复利用。
Slab Allocation 的主要术语如下。
1 Page:分配给 Slab 的内存空间,默认是 1MB。分配给 Slab 之后根据 Slab 的大小拆分成 Chunk。
2 Chunk:用于缓存记录的内存空间。
3 Slab Class:特定大小的 Chunk 的组。
下面,进入配置环节
**************************************************************************************************************
首先,需要准备三台centos 7 版本的虚拟机
192.168.75.168 MySQL
192.168.75.169 memcache
192.168.75.170 web
1 将三台虚拟机的防火墙关闭
systemctl stop firewalld
sed - i 's/enforcing/disabled/g' /etc/selinux/congfig
查看状态
systemctl stop firewalld
getenforce
2
在MySQL上的配置
1 创建memcache用户,密码为Nebula@123
2 给memcache用户修改算法
3 刷新授权列表
1 创建缓存数据库
2 使用缓存数据库
3 创建表
4 添加表记录
5 对memcache用户赋予库testab1的查看权限
create database testab1;
use testab1;
create table test1(id int not null auto_increment,name varchar(20) default null,pri mary key(id)) engine=innodb auto_increment=1 default charset=utf8;
insert into test1(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5');
grant select on testab1.* to memcache@'%';
show grants for memcache@'%';
2 memcache的配置(192.168.75.169)
1 yum安装memcached
yum install gcc gcc-c++ libevent-devel
yum install memcached -y
2 作为前台程序运行:启动memcached
memcached –u root -p 11211 -m 64m -vv
作为后台程序运行:启动memcached
memcached -p 11211 -m 64m –d –u root
memcache的连接
telnet localost 11211
set方法 保存数据
get方法 得到数据
3 web 端 置(192.168.75.170)
systemctl start httpd
yum install httpd php php-gb php-mysql php-memcache -y
1 编辑httpd的文件
vim /var/www/html/index.html
这里是192.168.75.170
到网页上测试
2 vim /var/www/html/index.php 测试PHP
<?php
phpinfo();
?>
出现此页面说明测试成功
3 vim /var/www/html/mysql.php ,测试MySQL
<?php
$link=mysql_connect('192.168.75.168:3306','memcache','Nebula@123');
if($link);
else echo "Fail!!"; echo "<h1>Success!!</h1>";
mysql_close();
?>
出现此页面,说明MySQL数据库可以正常连接,就是设置成功了
4 vim /var/www/html/memcache.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.75.169', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>";
var_dump($get_result);
?>
出现此页面说明memcache连接成功
5 配置session,vim /etc/php.ini,在最后面加上两行代码
扩展:
session.save_handler:设置session的储存方式为memcache。默认以文件方式存取session数据,如果想要使用自定义的处理来存取session数据,比如 memcache方式则修为session.save_handler =memcache
session.save_path:设置session储存的位置,多台memcache用逗号隔开
session.save_handler = memcache
session.save_path ="tcp://192.168.75.169:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
6 vim /var/www/html/memcache1.php,测试memcache的可用性
<?php
session_start();
if (!isset($_SESSION['session_time']))
{
$_SESSION['session_time'] = time();
}
echo "session_time:".$_SESSION['session_time']."<br />";
echo "now_time:".time()."<br />";
echo "session_id:".session_id()."<br />";
?>
7 vim /var/www/html/memcache2.php
<?php
$memcachehost = '192.168.75.169'; //memcached端地址
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from test1 limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
$conn=mysql_connect("192.168.75.168","memcache","Nebula@123"); //数据库节点,地址,用户,密码
mysql_select_db(testab1);
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result))
{
$arr[]=$row;
}
$f = 'mysql';
$memcache->add($key,serialize($arr),0,30);
$data = $arr ;
}
else{
$f = 'memcache';
$data_mem=$memcache->get($key);
$data = unserialize($data_mem);
}
echo $f;
echo "<br>";
echo "$key";
echo "<br>";
//print_r($data);
foreach($data as $a)
{
echo "number is <b><font color=#FF0000>$a[id]</font></b>";
echo "<br>";
echo "name is <b><font color=#FF0000>$a[name]</font></b>";
echo "<br>";
}
?>
1分钟后,mysql变成memcache,说明访问的是缓存数据库
此时,memcache服务器就配置好了