最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

來(lái)自phpguru得Php Cache類(lèi)源碼

 更新時(shí)間:2010年04月15日 23:33:05   作者:  
群里也有些朋友對(duì)基礎(chǔ)知識(shí)很不屑,總說(shuō)有能力就可以了,基礎(chǔ)知識(shí)考不出來(lái)什么.對(duì)于這樣的觀點(diǎn),我一直不茍同.
Cache的作用不用說(shuō)大家都知道咯,這些天也面試了一些人,發(fā)現(xiàn)很多人框架用多了,基礎(chǔ)都忘記了,你問(wèn)一些事情,他總是說(shuō)框架解決了,而根本不明白是怎么回事,所以也提醒大家應(yīng)該注意平時(shí)基礎(chǔ)知識(shí)的積累,之后對(duì)一些問(wèn)題才能游刃有余.

群里也有些朋友對(duì)基礎(chǔ)知識(shí)很不屑,總說(shuō)有能力就可以了,基礎(chǔ)知識(shí)考不出來(lái)什么.對(duì)于這樣的觀點(diǎn),我一直不茍同.
這個(gè)只是一點(diǎn)感概罷了. 下面看正題,介紹一個(gè)php的Cache類(lèi):

貼一下代碼吧:下面也有下載地址,其實(shí)很簡(jiǎn)單,重要的是學(xué)習(xí)
復(fù)制代碼 代碼如下:

<?php
/**
* o------------------------------------------------------------------------------o
* | This package is licensed under the Phpguru license. A quick summary is |
* | that 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. You 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::Put('group', 'unique id', 10, $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 an 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
*/
public static function Put($group, $id, $ttl, $data)
{
self::write($group, $id, $ttl, serialize($data));
}
}
?>

使用方法:
復(fù)制代碼 代碼如下:

$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);
}
}

源碼打包下載

相關(guān)文章

  • PHP new static 和 new self詳解

    PHP new static 和 new self詳解

    使用 self:: 或者 __CLASS__ 對(duì)當(dāng)前類(lèi)的靜態(tài)引用,取決于定義當(dāng)前方法所在的類(lèi):使用 static:: 不再被解析為定義當(dāng)前方法所在的類(lèi),而是在實(shí)際運(yùn)行時(shí)計(jì)算的。也可以稱(chēng)之為“靜態(tài)綁定”,因?yàn)樗梢杂糜冢ǖ幌抻冢╈o態(tài)方法的調(diào)用。
    2017-02-02
  • PHP簡(jiǎn)單選擇排序算法實(shí)例

    PHP簡(jiǎn)單選擇排序算法實(shí)例

    這篇文章主要介紹了PHP簡(jiǎn)單選擇排序算法實(shí)例,本文直接給出實(shí)現(xiàn)代碼,并以類(lèi)的方式實(shí)現(xiàn),需要的朋友可以參考下
    2015-01-01
  • PHP讀取xml方法介紹

    PHP讀取xml方法介紹

    在php開(kāi)發(fā)中,我們經(jīng)常會(huì)越到讀取xml文件的情況,這里簡(jiǎn)單總結(jié)下一些方法,方便需要的朋友
    2013-01-01
  • php小經(jīng)驗(yàn):解析preg_match與preg_match_all 函數(shù)

    php小經(jīng)驗(yàn):解析preg_match與preg_match_all 函數(shù)

    本篇文章是對(duì)php中的preg_match函數(shù)與preg_match_all函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php實(shí)現(xiàn)隨機(jī)顯示圖片方法匯總

    php實(shí)現(xiàn)隨機(jī)顯示圖片方法匯總

    本文分享一個(gè)php實(shí)現(xiàn)的隨機(jī)顯示圖片的函數(shù),可以將指定文件夾中存放的圖片隨機(jī)地顯示出來(lái)。有興趣的朋友研究下吧。
    2015-05-05
  • PHP中使用php://input處理相同name值的表單數(shù)據(jù)

    PHP中使用php://input處理相同name值的表單數(shù)據(jù)

    這篇文章主要介紹了PHP中使用php://input處理相同name值的表單數(shù)據(jù),本文是另一種處理相同name值表單數(shù)據(jù)的方法,文中同時(shí)給出另一種方法,需要的朋友可以參考下
    2015-02-02
  • 談?wù)凱HP連接Access數(shù)據(jù)庫(kù)的注意事項(xiàng)

    談?wù)凱HP連接Access數(shù)據(jù)庫(kù)的注意事項(xiàng)

    有的時(shí)候需要用php連接access數(shù)據(jù)庫(kù),結(jié)果整了半天Access數(shù)據(jù)庫(kù)就是連接不上,查找很多資料,以下是些個(gè)人經(jīng)驗(yàn),希望能給需要連接access 數(shù)據(jù)的人帶來(lái)幫助。
    2016-08-08
  • PHP計(jì)算個(gè)人所得稅示例【不使用速算扣除數(shù)】

    PHP計(jì)算個(gè)人所得稅示例【不使用速算扣除數(shù)】

    這篇文章主要介紹了PHP計(jì)算個(gè)人所得稅,結(jié)合實(shí)例形式分析了php自定義函數(shù)不使用速算扣除數(shù)計(jì)算個(gè)人所得稅的相關(guān)操作技巧,涉及數(shù)組遍歷、數(shù)值運(yùn)算的簡(jiǎn)單使用,需要的朋友可以參考下
    2018-03-03
  • 聊聊PHP中刪除字符串的逗號(hào)和尾部斜杠的方法

    聊聊PHP中刪除字符串的逗號(hào)和尾部斜杠的方法

    這篇文章通過(guò)兩個(gè)實(shí)例講解了PHP中刪除字符串中的逗號(hào)以及尾部斜杠的方法,文中給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值
    2021-09-09
  • PHP實(shí)現(xiàn)的登錄頁(yè)面信息提示功能示例

    PHP實(shí)現(xiàn)的登錄頁(yè)面信息提示功能示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)的登錄頁(yè)面信息提示功能,涉及php表單提交、數(shù)據(jù)庫(kù)查詢、判斷及session數(shù)據(jù)存儲(chǔ)等相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07

最新評(píng)論

壶关县| 临洮县| 唐山市| 星子县| 清镇市| 乌拉特前旗| 沙坪坝区| 宜黄县| 东至县| 老河口市| 长汀县| 曲麻莱县| 赤壁市| 时尚| 龙门县| 饶平县| 抚顺县| 无为县| 唐河县| 陇西县| 大荔县| 汨罗市| 平果县| 威海市| 绥德县| 雷山县| 姜堰市| 昆山市| 萝北县| 新巴尔虎左旗| 屏边| 高雄市| 和顺县| 溧阳市| 青龙| 台中市| 芦溪县| 祁阳县| 阳江市| 封丘县| 曲松县|