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的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,Cache
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“来自phpguru得Php Cache类源码”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。