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

來自phpguru得Php Cache類源碼

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

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

貼一下代碼吧:下面也有下載地址,其實很簡單,重要的是學(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__ 對當(dāng)前類的靜態(tài)引用,取決于定義當(dāng)前方法所在的類:使用 static:: 不再被解析為定義當(dāng)前方法所在的類,而是在實際運行時計算的。也可以稱之為“靜態(tài)綁定”,因為它可以用于(但不限于)靜態(tài)方法的調(diào)用。
    2017-02-02
  • PHP簡單選擇排序算法實例

    PHP簡單選擇排序算法實例

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

    PHP讀取xml方法介紹

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

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

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

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

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

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

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

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

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

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

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

    聊聊PHP中刪除字符串的逗號和尾部斜杠的方法

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

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

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

最新評論

芮城县| 东源县| 扎囊县| 南部县| 万安县| 德江县| 缙云县| 皋兰县| 巴马| 上饶县| 方山县| 郁南县| 新竹市| 呼伦贝尔市| 新郑市| 定日县| 罗田县| 增城市| 罗江县| 金阳县| 黑水县| 麻江县| 无锡市| 龙南县| 潜山县| 兴国县| 精河县| 彰化市| 永靖县| 安康市| 澎湖县| 英山县| 洛扎县| 加查县| 武安市| 五华县| 澎湖县| 岢岚县| 固镇县| 奇台县| 固原市|