看到好多面試都問設計模式,我就簡單的瞭解了一下,順便把之前封裝好的Reis做了一次修改.
單例模式(Singleton Pattern 單件模式或單元素模式)
單例模式確保某個類只有一個實例,而且自行實例化並向整個系統提供這個實例。
單例模式有以下3個特點:
1 . 它必須有一個構造函數,而且構造函數必須爲私有
2.必須有一個保存實例的靜態成員變量
3.擁有一個訪問這個實例的公共的靜態方法
爲什麼使用單例模式?
PHP一個主要應用場合就是應用程序與數據庫打交道的場景,在一個應用中會存在大量的數據庫操作,針對數據庫句柄連接數據庫的行爲,使用單例模式可以避免大量的new操作。因爲每一次new操作都會消耗系統和內存的資源。
單例模式下面上代碼:
<?php
/**
* Created by PhpStorm.
* User: luxiao
* Date: 2017/4/19
* Time: 16:21
*/
namespace My;
class RedisPackage
{
private static $handler = null;
private static $_instance = null;
private static $options = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'expire' => 0,
'persistent' => false,
'prefix' => '',
];
private function __construct($options = [])
{
if (!extension_loaded('redis')) {
throw new \BadFunctionCallException('not support: redis'); //判斷是否有擴展
}
if (!empty($options)) {
self::$options = array_merge(self::$options, $options);
}
$func = self::$options['persistent'] ? 'pconnect' : 'connect'; //長鏈接
self::$handler = new \Redis;
self::$handler->$func(self::$options['host'], self::$options['port'], self::$options['timeout']);
if ('' != self::$options['password']) {
self::$handler->auth(self::$options['password']);
}
if (0 != self::$options['select']) {
self::$handler->select(self::$options['select']);
}
}
/**
* @return RedisPackage|null 對象
*/
public static function getInstance()
{
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* 禁止外部克隆
*/
public function __clone()
{
trigger_error('Clone is not allow!',E_USER_ERROR);
}
/**
* 寫入緩存
* @param string $key 鍵名
* @param string $value 鍵值
* @param int $exprie 過期時間 0:永不過期
* @return bool
*/
public static function set($key, $value, $exprie = 0)
{
if ($exprie == 0) {
$set = self::$handler->set($key, $value);
} else {
$set = self::$handler->setex($key, $exprie, $value);
}
return $set;
}
/**
* 讀取緩存
* @param string $key 鍵值
* @return mixed
*/
public static function get($key)
{
$fun = is_array($key) ? 'Mget' : 'get';
return self::$handler->{$fun}($key);
}
/**
* 獲取值長度
* @param string $key
* @return int
*/
public static function lLen($key)
{
return self::$handler->lLen($key);
}
/**
* 將一個或多個值插入到列表頭部
* @param $key
* @param $value
* @return int
*/
public static function LPush($key, $value)
{
return self::$handler->lPush($key, $value);
}
/**
* 移出並獲取列表的第一個元素
* @param string $key
* @return string
*/
public static function lPop($key)
{
return self::$handler->lPop($key);
}
}
基類控制器Base.php
<?php
/**
* Created by PhpStorm.
* User: luxiao
* Date: 2017/4/20
* Time: 14:39
*/
namespace app\index\controller;
use think\Controller;
use My\RedisPackage;
class Base extends Controller
{
protected static $redis;
public function __construct()
{
parent::__construct();
self::$redis=RedisPackage::getInstance();
}
}
Redis控制器 Redis.php
<?php
/**
* Created by PhpStorm.
* User: luxiao
* Date: 2017/4/19
* Time: 14:39
*/
namespace app\index\controller;
use app\index\controller\Base;
class Redis extends Base
{
function redis()
{
self::$redis->set('zai','肖哥');
echo self::$redis->get('zai');
}
}
轉載:https://www.cnblogs.com/MyIsLu/p/6868473.html