[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定时执行该文件便可