From Phpguru PHP cache class Source _php tips

Source: Internet
Author: User
Tags mixed unique id
The role of the cache needless to say, everyone knows, and interviewed some people these days, found that a lot of people frame with many, the basis are forgotten, you ask some things, he always said that the framework is resolved, and do not understand what is going on, so also remind everyone should pay attention to the accumulation of basic knowledge, after some problems can be easily .

Group also some friends of the basic knowledge is very disdain, always said to have the ability to, the basic knowledge can not test out what. I have always disagreed with this view.
This is just a little sense. Here is a list of PHP's cache classes:

Post code: The following also has the download address, in fact, very simple, it is important to learn
Copy Code code as follows:

<?php
/**
* o------------------------------------------------------------------------------o
* | This package is licensed under the Phpguru license. A Quick Summary is |
* | This for commercial use, there is a small one-time licensing fee to pay. for |
* | Registered charities and educational institutes there is a reduced license |
* | Fee available. Can read more at: |
* | |
* | http://www.phpguru.org/static/license.html |
* o------------------------------------------------------------------------------o
*/
/**
* Caching Libraries for PHP5
*
* Handles data and output caching. Defaults TO/DEV/SHM
* (Shared memory). All methods are static.
*
* Eg: (Output caching)
*
* IF (! Outputcache::start (' group ', ' unique ID ', 600)) {
*
* // ... Output
*
* Outputcache::end ();
* }
*
* Eg: (Data caching)
*
* if (! $data = datacache::get (' group ', ' unique ID ')) {
*
* $data = time ();
*
* Datacache::P ut (' group ', ' unique ID ', $data);
* }
*
* Echo $data;
*/
Class Cache
{
/**
* Whether caching is enabled
* @var BOOL
*/
public static $enabled = true;
/**
* Place to store the cache files
* @var String
*/
protected static $store = '/dev/shm/';
/**
* Prefix to use on cache files
* @var String
*/
protected static $prefix = ' cache_ ';
/**
* Stores Data
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
* @param int $ttl How long to cache for (in seconds)
*/
protected static function Write ($group, $id, $ttl, $data)
{
$filename = Self::getfilename ($group, $id);
if ($fp = @fopen ($filename, ' XB ')) {
if (Flock ($FP, lock_ex)) {
Fwrite ($fp, $data);
}
Fclose ($FP);
Set Filemtime
Touch ($filename, Time () + $ttl);
}
}
/**
* Reads data
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function Read ($group, $id)
{
$filename = Self::getfilename ($group, $id);
Return file_get_contents ($filename);
}
/**
* Determines if a entry is cached
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function iscached ($group, $id)
{
$filename = Self::getfilename ($group, $id);
if (self:: $enabled && file_exists ($filename) && filemtime ($filename) > Time ()) {
return true;
}
@unlink ($filename);
return false;
}
/**
* Builds a filename/path from group, ID and
* Store.
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
*/
protected static function GetFileName ($group, $id)
{
$id = MD5 ($id);
Return self:: $store. Self:: $prefix. "{$group}_{$id}";
}
/**
* Sets the filename prefix to use
*
* @param string $prefix Filename prefix to use
*/
public static function Setprefix ($prefix)
{
Self:: $prefix = $prefix;
}
/**
* Sets the store for cache files. Defaults to
*/dev/shm. Must have trailing slash.
*
* @param string $store the Dir to store, the cache data in
*/
public static function Setstore ($store)
{
Self:: $store = $store;
}
}
/**
* Output Cache extension of Base caching class
*/
Class OutputCache extends Cache
{
/**
* Group of currently being recorded data
* @var String
*/
private static $group;
/**
* ID of currently being recorded data
* @var String
*/
private static $id;
/**
* Ttl of currently being recorded data
* @var int
*/
private static $ttl;
/**
* Starts caching off. Returns true if cached, and dumps
* the output. False if not cached and start output buffering.
*
* @param string $group Group to store data under
* @param string $id Unique ID of this data
* @param int $ttl How long to cache for (in seconds)
* @return BOOL True If cached, False if not
*/
public static function Start ($group, $id, $ttl)
{
if (self::iscached ($group, $id)) {
Echo Self::read ($group, $id);
return true;
} else {
Ob_start ();
Self:: $group = $group;
Self:: $id = $id;
Self:: $ttl = $ttl;
return false;
}
}
/**
* Ends caching. Writes data to disk.
*/
public static function End ()
{
$data = Ob_get_contents ();
Ob_end_flush ();
Self::write (self:: $group, Self:: $id, Self:: $ttl, $data);
}
}
/**
* Data Cache extension of Base caching class
*/
Class Datacache extends Cache
{
/**
* Retrieves data from the cache
*
* @param string $group Group this data belongs to
* @param string $id Unique ID of the data
* @return mixed either the resulting data, or null
*/
public static function Get ($group, $id)
{
if (self::iscached ($group, $id)) {
Return Unserialize (Self::read ($group, $id));
}
return null;
}
/**
* Stores data in the cache
*
* @param string $group Group this data belongs to
* @param string $id Unique ID of the data
* @param int $ttl How long to cache for (in seconds)
* @param mixed $data the data to store
*/
The public static function is put ($group, $id, $ttl, $data)
{
Self::write ($group, $id, $ttl, serialize ($data));
}
}
?>

How to use:
Copy Code code as follows:

$dir =!empty ($_server[' argv '][1])? $_server[' argv '][1]: '. ';
$DH = Opendir ($dir);
while ($filename = Readdir ($DH)) {
if ($filename = = '. ' OR $filename = = '.. ') {
Continue
}
if (Filemtime ($dir. Directory_separator. $filename) < time ()) {
Unlink ($dir. Directory_separator. $FILENAME);
}
}

SOURCE Package Download

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.