TP5.0 Redis(單例模式)(原)

看到好多面試都問設計模式,我就簡單的瞭解了一下,順便把以前封裝好的Reis作了一次修改.php

單例模式(Singleton Pattern 單件模式或單元素模式)面試

單例模式確保某個類只有一個實例,並且自行實例化並向整個系統提供這個實例。redis

單例模式有如下3個特色:數據庫

1 . 它必須有一個構造函數,並且構造函數必須爲私有設計模式

2.必須有一個保存實例的靜態成員變量緩存

3.擁有一個訪問這個實例的公共的靜態方法app

爲何使用單例模式?函數

PHP一個主要應用場合就是應用程序與數據庫打交道的場景,在一個應用中會存在大量的數據庫操做,針對數據庫句柄鏈接數據庫的行爲,使用單例模式能夠避免大量的new操做。由於每一次new操做都會消耗系統和內存的資源。spa

單例模式下面上代碼:設計

  1 <?php
  2 /**
  3  * Created by PhpStorm.
  4  * User: luxiao
  5  * Date: 2017/4/19
  6  * Time: 16:21
  7  */
  8 
  9 namespace My;
 10 
 11 class RedisPackage
 12 {
 13     private static $handler = null;
 14     private static $_instance = null;
 15     private static $options = [
 16         'host' => '127.0.0.1',
 17         'port' => 6379,
 18         'password' => '',
 19         'select' => 0,
 20         'timeout' => 0,
 21         'expire' => 0,
 22         'persistent' => false,
 23         'prefix' => '',
 24     ];
 25 
 26     private function __construct($options = [])
 27     {
 28         if (!extension_loaded('redis')) {
 29             throw new \BadFunctionCallException('not support: redis');      //判斷是否有擴展
 30         }
 31         if (!empty($options)) {
 32             self::$options = array_merge(self::$options, $options);
 33         }
 34         $func = self::$options['persistent'] ? 'pconnect' : 'connect';     //長連接
 35         self::$handler = new \Redis;
 36         self::$handler->$func(self::$options['host'], self::$options['port'], self::$options['timeout']);
 37 
 38         if ('' != self::$options['password']) {
 39             self::$handler->auth(self::$options['password']);
 40         }
 41 
 42         if (0 != self::$options['select']) {
 43             self::$handler->select(self::$options['select']);
 44         }
 45     }
 46 
 47 
 48     /**
 49      * @return RedisPackage|null 對象
 50      */
 51     public static function getInstance()
 52     {
 53         if (!(self::$_instance instanceof self)) {
 54             self::$_instance = new self();
 55         }
 56         return self::$_instance;
 57     }
 58 
 59     /**
 60      * 禁止外部克隆
 61      */
 62     public function __clone()
 63     {
 64         trigger_error('Clone is not allow!',E_USER_ERROR);
 65     }
 66 
 67     /**
 68      * 寫入緩存
 69      * @param string $key 鍵名
 70      * @param string $value 鍵值
 71      * @param int $exprie 過時時間 0:永不過時
 72      * @return bool
 73      */
 74 
 75 
 76     public static function set($key, $value, $exprie = 0)
 77     {
 78         if ($exprie == 0) {
 79             $set = self::$handler->set($key, $value);
 80         } else {
 81             $set = self::$handler->setex($key, $exprie, $value);
 82         }
 83         return $set;
 84     }
 85 
 86     /**
 87      * 讀取緩存
 88      * @param string $key 鍵值
 89      * @return mixed
 90      */
 91     public static function get($key)
 92     {
 93         $fun = is_array($key) ? 'Mget' : 'get';
 94         return self::$handler->{$fun}($key);
 95     }
 96 
 97     /**
 98      * 獲取值長度
 99      * @param string $key
100      * @return int
101      */
102     public static function lLen($key)
103     {
104         return self::$handler->lLen($key);
105     }
106 
107     /**
108      * 將一個或多個值插入到列表頭部
109      * @param $key
110      * @param $value
111      * @return int
112      */
113     public static function LPush($key, $value)
114     {
115         return self::$handler->lPush($key, $value);
116     }
117 
118     /**
119      * 移出並獲取列表的第一個元素
120      * @param string $key
121      * @return string
122      */
123     public static function lPop($key)
124     {
125         return self::$handler->lPop($key);
126     }
127 }

基類控制器Base.php

 1 <?php
 2 /**
 3  * Created by PhpStorm.
 4  * User: luxiao
 5  * Date: 2017/4/20
 6  * Time: 14:39
 7  */
 8 
 9 namespace app\index\controller;
10 use think\Controller;
11 use My\RedisPackage;
12 
13 class Base extends Controller
14 {
15     protected static $redis;
16 
17     public function __construct()
18     {
19         parent::__construct();
20             self::$redis=RedisPackage::getInstance();
21     }
22 }

Redis控制器 Redis.php

 1 <?php
 2 /**
 3  * Created by PhpStorm.
 4  * User: luxiao
 5  * Date: 2017/4/19
 6  * Time: 14:39
 7  */
 8 
 9 namespace app\index\controller;
10 
11 use app\index\controller\Base;
12 
13 class Redis extends Base
14 {
15     function redis()
16     {
17         self::$redis->set('zai','肖哥');
18         echo self::$redis->get('zai');
19     }
20 }
相關文章
相關標籤/搜索