<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* D O N O T R E M O V E T H E S E C O M M E N T S
*
* @Package PHPCache
* @Author Hamid Alipour, http://blog.code-head.com/ http://www.hamidof.com/
*/
if( !defined('PHPCACHE_BASE_DIR') ) define('PHPCACHE_BASE_DIR', dirname(__FILE__));
if( !defined('PHPCACHE_DRIVER_DIR') ) define('PHPCACHE_DRIVER_DIR', PHPCACHE_BASE_DIR .DIRECTORY_SEPARATOR .'drivers');
if( !defined('PHPCACHE_BD_LOGIN_ATTEMPT') ) define('PHPCACHE_BD_LOGIN_ATTEMPT', 100);
define('PHPCACHE_TIME_NOW', time());
define('PHPCACHE_1_SECOND', 1);
define('PHPCACHE_1_MINUTE', PHPCACHE_1_SECOND * 60);
define('PHPCACHE_1_HOUR', PHPCACHE_1_MINUTE * 60);
define('PHPCACHE_1_DAY', PHPCACHE_1_HOUR * 24);
define('PHPCACHE_1_WEEK', PHPCACHE_1_DAY * 4);
define('PHPCACHE_1_MONTH', PHPCACHE_1_DAY * 30);
define('PHPCACHE_1_YEAR', PHPCACHE_1_MONTH * 12);
/**
* PHPCACHE_GC_PROBABILITY = 10 &
* PHPCACHE_GC_DIVISOR = 100
* Means there is 10% chance that the garbage collection will be done on each
* PHPCache::configure($database) call
*/
if( !defined('PHPCACHE_GC_PROBABILITY') ) define('PHPCACHE_GC_PROBABILITY', 1);
if( !defined('PHPCACHE_GC_DIVISOR') ) define('PHPCACHE_GC_DIVISOR', 100);
/**
* TO = Table optimizer
*/
if( !defined('PHPCACHE_TO_PROBABILITY') ) define('PHPCACHE_TO_PROBABILITY', 10);
if( !defined('PHPCACHE_TO_DIVISOR') ) define('PHPCACHE_TO_DIVISOR', 100);
require_once PHPCACHE_BASE_DIR .DIRECTORY_SEPARATOR .'phpcache.interface.php';
require_once PHPCACHE_DRIVER_DIR .DIRECTORY_SEPARATOR .'phpcache.driver.interface.php';
class PHPCache implements PHPCache_Interface {
private $db_handle;
private $db_table;
static private $instance;
private $time_now;
private $last_error;
private function __construct($params) {
if( !isset($params['type']) ) {
$this->trigger_error('I couldn\'t find the database type in $params[\'type\'].');
}
$database_type = strtolower($params['type']);
$driver_file = PHPCACHE_DRIVER_DIR .DIRECTORY_SEPARATOR .'phpcache.driver.' .$database_type .'.class.php';
if( !file_exists($driver_file) ) {
$this->trigger_error('PHPCache doesn\'t support the database type in $params[\'type\'] yet.');
}
require_once($driver_file);
$driver_class = 'PHPCache_Driver_' .$database_type;
$this->db_handle = new $driver_class($params);
$this->db_table = isset($params['table']) ? $params['table'] : 'PHPCache';
$this->time_now = PHPCACHE_TIME_NOW;
$this->last_error = NULL;
$this->clean_up();
}
public static function configure($params) {
if( self::$instance == NULL ) {
self::$instance = new PHPCache($params);
}
}
public static function instance() {
if( self::$instance == NULL ) {
$this->trigger_error('You have to call PHPCache::configure first.');
}
return self::$instance;
}
public function create_table() {
$this->db_handle->create_table($this->db_table);
}
public function store($key, $value, $expires) {
$data = array(
'PHPCache_key' => $this->db_handle->escape(md5($key)),
'PHPCache_value' => $this->db_handle->escape(serialize($value)),
'PHPCache_expires' => $this->db_handle->escape($this->time_now + $expires)
);
$query = "
REPLACE INTO
{$this->db_table}
(
PHPCache_key,
PHPCache_value,
PHPCache_expires
)
VALUES
(
{$data['PHPCache_key']},
{$data['PHPCache_value']},
{$data['PHPCache_expires']}
)
";
if( $this->db_handle->query($query) ) {
return true;
} else {
$this->last_error = $this->db_handle->error();
return false;
}
}
public function get($key) {
$key = $this->db_handle->escape(md5($key));
$query = "
SELECT
PHPCache_value, PHPCache_expires
FROM
{$this->db_table}
WHERE
PHPCache_key = $key
";
if( !($result = $this->db_handle->query($query)) ) {
$this->trigger_error($this->db_handle->error());
return false;
}
if( $result->num_rows < 1 ) {
return false;
}
$data = $result->fetch_assoc();
if( $data['PHPCache_expires'] < $this->time_now ) {
return false;
}
if( $data['PHPCache_value'] && trim($data['PHPCache_value']) != '' ) {
$_data = unserialize($data['PHPCache_value']);
if( $_data === false ) {
$this->trigger_error("Unserialize failed, you might need to increase the size of database column {$this->db_table}.PHPCache_value");
return false;
}
return $_data;
} else {
return NULL;
}
}
public function set_expire($key) {
$key = $this->db_handle->escape(md5($key));
$expires = $this->db_handle->escape($this->time_now - PHPCACHE_1_YEAR);
$query = "
REPLACE INTO
{$this->db_table}
(
PHPCache_key,
PHPCache_expires
)
VALUES
(
{$key},
{$expires}
)
";
$this->db_handle->query($query);
}
public function remove($key) {
$key = $this->db_handle->escape(md5($key));
$query = "
DELETE
FROM
{$this->db_table}
WHERE
PHPCache_key = $key
";
$this->db_handle->query($query);
}
public function clean_up() {
if( rand(1, PHPCACHE_GC_DIVISOR) <= PHPCACHE_GC_PROBABILITY ) {
$this->gc();
}
if( rand(1, PHPCACHE_TO_DIVISOR) <= PHPCACHE_TO_PROBABILITY ) {
$this->optimize_table();
}
}
public function gc() {
$query = "
DELETE
FROM
{$this->db_table}
WHERE
PHPCache_expires < {$this->time_now}
";
$this->db_handle->query($query);
}
public function optimize_table() {
$this->db_handle->optimize_table($this->db_table);
}
private function trigger_error($msg) {
$this->last_error = $msg;
trigger_error("PHPCache Error: $msg", E_USER_ERROR);
exit;
}
public function has_error() {
return $this->last_error != NULL;
}
public function last_error() {
return $this->last_error;
}
} // Class
?>
PHPCache,php 缓存控制


**PHP Cache技术详解** 在PHP开发中,缓存是一种常用的技术手段,用于提高网站或应用程序的性能。PHP Cache就是一种实现PHP代码缓存的机制,它允许开发者将频繁执行的PHP脚本结果存储起来,避免每次请求时都重新计算,从而显著提升页面加载速度和系统响应效率。 **一、PHP Cache的原理** PHP Cache的工作原理是将执行过的PHP脚本编译后的opcode(操作码)存储在内存中,当后续请求相同脚本时,不再需要重新解析和编译,直接从缓存中读取并执行,减少了CPU和内存资源的消耗。这种技术尤其适用于那些静态内容多、更新频率低的网站。 **二、PHPCache类库介绍** 提到的"PHPCache"是一个特定的PHP缓存控制类库,它提供了一种方便的方式来管理缓存。通过数据库(如MySQL)控制缓存的存取时间,使得开发者可以更灵活地设置和管理缓存。其主要特性包括: 1. **接口封装**:PHPCache提供了一个统一的接口`phpcache.interface.php`,使得不同缓存驱动的切换变得更加简单。 2. **驱动支持**:`drivers`目录包含了不同的缓存驱动实现,例如文件系统、数据库等,开发者可以根据实际需求选择合适的缓存方式。 3. **示例使用**:`example.php`文件提供了如何使用PHPCache类库的实例,通过查看和学习这个例子,可以快速掌握其使用方法。 **三、PHPCache的使用步骤** 1. **引入类库**:你需要在PHP脚本中引入`phpcache.class.php`文件,以便使用PHPCache类。 2. **实例化对象**:创建一个PHPCache对象,通常会指定一个缓存驱动,比如数据库驱动。 3. **设置缓存**:调用对象的方法来设置缓存,例如`setCache('key', 'value', $expiration)`,其中`key`是缓存的标识,`value`是要存储的数据,`$expiration`是缓存过期时间。 4. **获取缓存**:通过`getCache('key')`方法获取指定`key`的缓存数据。 5. **清除缓存**:如果需要删除某个缓存,可以使用`deleteCache('key')`方法。 6. **控制缓存生命周期**:由于PHPCache使用数据库控制缓存,所以可以利用数据库操作来实现更复杂的缓存生命周期管理,如根据用户行为或定时任务更新缓存。 **四、缓存策略** 在实际应用中,根据业务场景的不同,可能需要采用不同的缓存策略: 1. **LRU(Least Recently Used)**:最近最少使用的缓存策略,当缓存满时,优先淘汰最久未使用的数据。 2. **LFU(Least Frequently Used)**:访问频率最低的缓存策略,淘汰访问次数最少的数据。 3. **Time-To-Live(TTL)**:基于时间的缓存策略,设置固定过期时间,到期自动失效。 **五、总结** PHPCache作为一个强大的PHP缓存解决方案,结合了数据库控制的灵活性和多种缓存驱动的支持,可以帮助开发者高效地管理和优化应用的性能。通过深入理解其工作原理和使用方法,可以更好地适应各种复杂的缓存需求,为项目带来显著的性能提升。






























- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 0
- 资源: 3
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于Python的Django-vue二手电子设备交易平台源码-说明文档-演示视频.zip
- test6666666666
- 博文 自然语言处理:文本分类 中代码用到的测试集
- pythonProject_0.zip
- 搭建Spring Boot+Vue前后端分离.docx
- C#实现ModbusRTU通信测试的Demo
- <script type="text/javascript" src="./js/vue.min.js"> </script>
- .NET 9.0 中 DeepSeek 模型入门示例
- 本科毕设安卓Android恶意软件应用检测项目-机器学习python源码(含数据、文档).zip
- C#连接西门子PLC并实现报警发送邮件到邮箱
- 自动化全天采集超清壁纸图网站HTML源码 可做主站增加流量用
- 方法在计算机中的执行原理
- 人工智能领域中的大模型技术及应用实践
- 全球地震数据,配合【Python数据可视化(五)GeoJSON文件】使用
- My SQL环境下的数据库与数据表操作及多种SQL查询技术的应用实践
- 基于Python的Django-vue高校成绩分析系统的设计与实现源码-说明文档-演示视频.zip


