Summary of common php Data file cache classes

Source: Internet
Author: User
Tags flock
This article mainly introduces the common php Data file cache class. The example describes the php file cache and the method of using memcache to cache data. the code is encapsulated and easy to use. it is a very practical technique, you can refer to the examples in this article to summarize common php Data file cache classes. Share it with you for your reference. The specific analysis is as follows:

Data File Cache practices we commonly use PHP file cache and memcache to cache data. below I will summarize the cache data and data file cache for memcache respectively. For more information, see.

1. for a common variable, convert the variable to the php format and write it to the file. if you include this file, it is equivalent to loading the cache.

2. for array-type variables, convert array to the string defined by the php language array and write it to the file. if you use include, it is equivalent to loading the cache.

3. cache time control: compare the cache file creation time with the current time by obtaining the cache file creation time. if the update time is not reached, read the cache directly. if the update time is reached, query the database.

File Cache class, the code is as follows:

The code is 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 ";
}

$ Space2 = str_repeat ("t", $ level + 1 );
$ Comma = $ space2;
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 )";

// A ";" is required at the end.
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;
}
}
?>


The code is as follows:

The code is as follows:

/**
* Generate file cache
*
* @ Param string $ filePath: Path to save the cached file
* @ Param string $ array name in the cache file
* @ Param array $ data
*
* @ Return boolean
*/
DataCache: writeCache ($ filePath, $ arrayName, $ data );


Memcache to cache data. Here we provide the file cache class. the code is as follows:

The code is as follows:

<? Php
/**
* File cache class
* File caching
*/
Class Cache_FileCache {

/**
* Set cache
* @ Param $ key the key cached by the key
* @ Param $ data cached content
* @ Param $ cacheLife cache time (unit: Seconds) if it is 0, it indicates unlimited 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 __, "failed to set file permissions ");
If ($ setReslut & $ chmodReslut)
{
Return true;
}
Else
{
Return false;
}
}
}

/**
* Get cached data
* @ Param $ key the key cached by the 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 ();
}
}
}

/**
* Deleting 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 functions
* @ Param $ line: number of rows
* @ 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 design PHP programs based on the ThinkPHP framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.