我在 Codeigniter 中将这个简单的缓存类设置为库:
<?php
class Easy_cache {
static public $expire_after;
static function Easy_cache()
{
if ($this->expire_after == '')
{
$this->expire_after = 300;
}
}
static function store($key, $value)
{
$key = sha1($key);
$value = serialize($value);
file_put_contents(BASEPATH.'cache/'.$key.'.cache', $value);
}
static function is_cached($key)
{
$key = sha1($key);
if (file_exists(BASEPATH.'cache/'.$key.'.cache') && (filectime(BASEPATH.'cache/'.$key.'.php')+$this->expire_after) >= time())
return true;
return false;
}
static function get($key)
{
$key = sha1($key);
$item = file_get_contents(BASEPATH.'cache/'.$key.'.cache');
$items = unserialize($item);
return $items;
}
static function delete($key)
{
unlink(BASEPATH.'cache/'.sha1($key).'.cache');
}
}
我现在想使用它,所以在控制器中我正在使用它(我正在通过 加载库autoload.php
):
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$cache = $this->easy_cache;
if ( !$cache::is_cached('statistics') )
{
$data = array('data' => $this->model_acc->count());
$cache::store('server_statistics', $data);
}
else
$data = array('this' => 'value');
$this->load->view('main', array('servers' => $this->servers->get()));
}
}
然后我收到这个错误:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in [..]
我猜它与双点和静态函数有关,但我是这些类的新手,那有什么问题?