php实现文件数据缓存实现代码缓存技术是每次访问页面的时候,都会先检测相应的缓存是否存在,如果不存在,就连接数据库,得到数据,完成模板变量的赋值,显示页面,同时生成缓存文件,这样下次访问的时候缓存文件就发挥作用了
php教程实现文件数据缓存实现代码
缓存技术是每次访问页面的时候,都会先检测相应的缓存是否存在,如果不存在,就连接数据库教程,得到数据,完成模板变量的赋值,显示页面,同时生成缓存文件,这样下次访问的时候缓存文件就发挥作用了,而不会再执行if块的数据查询语句了。当然,在实际使用中会有很多东西要考虑,比如,有效期的设置,缓存组的设置等等
class cacheexception extends exception {}
/**
* 缓存抽象类
*/
abstract class cache_abstract {
/**
* 读缓存变量
*
* @param string $key 缓存下标
* @return mixed
*/
abstract public function fetch($key);
/**
* 缓存变量
*
* @param string $key 缓存变量下标
* @param string $value 缓存变量的值
* @return bool
*/
abstract public function store($key, $value);
/**
* 删除缓存变量
*
* @param string $key 缓存下标
* @return cache_abstract
*/
abstract public function delete($key);
/**
* 清(删)除所有缓存
*
* @return cache_abstract
*/
abstract public function clear();
/**
* 锁定缓存变量
*
* @param string $key 缓存下标
* @return cache_abstract
*/
abstract public function lock($key);
/**
* 缓存变量解锁
*
* @param string $key 缓存下标
* @return cache_abstract
*/
abstract public function unlock($key);
/**
* 取得缓存变量是否被锁定
*
* @param string $key 缓存下标
* @return bool
*/
abstract public function islocked($key);
/**
* 确保不是锁定状态
* 最多做$tries次睡眠等待解锁,超时则跳过并解锁
*
* @param string $key 缓存下标
*/
public function checklock($key) {
if (!$this->islocked($key)) {
return $this;
}
$tries = 10;
$count = 0;
do {
usleep(200);
$count ++;
} while ($count islocked($key)); // 最多做十次睡眠等待解锁,超时则跳过并解锁
$this->islocked($key) && $this->unlock($key);
return $this;
}
}
/**
* apc扩展缓存实现
*
*
* @category mjie
* @package cache
* @author 流水孟春
* @copyright copyright (c) 2008-
* @license new bsd license
* @version $id: cache/apc.php 版本号 2010-04-18 23:02 cmpan $
*/
class cache_apc extends cache_abstract {
protected $_prefix = 'cache.mjie.net';
public function __construct() {
if (!function_exists('apc_cache_info')) {
throw new cacheexception('apc extension didn't installed');
}
}
/**
* 保存缓存变量
*
* @param string $key
* @param mixed $value
* @return bool
*/
public function store($key, $value) {
return apc_store($this->_storagekey($key), $value);
}
/**
* 读取缓存
*
* @param string $key
* @return mixed
*/
public function fetch($key) {
return apc_fetch($this->_storagekey($key));
}
/**
* 清除缓存
*
* @return cache_apc
*/
public function clear() {
apc_clear_cache();
return $this;
}
/**
* 删除缓存单元
*
* @return cache_apc
*/
public function delete($key) {
apc_delete($this->_storagekey($key));
return $this;
}
/**
* 缓存单元是否被锁定
*
* @param string $key
* @return bool
*/
public function islocked($key) {
if ((apc_fetch($this->_storagekey($key) . '.lock')) === false) {
return false;
}
return true;
}
/**
* 锁定缓存单元
*
* @param string $key
* @return cache_apc
*/
public function lock($key) {
apc_store($this->_storagekey($key) . '.lock', '', 5);
return $this;
}
/**
* 缓存单元解锁
*
* @param string $key
* @return cache_apc
*/
public function unlock($key) {
apc_delete($this->_storagekey($key) . '.lock');
return $this;
}
/**
* 完整缓存名
*
* @param string $key
* @return string
*/
private function _storagekey($key) {
return $this->_prefix . '_' . $key;
}
}
/**
* 文件缓存实现
*
*
* @category mjie
* @package cache
* @author 流水孟春
* @copyright copyright (c) 2008-
* @license new bsd license
* @version $id: cache/file.php 版本号 2010-04-18 16:46 cmpan $
*/
class cache_file extends cache_abstract {
public $usesubdir = false;
protected $_cachesdir = 'cache';
public function __construct() {
if (defined('data_dir')) {
$this->_setcachedir(data_dir . '/cache');
}
}
/**
* 获取缓存文件
*
* @param string $key
* @return string
*/
protected function _getcachefile($key) {
$subdir = $this->usesubdir ? substr($key, 0, 2) . '/' : '';
return $this->_cachesdir . '/' . $subdir . $key . '.php';
}
/**
* 读取缓存变量
* 为防止信息泄露,缓存文件格式为php文件,并以""开头
*
* @param string $key 缓存下标
* @return mixed
*/
public function fetch($key) {
$cachefile = self::_getcachefile($key);
if (file_exists($cachefile) && is_readable($cachefile)) {
// include 方式
//return include $cachefile;
// 系列化方式
return unserialize(@file_get_contents($cachefile, false, null, 13));
}
return false;
}
/**
* 缓存变量
* 为防止信息泄露,缓存文件格式为php文件,并以""开头
*
* @param string $key 缓存变量下标
* @param string $value 缓存变量的值
* @return bool
*/
public function store($key, $value) {
$cachefile = self::_getcachefile($key);
$cachedir = dirname($cachefile);
if(!is_dir($cachedir)) {
if(!@mkdir($cachedir, 0755, true)) {
throw new cacheexception("could not make cache directory");
}
}
// 用include方式
//return @file_put_contents($cachefile, '
return @file_put_contents($cachefile, '' . serialize($value));
}
/**
* 删除缓存变量
*
* @param string $key 缓存下标
* @return cache_file
*/
public function delete($key) {
if(emptyempty($key)) {
throw new cacheexception("missing argument 1 for cache_file::delete()");
}
$cachefile = self::_getcachefile($key);
if(!@unlink($cachefile)) {
throw new cacheexception("cache file could not be deleted");
}
return $this;
}
/**
* 缓存单元是否已经锁定
*
* @param string $key
* @return bool
*/
public function islocked($key) {
$cachefile = self::_getcachefile($key);
clearstatcache();
return file_exists($cachefile . '.lock');
}
/**
* 锁定
*
* @param string $key
* @return cache_file
*/
public function lock($key) {
$cachefile = self::_getcachefile($key);
$cachedir = dirname($cachefile);
if(!is_dir($cachedir)) {
if(!@mkdir($cachedir, 0755, true)) {
if(!is_dir($cachedir)) {
throw new cacheexception("could not make cache directory");
}
}
}
// 设定缓存锁文件的访问和修改时间
@touch($cachefile . '.lock');
return $this;
}
/**
* 解锁
*
* @param string $key
* @return cache_file
*/
public function unlock($key) {
$cachefile = self::_getcachefile($key);
@unlink($cachefile . '.lock');
return
下面来看一款关于smarty缓存的文件实例代码
再来看看smarty提供的页面缓存功能:
1
2require('smarty.class.php');
3$smarty = new smarty;
4$smarty->caching = true;
5if(!$smarty->is_cached('index.tpl')) {
6 // no cache available, do variable assignments here.
7 $contents = get_database_contents();
8 $smarty->assign($contents);
9}
10$smarty->display('index.tpl');
11?>
php缓存技术工作时,当程序查询数据的时候,会把相应的结果序列化后保存到文件中,以后同样的查询语句就可以不用直接查询数据库,而是从缓存文件中获得。这一改进使得程序运行速度得以太幅度提升.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。