Skip to content

Instantly share code, notes, and snippets.

@bzz0217
Last active December 26, 2019 01:31
Show Gist options
  • Save bzz0217/430f0e7ce81e24f7ceae to your computer and use it in GitHub Desktop.
Save bzz0217/430f0e7ce81e24f7ceae to your computer and use it in GitHub Desktop.
簡易キャッシュ
<?php
class cache{
public static $config = [
'filepath' => '.cache',
'expire_min' => '60'
];
private $modified_datetime;
private $expiration_time_flg;
public function __construct() {
if (self::$config['filepath']){
if(file_exists(self::$config['filepath'])){
$this->setModifiedTime();
$this->expiration_time_flg = $this->expire_check();
}
}
}
/*
* キャッシュファイルの有効期限チェック
*/
public function expire_check(){
if(($this->modified_datetime + self::$config['expire_min'] * 60) > time()){
//有効期限内
return true;
}else{
//有効期限超過
return false;
}
}
/*
* キャッシュファイルの最終更新日時取得
*/
public function setModifiedTime(){
if (file_exists(self::$config['filepath'])){
clearstatcache(false, self::$config['filepath']);
$this->modified_datetime = filemtime(self::$config['filepath']);
}
}
/*
* キャッシュセット
*/
public function setCache($cacheData){
@file_put_contents(self::$config['filepath'], $cacheData);
$this->setModifiedTime();
}
/*
* キャッシュ取得
* @return string cache
*/
public function getCache(){
$cache = '';
if (file_exists(self::$config['filepath'])) {
if ($this->expiration_time_flg){
//有効期限内チェック
$cache = @file_get_contents(self::$config['filepath']);
}
}
return $cache;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment