[TOC]
##1)靜態緩存
保存在磁盤上的靜態文件,用PHP生成數據放入靜態文件中,以此減輕數據庫的壓力
###**1)PHP靜態文件緩存類**
```php
<?php
class File {
private $_dir; // 默認路徑
const EXT = '.txt'; // 文件后綴名
public function __construct() {
// dirname() 當前文件目錄
$this->_dir = dirname(__FILE__) . '/files/'; //默認路徑為當前文件下的files下
}
/**
* [cacheData description]
* @Author ZJC
* @DateTime 2017-02-15T10:31:38+0800
* @param [type] $key [緩存文件文件名]
* @param string $value [緩存數據]
* @param string $path [緩存路徑]
* @return [type] [description]
*/
public function cacheData($key, $value = '', $path = '') {
$filename = $this->_dir . $path .$key . self::EXT;
if ($value !== '') { // 將value值寫入緩存
if(is_null($value)) {
return @unlink($filename);
}
// 判斷是否存在文件目錄,如果不存在,則創建
$dir = dirname($filename);
if (!is_dir($dir)) {
mkdir($dir, 0777);
}
// file_put_contents:將一個字符串寫入文件
// int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
// $data部分最好為字符串形式,因為不支持多維數組;(序列化,JSON都可以)
return file_put_contents($filename, json_encode($value)); // c成功返回字節數,失敗返回FALSE
}
if(!is_file($filename)) { // 顯示緩存
return FALSE;
} else {
return json_decode(file_get_contents($filename), true);
}
}
}
```
###**2)調用方法**
```php
<?php
require_once('./file.php');
$data = array(
'id' => 1,
'name' => 'singwa',
'type' => array(4,5,6),
'test' => array(1,45,67=>array(123, 'test')),
);
$file = new File();
// 寫入緩存
if($file->cacheData('index_mk_cache', $data)) {
// var_dump($file->cacheData('index_mk_cache'));exit;
echo "success";
} else {
echo "error";
}
// 查看緩存
if($file->cacheData('index_mk_cache')) {
var_dump($file->cacheData('index_mk_cache'));exit;
echo "success";
} else {
echo "error";
}
// 刪除緩存
if($file->cacheData('index_mk_cache', null)) {
echo "success";
} else {
echo "error";
}
```
##2)Memcache和Redis緩存技術
教程地址:https://linux.cn/article-6719-1.html 、http://www.imooc.com/qadetail/119389
本人文章:http://www.hmoore.net/visionz/redis-memcache/274193
```
Memcache,Redis都需要安裝才能使用;
1. Memcache和Redis都是用來管理數據的(一種緩存服務)
2. 他們數據都是存放在內存里的(獲取速度快)
3. Redis可以定期將數據備份到磁盤(持久化)
4. Memcache只是簡單的key/value緩存
5. Redis不僅僅支持簡單的k/v類型的數據,同時還提供list,set,hash等數據結構的存儲
```
###**如何操作數據**
命令包括以下:
```
1. 開啟redis客戶端
2. 設置緩存值 - set index-mk-cache 數據
3. 獲取緩存數據 - get index-mk-cache
4. 設置過期時間 - setex key 10 'cache'
5. 刪除緩存 - del key
```
**php操作Redis**
```php
1. 按照phpredis擴展
2. php鏈接redis服務-connect(127.0.0.1, 6379)
3. set 設置緩存
4. get 獲取緩存
```
*設置 redis 緩存,文件地址:/var/www/app/setCache.php*
```
$redis = new Redis();
$redis->connect('192.168.2.110', 6379);
// 設置緩存
// $redis->set('vision', 123);
// 設置定期緩存,15秒失效
$redis->setex('vision2', 15, 'heoolshdfksjfl');
```
*獲取 redis 緩存,文件地址:/var/www/app/getCache.php*
```
$redis = new Redis();
$redis->connect('192.168.2.110', 6379);
var_dump($redis->get('vision2'));
```
**php操作Memcache**
```
1. 安裝memcache擴展
2. 鏈接服務-connect('memcache_host', 11211);
3. set 設置緩存
4. get 獲取緩存
```
*設置和獲取 memcache 與 redis 相似,文件地址:/var/www/app/memcache.php*
```
$memcache_obj = new Memcache;
/* connect to memcached server */
$memcache_obj->connect('127.0.0.1', 11211);
/*
設置'var_key'對應值,使用即時壓縮
失效時間為50秒
*/
$memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 20);
echo $memcache_obj->get('var_key');
```
##3)定時任務
學習要點
```
1. 掌握如何設置定時任務常用命令
2. 掌握如何定時運行PHP程序
```
定時任務命令
```
1. 定時任務服務提供crontab命令來設定服務
2. crontab -e // 編輯某個用戶的cron服務
3. crontab -l // 列出某個用戶cron服務的詳細內容
4. crontab -r // 刪除某個用戶的cron服務
```
crontab格式
```
分 小時 日 月 星期 命令
* * * * * *
0-59 0-23 1-31 1-12 0-6 command
注:"*"代表取值范圍內的數組
"/"代表每、比如每分鐘等
*/1 * * * * /usr/bin/php /var/www/html/test.php
```