策略模式
1.概念:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户
一类动作的各个封装 维护度低 耦合度也低
2.使用场景:thinkphp 缓存类的封装 各种数据库连接的封装
3.编码
Strategy.php
/**
* Created by PhpStorm.
* User: 申大侠
* Date: 2019/4/16
* Time: 11:30
*/
namespace Mode;
class Strategy{
static private $handler;
/**
* 连接缓存
* @access public
* @param string $type 缓存类型
* @param array $options 配置数组
* @return object
*/
static public function connect($type,$options=array()) {
if(empty($type)){
return false;
}
$class = strpos($type,'\\')? $type : 'Mode\\Cache\\Driver\\'.ucwords(strtolower($type));
if(class_exists($class))
$cache = new $class($options);
else
return false;
return $cache;
}
/**
* 取得缓存类实例
* @param string $type
* @param array $options
* @return bool
*/
static function getInstance($type='',$options=array()) {
if(empty($type)){
return false;
}
static $_instance = array();
if(!isset($_instance[$type])){
$_instance[$type] = self::connect($type,$options);
}
self::$handler = $_instance[$type];
return $_instance[$type];
}
public function __call($method,$args){
//调用缓存类型自己的方法
if(method_exists(self::$handler, $method)){
return call_user_func_array(array(self::$handler,$method), $args);
}else{
return false;
}
}
}
spl_autoload_register(function($class){
$class = str_replace('\\', '/', $class);
include_once "../".$class.".php";
});
$obj = Strategy::getInstance('file');
$obj->set('Str_test', 'file:::hello world');
echo $obj->get('Str_test');
$obj = Strategy::getInstance('redis');
$obj->set('Str_test', 'redis:::hello world');
echo $obj->get('Str_test');
File.php
/**
* Created by PhpStorm.
* User: 申大侠
* Date: 2019/4/16
* Time: 11:30
*/
namespace Mode\Cache\Driver;
use Mode\Strategy;
/**
* 文件类型缓存类
*/
class File extends Strategy {
/**
* 架构函数
* @access public
*/
public function __construct($options=array()) {
$this->_temp = "./temp/";
$this->init();
}
/**
* 初始化检查
* @access private
* @return boolean
*/
private function init() {
// 创建应用缓存目录
if (!is_dir($this->_temp)) {
mkdir($this->_temp);
}
}
/**
* 取得变量的存储文件名
* @access private
* @param string $name 缓存变量名
* @return string
*/
private function filename($name) {
$name = md5("shen_".$name);
$filename = $this->_temp.$name. ".php";
return $filename;
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$filename = $this->filename($name);
if (!is_file($filename)) {
return false;
}
$content = file_get_contents($filename);
if( false !== $content) {
$content = unserialize($content);
return $content;
}else {
return false;
}
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int $expire 有效时间 0为永久
* @return boolean
*/
public function set($name,$value,$expire=null) {
$filename = $this->filename($name);
$data = serialize($value);
$result = file_put_contents($filename,$data);
if($result) {
return true;
}else {
return false;
}
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function rm($name) {
return unlink($this->filename($name));
}
/**
* 清除缓存
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function clear() {
$path = $this->_temp;
$files = scandir($path);
if($files){
foreach($files as $file){
if ($file != '.' && $file != '..' && is_dir($path.$file) ){
array_map( 'unlink', glob( $path.$file.'/*.*' ) );
}elseif(is_file($path.$file)){
unlink( $path . $file );
}
}
return true;
}
return false;
}
}
Redis.php
/**
* Created by PhpStorm.
* User: 申大侠
* Date: 2019/4/16
* Time: 11:30
*/
namespace Mode\Cache\Driver;
use Mode\Strategy;
class Redis extends Strategy{
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options=array()) {
$this->handler = new \Redis;
$this->handler->connect('127.0.0.1', 6379); //连接Redis
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name) {
$value = $this->handler->get($name);
$jsonData = json_decode( $value, true );
return ($jsonData === NULL) ? $value : $jsonData; //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer $expire 有效时间(秒)
* @return boolean
*/
public function set($name, $value, $expire = null) {
if(is_null($expire)) {
$expire = 100;
}
//对数组/对象数据进行缓存处理,保证数据完整性
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if(is_int($expire) && $expire) {
$result = $this->handler->setex($name, $expire, $value);
}else{
$result = $this->handler->set($name, $value);
}
return $result;
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function rm($name) {
$this->handler->delete($name);
}
/**
* 清除缓存
* @access public
* @return boolean
*/
public function clear() {
return $this->handler->flushDB();
}
}
代码包下载地址:https://download.csdn.net/download/yilukuangpao/11125387