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

看到好多面试都问设计模式,我就简单的了解了一下,顺便把之前封装好的Reis做了一次修改.

单例模式(Singleton Pattern 单件模式或单元素模式)

单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。

单例模式有以下3个特点:

1 . 它必须有一个构造函数,而且构造函数必须为私有

2.必须有一个保存实例的静态成员变量

3.拥有一个访问这个实例的公共的静态方法

为什么使用单例模式?

PHP一个主要应用场合就是应用程序与数据库打交道的场景,在一个应用中会存在大量的数据库操作,针对数据库句柄连接数据库的行为,使用单例模式可以避免大量的new操作。因为每一次new操作都会消耗系统和内存的资源。

单例模式下面上代码:

  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 appindexcontroller;
10 use thinkController;
11 use MyRedisPackage;
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 appindexcontroller;
10 
11 use appindexcontrollerBase;
12 
13 class Redis extends Base
14 {
15     function redis()
16     {
17         self::$redis->set('zai','肖哥');
18         echo self::$redis->get('zai');
19     }
20 }
原文地址:https://www.cnblogs.com/MyIsLu/p/6868473.html