From d3a0007bb45726e9c1ecd73fb97a82a2b8524a62 Mon Sep 17 00:00:00 2001 From: "756684177@qq.com" <756684177@qq.com> Date: Tue, 1 Nov 2016 17:32:04 +0800 Subject: [PATCH] =?UTF-8?q?Redis=20=E4=B8=BB=E4=BB=8E=E5=A4=8D=E5=88=B6Mas?= =?UTF-8?q?ter=E5=92=8CSlave?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Home/Common/function.php | 54 ++++++++++++++++++- .../Controller/DataBaseController.class.php | 28 ++++++++-- .../Home/Controller/IndexController.class.php | 10 ++++ ThinkPHP/Library/Org/Util/Car.class.php | 48 +++++++++++++++++ ...nce..class.php => RedisInstance.class.php} | 54 ++++++++++++++----- 5 files changed, 174 insertions(+), 20 deletions(-) create mode 100644 ThinkPHP/Library/Org/Util/Car.class.php rename ThinkPHP/Library/Org/Util/{RedisInstance..class.php => RedisInstance.class.php} (69%) diff --git a/Backend/Home/Common/function.php b/Backend/Home/Common/function.php index 22b9859..8ae2b86 100644 --- a/Backend/Home/Common/function.php +++ b/Backend/Home/Common/function.php @@ -40,4 +40,56 @@ function cli_test() sleep(3); } echo 'done'; -} \ No newline at end of file +} + +/** + * 匿名函数也叫闭包函数(closures允许创建一个没有指定没成的函数,最经常用作回调函数参数的值。 + * 闭包函数没有函数名称,直接在function()传入变量即可 使用时将定义的变量当作函数来处理 + */ +function everyFunction(){ + $message = 'hello'; + $example = function() use ($message){ + var_dump($message); + }; + echo $example(); + $message = 'world'; + //输出hello 因为继承变量的值的时候是函数定义的时候而不是 函数被调用的时候 + echo $example(); + //重置为hello + $message = 'hello'; + //此处传引用 + $example = function() use(&$message){ + var_dump($message); + }; + echo $example(); + //输出hello + $message = 'world'; + echo $example(); + //此处输出world + //闭包函数也用于正常的传值 + $message = 'hello'; + $example = function ($data) use ($message){ + return "{$data},{$message}"; + }; + + echo $example('world'); +} + + + + + + + + + + + + + + + + + + + diff --git a/Backend/Home/Controller/DataBaseController.class.php b/Backend/Home/Controller/DataBaseController.class.php index 1aee8ac..89a25f2 100644 --- a/Backend/Home/Controller/DataBaseController.class.php +++ b/Backend/Home/Controller/DataBaseController.class.php @@ -356,13 +356,31 @@ public function addAll($dataList, $options = array(), $replace = false) } public function test(){ - $aa = '4001:user_id'; - $messageId = explode(':',$aa); - switch($messageId[0]){ - case 4001: + $redis = RedisInstance::Instance(); + $conn = $redis->connect('127.0.0.1', '6379000'); + if($conn == true){ + var_dump($conn); + }else{ + echo 'Redis server went away'; + } + var_dump($redis); + + } + public function test2(){ + $redis = RedisInstance::LocationInstance(); + var_dump($redis); + $newRedis= new \Redis(); + var_dump($newRedis); + die; + if($redis != false){ + var_dump($redis->keys('*')); + echo '11111111'; + }else{ + echo '00000000'; } - echo $messageId[1]; + + } } diff --git a/Backend/Home/Controller/IndexController.class.php b/Backend/Home/Controller/IndexController.class.php index 459f7ad..ef0233b 100644 --- a/Backend/Home/Controller/IndexController.class.php +++ b/Backend/Home/Controller/IndexController.class.php @@ -2,6 +2,7 @@ namespace Home\Controller; use Home\Controller\BaseController; +use Org\Util\Car; use Org\Util\RedisInstance; use Org\Util\Tree; use Org\Util\UserAgent; @@ -159,4 +160,13 @@ public function getSessionRedis(){ echo session_name().'='.session_id(); die; } + + public function everyFunction(){ + $carObj = new Car(); + var_dump($carObj); + $carObj->add('butter',1); + $carObj->add('milk',4); + $carObj->add('eggs',9); + print_r($carObj->getTotal(0.05)); + } } \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Util/Car.class.php b/ThinkPHP/Library/Org/Util/Car.class.php new file mode 100644 index 0000000..beddd01 --- /dev/null +++ b/ThinkPHP/Library/Org/Util/Car.class.php @@ -0,0 +1,48 @@ +products[$product] = $quantity; + } + + public function getQuantity($product) + { + //是否定义了 + return isset($this->products[$product]) ? $this->products[$product] : FALSE; + } + + public function getTotal($tax) + { + $total = 0.0; + $callback = function ($quantity, $product) use ($tax, &$total) { + //constant 返回常量的值 + //__class__返回类名 + $price = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); + + $total += ($price * $quantity) * ($tax + 1.0); + }; + //array_walk() 函数对数组中的每个元素应用用户自定义函数。在函数中,数组的键名和键值是参数 + array_walk($this->products, $callback); + //回调匿名函数 + return round($total, 2); + } +} \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Util/RedisInstance..class.php b/ThinkPHP/Library/Org/Util/RedisInstance.class.php similarity index 69% rename from ThinkPHP/Library/Org/Util/RedisInstance..class.php rename to ThinkPHP/Library/Org/Util/RedisInstance.class.php index 092c2c1..ffead63 100644 --- a/ThinkPHP/Library/Org/Util/RedisInstance..class.php +++ b/ThinkPHP/Library/Org/Util/RedisInstance.class.php @@ -22,6 +22,7 @@ class RedisInstance * @var */ private static $_connectSource; + /** * 私有化构造函数,防止类外实例化 * RedisConnect constructor. @@ -29,6 +30,7 @@ class RedisInstance private function __construct() { } + /** * 单例方法,用于访问实例的公共的静态方法 * 这个只是一个实例 @@ -38,15 +40,16 @@ private function __construct() */ public static function Instance() { - try{ + try { if (!(static::$_instance instanceof \Redis)) { static::$_instance = new \Redis(); } return static::$_instance; - }catch (\Exception $e){ + } catch (\Exception $e) { return false; } } + /** * 单例方法,用于访问Master实例的公共的静态方法 * @return \Redis @@ -54,14 +57,17 @@ public static function Instance() */ public static function MasterInstance() { - try{ - self::Instance()->connect('121.41.88.209', '63789'); + try { + $_connectSource = self::Instance()->connect('121.41.88.209', '63789'); + if ($_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error. self::Instance()->auth('tinywanredis'); + return static::$_instance; - }catch (\Exception $e){ - return false; + } catch (\Exception $e) { + return FALSE; } } + /** * Slave1 实例 * @return null @@ -69,13 +75,15 @@ public static function MasterInstance() */ public static function SlaveOneInstance() { - try{ + try { self::Instance()->connect('121.41.88.209', '63788'); + if (self::$_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error. return static::$_instance; - }catch (\Exception $e){ + } catch (\Exception $e) { return false; } } + /** * Slave2 实例 * @return null @@ -83,13 +91,31 @@ public static function SlaveOneInstance() */ public static function SlaveTwoInstance() { - try{ + try { self::Instance()->connect('121.41.88.209', '63700'); + if (self::$_connectSource === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error. return static::$_instance; - }catch (\Exception $e){ + } catch (\Exception $e) { return false; } } + + /** + * Slave2 实例 + * @return null + * @static + */ + public static function LocationInstance() + { + try { + if (self::Instance()->connect('127.0.0.1', '6379') === FALSE) return FALSE; //@return bool TRUE on success, FALSE on error. + if (self::Instance()->ping() != '+PONG') return FALSE; + return static::$_instance; + } catch (\Exception $e) { + return false; + } + } + /** * Redis数据库是否连接成功 * @return bool|string @@ -97,18 +123,17 @@ public static function SlaveTwoInstance() public static function connect() { // 如果连接资源不存在,则进行资源连接 - if (!self::$_connectSource) - { + if (!self::$_connectSource) { //@return bool TRUE on success, FALSE on error. self::$_connectSource = self::Instance()->connect('121.41.88.209', '63789'); // 没有资源返回 - if (!self::$_connectSource) - { + if (!self::$_connectSource) { return 'Redis Server Connection Fail'; } } return self::$_connectSource; } + /** * 私有化克隆函数,防止类外克隆对象 */ @@ -116,6 +141,7 @@ private function __clone() { // TODO: Implement __clone() method. } + /** * @return \Redis * @static