天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

來自phpguru得Php Cache類源碼

Cache的作用不用說大家都知道咯,這些天也面試了一些人,發現很多人框架用多了,基礎都忘記了,你問一些事情,他總是說框架解決了,而根本不明白是怎么回事,所以也提醒大家應該注意平時基礎知識的積累,之后對一些問題才能游刃有余.

群里也有些朋友對基礎知識很不屑,總說有能力就可以了,基礎知識考不出來什么.對于這樣的觀點,我一直不茍同.
這個只是一點感概罷了. 下面看正題,介紹一個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);
}
}

源碼打包下載

php技術來自phpguru得Php Cache類源碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 高清国语自产拍在线 | 亚洲大片在线观看 | 国产精品97久久AV色婷婷综合 | 国产欧美日韩国产高清 | 3DNagoonimation动漫| 国产成人啪精视频精东传媒网站 | 超碰在线视频人人AV | 色多多污网站在线观看 | 99热在线视频 | 在线播放日韩欧美亚洲日本 | 一级毛片免费下载 | 欧美eee114| 亚洲成A人片在线观看中文不卡 | 欧美日本高清动作片www网站 | 国产成人免费高清激情视频 | 日本一二三区在线视频 | 97视频在线免费播放 | 囯产精品久久久久久久久蜜桃 | 99视频精品全部免费 在线 | 四虎精品久久久久影院 | 秋霞特色大片18岁入口 | 国产乱妇乱子在线播视频播放网站 | 亚洲91av| avtt天堂网Av无码 | 小草视频免费观看在线 | 国产又粗又猛又爽黄老大爷 | 4399日本电影完整版在线观看免费 | 网友自拍成人在线视频 | 一区视频免费观看 | 中文字幕在线观看 | 成年人视频在线观看免费 | 久久精品视频在线看15 | 国产AV麻豆出品在线播放 | 亚洲免费人成在线视频观看 | 无修肉动漫在线观看影片 | 亚瑟天堂久久一区二区影院 | 日韩人妻精品久久日 | 成人久久欧美日韩一区二区三区 | 国产精品九九久久精品视 | 福利社影院 | 永久免费看A片无码网站四虎 |