<?php
namespace startmvc\core;
use startmvc\core\Config;
class Cache {
private $drive;
public function __construct(string $driveName, array $params = array()) {
$config=Config::load('cache');
$driveName=$driveName??$config['drive'];
$params=$params?$params:$config[$driveName];
$classFile = __DIR__ . DS .'cache'.DS. ucfirst($driveName) . '.php';
if (file_exists($classFile)) {
$className = 'startmvc\\core\\cache\\' . ucfirst($driveName);
if (class_exists($className)) {
$this->drive = new $className($params);
} else {
throw new \Exception("类 $className 不存在");
}
} else {
throw new \Exception("文件 $classFile 没有找到");
}
}
public function set(string $key, $val) {
$this->drive->set($key, $val);
return $this;
}
public function has(string $key) {
return $this->drive->has($key);
}
public function get(string $key) {
return $this->drive->get($key);
}
public function delete(string $key) {
$this->drive->delete($key);
return $this;
}
public function clear() {
$this->drive->clear();
return $this;
}
}