TP5.0 Redis(單例模式)(原)
阿新 • • 發佈:2018-12-12
看到好多面試都問設計模式,我就簡單的瞭解了一下,順便把之前封裝好的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