[Laravel] 11 - WEB API : cache & timer

前言


1、資源

Ref: https://www.imooc.com/video/2870php

 

 

2、緩存

緩存:靜態緩存、Memcache、redis緩存html

Ref: [Laravel] 09 - Functional modelslinux

put(), add(), forever(), has(), get(), pull(), forget()
配置文件:[config
/cache.php]

注意,本篇的緩存是php,而非基於laravel。laravel

 

 

 

靜態緩存


1、生成靜態緩存

獲取文件的當前目錄:dirname(__FILE__)redis

目錄操做:is_dir(), mk_dir()json

寫文件:file_put_contents ----> ref: http://www.runoob.com/php/func-filesystem-file-put-contents.html緩存

<?php

class File {
    private $_dir;
    const EXT = '.txt';
    
    public function __construct() {
        $this->_dir = dirname(__FILE__) . '/files/';
    }
public function cacheData($key, $value = '', $cacheTime = 0) { $filename = $this->_dir . $key . self::EXT;
     // 寫!
if($value !== '') {

// 刪! if(is_null($value)) { return @unlink($filename); }

// (1).建立目錄
$dir = dirname($filename); if(!is_dir($dir)) { mkdir($dir, 0777); } $cacheTime = sprintf('%011d', $cacheTime);

// (2).以json字符串的形式寫入到file中
return file_put_contents($filename, $cacheTime . json_encode($value)); }

     --------------------------------------------------------------------------
// 讀!
if(!is_file($filename)) { return FALSE; }
$contents = file_get_contents($filename); $cacheTime = (int)substr($contents, 0 ,11); $value = substr($contents, 11);
if($cacheTime !=0 && ($cacheTime + filemtime($filename) < time())) { unlink($filename); return FALSE; }  
return json_decode($value, true); } } $file = new File(); echo $file->cacheData('test1');  # 使用cacheData

cacheData的另外一個使用:生成了 index_mk_cache.txtide

<?php

require_once('./file.php');

$data = array(
  'id'   => 1, 
  'name' => 'singwa', 
  'type' => array(4,5,6),
  'test' => array(1,45,67 => array(123, 'tsysa'),),
};

----------------------------------------------------
$file = new File(); if($file->cacheData('index_mk_cache', $data)) {   echo "success"; } else {   echo "err"; }

 

 

2、獲取靜態緩存

沒有第二個參數,就表示是」獲取「。函數

$file = new File();
if($file->cacheData('index_mk_cache')) {
  echo "success";
} else {
  echo "err";
}

 

 

3、刪除靜態緩存

刪除文件的函數:工具

unlink($filename);

 

 

 

Memcache、Redis緩存


1、簡介

 

 

 

2、開啓Redis服務

  • 打開redis的端口服務
redis-server 6379.conf

 

  • 進入安裝目錄
cd /wxh/redis-stable/src

 

  • 進入命令行
$ redis-cli
127.0.0.1:6379

 

 

3、終端連接Redis

Set and Get

12sec後超時刪除,del手動刪除

 

 

4、PHP鏈接Redis

Step 01: 安裝phpredis擴展。

Step 02: 

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('singwal', 123);

$redis->setex('singwa2', 15, 'xxxxxx');

 

 Step 03: 

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->get('singwal');
var_dump($redis->get('singwal');

 

 

5、操做 Memcache

方式一

鏈接:memcache_connect()

設置:memcache_set()

獲取:memcache_get()

 

方式二

鏈接:memcache_obj->connect()

設置:memcache_obj->set()

獲取:memcache_obj->get()

 

 

 

定時任務


1、簡介

在LINUX中,週期執行的任務通常由cron這個守護進程來處理[ps -ef|grep cron]。cron讀取一個或多個配置文件,這些配置文件中包含了命令行及其調用時間。
cron的配置文件稱爲「crontab」,是「cron table」的簡寫。

 

  • 定時格式

 

  • 定時例子

 

 

2、Crontab操做  

  • cron服務

cron是一個linux下 的定時執行工具,能夠在無需人工干預的狀況下運行做業。


  service crond start    //啓動服務
  service crond stop     //關閉服務
  service crond restart  //重啓服務
  service crond reload   //從新載入配置
  service crond status   //查看服務狀態 

  

  • 寫定時任務
sudo crontab -e

 

  • 列出定時任務
sudo crontab -l

 

  • 刪掉全部的定時任務
sudo crontab -r

 

  • 示範

利用Crontab定時執行該文件便可

相關文章
相關標籤/搜索