巅云智能建站平台搭建版(创业流派版)火爆上线,毕生受权!新增:文章智能收罗+全站真静态打包+都会分站+智能小法式+不法词过滤+H5自顺应+智能链词等功效功效概况
赞助文档Help

来自phpguru得Php Cache类源码

一佰互联网站建造(www.taishanly.com) 宣布日期 2020-04-30 09:22:31 阅读数: 99

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

源码打包下载
TAG标签: 来自phpguru得Php   Cache类源码  
一佰互联是天下着名建站品牌办事商,咱们有九年、网站建造、网页设想、php开辟和域名注册及假造主机办事经历,供给的办事更是天下着名。最近几年来还整合团队上风自立开辟了可视化多用户”“3.0平台版,拖拽排版网站建造设想,轻松完成pc站、手机微网站、小法式、APP一体化全网营销网站扶植 ,已胜利的为天下上百家收集公司供给自助建站平台搭建办事。

相干动静more

20
05月
【虎林网】若何获得精准流量?_巅云建站

【虎】获得? 作为SEO职员真正要问本身的是你的SEO思惟有不变更?若是你的SEO思惟还逗留在发外链优化关头词排名仅此罢了下面,那末S... >>概况

17
04月
阿里回应并未求全谴责经营商违规 乃合作敌手歪曲

对有动静称,“阿里巴巴曾向局部媒体发送邮件,公然求全谴责底子经营商在假造经营商营业上违规,转售零售价钱太高”一事。阿里... >>概况

30
04月
没法在产生毛病时建立会话,请查抄 PHP 或

windows下的处理体例:经由过程查找php.ini的session.save_path = ""的途径,查抄是不是存在这个目次或这个目次是不是有... >>概况

15
04月
杀毒软件McAfee开创人迈克菲要竞选美国总

09月10日报道:北京时候9月9日动静,据科技博客TheVerge报道,杀毒软件McAfee开创人约翰·迈克菲(JohnMcA... >>概况

高端网站扶植

美工统筹SEO,为企业电子商务营销助力!

德律风:

023-85725751
建站

产物

域名注册 假造主机 云办事器 企业邮局
智能建站 APP打包 微站/小法式 创业平台
网站推行 媒体营销 智能收罗 AI机械人
400德律风 短信营销 店销机械人
私家定制 流派网站