代码
class Test extends Controller {
function main()
{
// load the library
$this->load->library('simple_cache');
// key is the name you have given to the cached data
// will check if the item is cached
if (!$this->simple_cache->is_cached('key'))
{
// not cached, do our things that need caching
$data = array('print' => 'Hello World');
// store in cache
$this->simple_cache->cache_item('key', $data);
} else {
$data = $this->simple_cache->get_item('key');
}
$this->load->view('hello', $data);
}
}
simple cache library
<?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 3 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, see <http://www.gnu.org/licenses/>.
*/
class Simple_cache {
public $expire_after = 300;
public $cache_file = BASEPATH.'cache/';
function Simple_cache($life_time = '')
{
if (!empty($life_time))
{
$this->expire_after = $life_time;
}
}
function cache_item($key, $value)
{
if(!is_dir($this->cache_file))
{
$temp = explode('/',$this->cache_file);
$cur_dir = '';
for($i = 0; $i < count($temp); $i++)
{
$cur_dir .= $temp[$i].'/';
if (!is_dir($cur_dir))
{
@mkdir($cur_dir,0755);
}
}
}
if(!empty($value))
{
file_put_contents($this->cache_file.sha1($key).'.cache', serialize($value));
}
}
function is_cached($key)
{
if (file_exists($this->cache_file.sha1($key).'.cache')){
{
if((filectime($this->cache_file.sha1($key).'.cache')+$this->expire_after) >= time()))
{
return true;
}else{
delete_item($key);
return false;
}
} else {
return false;
}
}
function get_item($key)
{
$item = file_get_contents($this->cache_file.sha1($key).'.cache');
return unserialize($item);
}
function delete_item($key)
{
unlink($this->cache_file.sha1($key).'.cache');
}
function clear_all(){
}
}
分享到:
相关推荐
CodeIgniter自述文件的简单缓存 由开发,并根据GNU通用公共许可证发行(请参阅LICENSE)。 关于 用于CodeIgniter的简单缓存是基于GNU通用公共许可证发行的基于开源文件的片段缓存库。 它被设计为易于使用,并且很...
CodeIgniter框架是一个非常小巧的PHP框架。CI自带数据库文件缓存,但按官方的说法,缓存设置后永不过期,除非你调用方法主动删除。Cache files DO NOT expire. Any queries that have been cached will remain ...
Codeigniter支持缓存技术,以达到最快的速度。尽管CI已经相当高效了,但是网页中的动态内容、主机的内存CPU和数据库读取速度等因素直接影响了网页的加载速度。依靠网页缓存,你的网页可以达到近乎静态网页的加载速度...
CodeIgniter多级缓存简单的修改就可以将缓存文件存储到多级子文件夹中。 Codeigniter的基于文件的缓存系统将完全呈现HTML和SQL对象的输出,当用户访问您的网页时,Codeigniter将从没有MySQL连接的缓存文件中加载数据...
MP_Cache 类是为CodeIgniter提供更多灵活性的缓存解决方案。 你能使用它来缓存从数据库查询获得的单一数据变量或者缓存整个页面。 MP_Cache 把序列化后的数据保存进文件,同时提供函数方法去回收重建和删除该缓存文件...
本文实例讲述了CI框架网页缓存简单用法。分享给大家供大家参考,具体如下: CodeIgniter 可以让你通过缓存页面来达到更好的性能。 尽管 CodeIgniter 已经相当高效了,但是网页中的动态内容、主机的内存 CPU 和数据库...
Codeigniter-数据库驱动的路由简单的模型可以从数据库生成URL缓存,从而提高性能。 将此代码插入您的application/config/routes.php # Static Pages Route dynamicly generatedif ( file_exists ( APPPATH . 'cache...
CodeIgniter 是一个应用程序框架 1、免费:CodeIgniter是经过Apache/BSD-style开源许可授权的,只要你愿意就可以使用它。... 如果缓存(Cache)文件存在,它将绕过通常的系统执行顺序,被直接发送给浏览器。 安全(Secur
CodeIgniter很适合小站点应用开发,但是它自带的view功能可能会给不懂PHP的前端人员带来麻烦。 相比之下phpcms的view模板解析就强大多了,所以... /** * 模板解析缓存 */final class template_cache { public $c
ci-blade适用于Codeigniter 3.0+的Laravel... // view cache directory0x1.1简单易用$this->blade->view('welcome_message'); 0x1.2将var传递给模板$this->blade->view('welcome_message', $vars); #Laravel刀片文档
CodeIgniter开发人员工具栏 ... 更改缓存文件夹chmod 777 application/cache权限 对于HMVC 如果您使用的是HMVC第三方库, MX_Loader.php而不是MY_Loader.php复制到您的core目录,并将文件名更改为MY_Loader.php而不
本文实例讲述了CI框架中... * CodeIgniter * * An open source application development framework for PHP 5.2.4 or newer * * NOTICE OF LICENSE * * Licensed under the Open Software License version 3.0 *
向Web服务器授予日志,会话和缓存写入权限。 sudo chmod -R 755 ./application/{logs,cache,session} ; sudo chown -R nginx:nginx ./application/{logs,cache,session} ; Web服务器设置。 将以下
我习惯把缓存用Redis来存储,如果有朋友不想安装Reids,放入项目安装时.env文件中的CACHE_DRIVER =文件默认配置即可。 安装本项目跟普通安装一样 下载本项目,然后在项目根目录执行composer install 包安装完成后,...
* 包括`Database`, `Cache`, `HttpClient`, `Session`, `Storage`, `Upload` 等支持多驱动,可以适应不同环境的需求,其中数据库支持 `MySQL`, `MySQLI`, `Mongo`, `SQLite`, `Postgre`,缓存支持 `Memcache`, `File...