欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

來自phpguru得Php Cache類源碼

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

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

貼一下代碼吧:下面也有下載地址,其實很簡單,重要的是學習
復制代碼 代碼如下:

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

使用方法:
復制代碼 代碼如下:

$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中遍歷stdclass object的實現(xiàn)代碼

    PHP中遍歷stdclass object的實現(xiàn)代碼

    從網(wǎng)上查到的方法是 用get_object_vars 把類類型轉(zhuǎn)換成數(shù)組 然后在用 foreach 遍歷即可
    2011-06-06
  • 在項目中尋找代碼的壞命名

    在項目中尋找代碼的壞命名

    這段時間一直做項目,所以相對忙碌些,今天終于有時間回過頭來好好看一下自己寫的代碼,看哪里有問題,哪里有壞味道
    2012-07-07
  • PHP高級OOP技術(shù)演示

    PHP高級OOP技術(shù)演示

    如果你了解基本的OOP概念,那么我將向你展示更高級的技術(shù)。
    2009-08-08
  • 必須收藏的23個php實用代碼片段

    必須收藏的23個php實用代碼片段

    這篇文章主要為大家分享了必須收藏的23個php實用代碼片段,幫助大家更好地學習php程序設(shè)計,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 常見PHP數(shù)據(jù)庫解決方案分析介紹

    常見PHP數(shù)據(jù)庫解決方案分析介紹

    您可以用很多的方式創(chuàng)建PHP數(shù)據(jù)庫設(shè)計、數(shù)據(jù)庫訪問和基于數(shù)據(jù)庫的 PHP 業(yè)務(wù)邏輯代碼,但最終一般以錯誤告終。本文說明了數(shù)據(jù)庫設(shè)計和訪問數(shù)據(jù)庫的PHP代碼中出現(xiàn)的常見問題,以及在遇到這些問題時如何修復它們。
    2015-09-09
  • PHP非對稱與對稱雙向加密解密的方式

    PHP非對稱與對稱雙向加密解密的方式

    RSA非對稱加密解密算法是一種廣泛應(yīng)用于信息安全領(lǐng)域的加密算法,AES、DES、3DES都是對稱加密算法,也就是說加密和解密使用的是同一個密鑰,本文給大家介紹了PHP非對稱與對稱雙向加密解密的方式,需要的朋友可以參考下
    2023-10-10
  • PHP設(shè)置隨機數(shù)的方法小結(jié)

    PHP設(shè)置隨機數(shù)的方法小結(jié)

    這篇文章主要介紹了PHP設(shè)置隨機數(shù)的方法,結(jié)合實例形式分析了php生成隨機數(shù)/生成隨機字符串的6種實現(xiàn)方法與相關(guān)操作注意事項,文中有詳細的代碼示例,需要的朋友可以參考下
    2023-09-09
  • php自定文件保存session的方法

    php自定文件保存session的方法

    這篇文章主要介紹了php自定文件保存session的方法,詳細講述了session創(chuàng)建與使用的技巧,以及對應(yīng)的作用范圍分析,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • 在PHP中養(yǎng)成7個面向?qū)ο蟮暮昧晳T

    在PHP中養(yǎng)成7個面向?qū)ο蟮暮昧晳T

    如果您尚未打算用 OO 原則創(chuàng)建應(yīng)用程序,則使用 PHP 的面向?qū)ο螅∣O)的語言特性,這 7 個習慣將幫助您開始在過程編程與 OO 編程之間進行轉(zhuǎn)換。
    2010-07-07
  • 10個可以簡化php開發(fā)過程的MySQL工具

    10個可以簡化php開發(fā)過程的MySQL工具

    使用各種精心設(shè)計的工具來管理MySQL數(shù)據(jù)庫要比單純使用傳統(tǒng)的方法輕松得的多。開發(fā)人員應(yīng)該不斷尋找那些能夠縮短開發(fā)時間的工具。這也是我們本文整理這10個能夠簡化開發(fā)過程的MySQL工具的原因。
    2010-04-04

最新評論