-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* okvCache is php file caching. | ||
* | ||
* @license GPL v.3 | ||
* @author vee w. | ||
* @link http://www.okvee.net | ||
*/ | ||
|
||
|
||
class okvcache { | ||
|
||
|
||
public $cache_path; | ||
|
||
|
||
function __construct() { | ||
// set cache path | ||
$this->cache_path = dirname(__FILE__).'/cache/'; | ||
// check if folder is not exists. | ||
if ( !file_exists( $this->cache_path ) ) { | ||
mkdir( $this->cache_path ); | ||
} | ||
}// __construct | ||
|
||
|
||
/** | ||
* clear all cache | ||
* @return boolean | ||
*/ | ||
function clear() { | ||
$scd = scandir( $this->cache_path ); | ||
if ( is_array( $scd ) ) { | ||
foreach ( $scd as $file ) { | ||
if ( $file != '.' && $file != '..' && $file != 'index.html' && $file != '.htaccess' ) { | ||
unlink( $this->cache_path.$file ); | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
}// clean | ||
|
||
|
||
/** | ||
* delete a single cache | ||
* @param string $id | ||
* @return boolean | ||
*/ | ||
function delete( $id = '' ) { | ||
if ( file_exists( $this->cache_path.md5( $id ) ) ) { | ||
unlink( $this->cache_path.md5( $id ) ); | ||
} | ||
return true; | ||
}// delete | ||
|
||
|
||
/** | ||
* get cached | ||
* @param string $id | ||
* @return boolean | ||
*/ | ||
function get( $id = '' ) { | ||
// check file exists | ||
if ( !file_exists( $this->cache_path.md5( $id ) ) ) { | ||
return false; | ||
} | ||
// open cached | ||
$fp = fopen( $this->cache_path.md5( $id ), 'r' ); | ||
if ( $fp === false ) { | ||
// no cached, something error like permission error | ||
return false; | ||
} | ||
$content = fread( $fp, filesize( $this->cache_path.md5( $id ) ) ); | ||
fclose( $fp ); | ||
// unserialize values | ||
$content = unserialize( $content ); | ||
if ( !isset( $content['data'] ) && !isset( $content['cache_expire'] ) ) { | ||
return false; | ||
} | ||
// check expire cache? | ||
$file_date = filemtime( $this->cache_path.md5( $id ) ); | ||
$now = time(); | ||
if ( ( $now-$file_date ) > $content['cache_expire'] ) { | ||
// expired | ||
$this->delete( $id ); | ||
return false; | ||
} | ||
// | ||
return $content['data']; | ||
}// get | ||
|
||
|
||
/** | ||
* save cache | ||
* @param string $id | ||
* @param mixed $data | ||
* @param integer $ttl | ||
* @return boolean | ||
*/ | ||
function save( $id = '', $data = '', $ttl = 60 ) { | ||
$fp = fopen( $this->cache_path.md5( $id ), 'w+' ); | ||
fwrite( $fp, serialize( array( 'data' => $data, 'cache_expire' => $ttl ) ) ); | ||
fclose( $fp ); | ||
return true; | ||
}// save | ||
|
||
|
||
} | ||
|
||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>test okvCache</title> | ||
</head> | ||
|
||
<body> | ||
|
||
|
||
<?php | ||
/** | ||
* ตัวอย่างการใช้งานในรูปแบบต่างๆ | ||
* | ||
* @license GPL v.3 | ||
* @author vee w. | ||
* @link http://www.okvee.net | ||
* | ||
* การใช้งาน | ||
* include ( 'okvcache.php' ); ทำการ include file okvcache.php เข้ามาก่อน | ||
* $cache = new okvcache(); กำหนด new object | ||
* จากนั้นทำตามตัวอย่างในแบบต่างๆด้านล่าง โดยการกำหนดค่า save() จะมี parameter 3 ค่า คือ id(string), data(mixed), ttl(integer) | ||
* ซึ่ง ttl คือจำนวนวินาทีที่จะหมดอายุ ค่าเดิมคือ 60 วินาที สามารถกำหนดเพิ่มได้ตามต้องการ | ||
* ตัวอย่างการลบ cache อยู่ด้านล่างของหน้า. | ||
*/ | ||
|
||
|
||
include( 'okvcache.php' ); | ||
|
||
// start new class | ||
$cache = new okvcache(); | ||
|
||
/* | ||
* ทดสอบ string cache | ||
*/ | ||
echo '<h3>string cache</h3>'; | ||
if ( false === $val = $cache->get( 'string-cache' ) ) { | ||
$data = 'this is string text.'; | ||
$val = $data; | ||
$cache->save( 'string-cache', $data ); | ||
} else { | ||
echo '<strong>cached:</strong> '; | ||
} | ||
echo $val; | ||
|
||
|
||
echo '<hr />'; | ||
###################################################### | ||
|
||
|
||
/* | ||
* ทดสอบ array cache | ||
*/ | ||
echo '<h3>array cache</h3>'; | ||
if ( false === $val = $cache->get( 'array-cache' ) ) { | ||
$data = array( 'apple', 'banana', 'mango' ); | ||
$val = $data; | ||
$cache->save( 'array-cache', $data ); | ||
} else { | ||
echo '<strong>cached:</strong> '; | ||
} | ||
print_r( $val ); | ||
|
||
|
||
echo '<hr />'; | ||
###################################################### | ||
|
||
|
||
/* | ||
* ทดสอบ object cache | ||
*/ | ||
echo '<h3>object cache</h3>'; | ||
if ( false === $val = $cache->get( 'obj-cache' ) ) { | ||
$data = new stdClass(); | ||
$data->fruit = array( 'apple', 'banana', 'mango' ); | ||
$data->text = 'this is string text.'; | ||
$val = $data; | ||
$cache->save( 'obj-cache', $data ); | ||
} else { | ||
echo '<strong>cached:</strong> '; | ||
} | ||
print_r( $val ); | ||
|
||
|
||
echo '<hr />'; | ||
###################################################### | ||
|
||
|
||
/* | ||
* ทดสอบ mixed cache | ||
*/ | ||
echo '<h3>mixed cache</h3>'; | ||
if ( false === $val = $cache->get( 'mixed-cache' ) ) { | ||
$data1 = array( 'apple', 'banana', 'mango' ); | ||
$data2 = new stdClass(); | ||
$data2->cars = array( 'mercedes', 'chevrolet', 'toyota' ); | ||
$data2->text = 'this is string text.'; | ||
$data2->boolean = false; | ||
$data = array_merge( $data1, array( $data2 ) ); | ||
$val = $data; | ||
$cache->save( 'mixed-cache', $data ); | ||
} else { | ||
echo '<strong>cached:</strong> '; | ||
} | ||
print_r( $val ); | ||
|
||
|
||
echo '<hr />'; | ||
###################################################### | ||
|
||
|
||
/* | ||
* ทดสอบ boolean cache | ||
*/ | ||
echo '<h3>boolean cache</h3>'; | ||
if ( false === $val = $cache->get( 'bool-cache' ) ) { | ||
$data = true; | ||
$val = $data; | ||
$cache->save( 'bool-cache', $data ); | ||
} else { | ||
echo '<strong>cached:</strong> '; | ||
} | ||
var_dump( $val ); | ||
|
||
|
||
echo '<hr />'; | ||
###################################################### | ||
|
||
|
||
// end, clear variables (not required) | ||
unset( $cache, $data, $data1, $data2, $val ); | ||
|
||
?> | ||
<h3>การลบแคช 1 แคช</h3> | ||
<p>ใช้ <code>$cache->delete( 'cache-id(string)' );</code></p> | ||
|
||
|
||
<h3>การล้างแคชทั้งหมด</h3> | ||
<p>ใช้ <code>$cache->clear();</code></p> | ||
|
||
|
||
</body> | ||
</html> |