This example summarizes the common PHP data file cache classes. Share to everyone for your reference. The specific analysis is as follows:
The practice of data file caching we often have PHP file caching and using Memcache to cache data, below I summarized the memcache cache data and data file caching respectively. Interested friends can refer to.
1. For the general variable, the variable into a PHP language format, written to the file, as long as the include this file is equivalent to loading the cache.
2. For array-type variables, the array is converted to the PHP language definition array string, written to the file, as long as the include is equivalent to loading the cache.
3. Cached cache time control, by getting the cache file creation time and current time to compare, if not to update the time, direct read cache, if the update time, query the database.
File cache class with the following code:
Copy Code code as follows:
<?php
Class Datacache
{
/**
* Array Conversion
*
* @param array $array
* @param string $arrayName
* @param array $level
*
* @return String
*/
Private Function Arrayeval ($array, $arrayName = ', $level = 0)
{
$space = Str_repeat ("T", $level);
if (Emptyempty ($arrayName))
{
$evaluate = "Arrayn$space (n");
}
Else
{
$evaluate = "${$arrayName} = Arrayn$space (n";
}
$space 2 = str_repeat ("T", $level + 1);
$comma = $space 2;
if (!emptyempty ($array))
{
foreach ($array as $key => $val)
{
$key = is_string ($key)? ''' . Addcslashes ($key, ' ' \ '). ": $key;
$val =!is_array ($val) && (!preg_match ('/^-?[ 1-9]d*$/', $val) | | strlen ($val) > 12)? ''' . Addcslashes ($val, ' ' \ '). ": $val;
if (Is_array ($val))
{
$evaluate. = "$comma $key =>". Arrayeval ($val, ', $level + 1);
}
Else
{
$evaluate. = "$comma $key => $val";
}
$comma = ", N$space2";
}
}
$evaluate. = "N$space)";
finally need a ";"
if ($level = = 0)
{
$evaluate. = ";";
}
return $evaluate;
}
/**
* Write Cache
*
* @param string $path
* @param string $arrayName
* @param array $data
*
* @return Boolean
*/
public static function Writecache ($path, $arrayName, $data)
{
if ($handle = fopen ($path, ' w+ '))
{
$data = Self::arrayeval ($data, $arrayName);
$dataConvert = "<?PHPN". $data;
Flock ($handle, LOCK_EX);
$rs = fputs ($handle, $dataConvert);
Flock ($handle, lock_un);
Fclose ($handle);
if ($rs!== false)
{
return true;
}
}
return false;
}
}
?>
Call the method, the code is as follows:
Copy Code code as follows:
/**
* Generate file Cache
*
* @param string $filePath The save path to the cached file
* @param string $arrayName The name of the array stored in the cache file
* @param array $data data
*
* @return Boolean
*/
Datacache::writecache ($filePath, $arrayName, $data);
Memcache to cache the data, which provides the class for this file cache, the following code:
Copy Code code as follows:
<?php
/**
* File Cache class
* Provide file caching
*/
Class cache_filecache{
/**
* Set Cache
* @param $key-Cached keyword key
* @param $data Cached content
* @param $cacheLife Cache time (in seconds) if 0 indicates infinite time
* @return Bool
*/
public static function Setcache ($key, $data, $cacheLife)
{
if (file_exists (__site_file_cache))
{
@ $file = __site_file_cache. "/" . $key. ". PHP ";
$cache = Array ();
$time = __sys_time;
$cache [' content '] = $data;
$cache [' expire '] = $cacheLife = = 0? 0: $time + $cacheLife;
$cache [' mtime '] = $time;
$cache = serialize ($cache);
$setReslut = @file_put_contents ($file, $cache) or Self::error (__line__, "File write error");
$chmodReslut = @chmod ($file, 0777) or Self::error (__line__, "Set file permissions failed");
if ($setReslut && $chmodReslut)
{
return true;
}
Else
{
return false;
}
}
}
/**
* Get Cached data
* @param $key-Cached keyword key
* @return Array
*/
public static function GetCache ($key)
{
@ $file = __site_file_cache. "/" . $key. ". PHP ";
if (file_exists ($file))
{
$data = @file_get_contents ($file);
$data = Unserialize ($data);
if ($data [' Expire ']==0 | | $data [' expire '] > __sys_time)
{
return $data [' content '];
}
Else
{
Unlink ($file);
return Array ();
}
}
}
/**
* Delete cached files
* @param $key Cache $key
* @return Bool
*/
public static function Delcache ($key)
{
if (@unlink (__site_file_cache.) /". $key.". PHP))
{
return true;
}
Else
{
return false;
}
}
/**
* Clear All cached files
* @return Bool
*/
public static function Clearallcache ()
{
$files = Scandir (__site_file_cache);
foreach ($files as $val)
{
@unlink (__site_file_cache.) /". $val);
}
}
/**
* Error handling function
* @param $line number of lines
* @param $msg Information
*/
public static function error ($line, $msg)
{
Die ("Error file:. __file__.") /N Error Line: $line/N error message: $msg ");
}
}
?>
I hope this article will help you with the PHP program design based on thinkphp framework.