RedisOP.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: scstf
  5. * Date: 2018/9/14
  6. * Time: 13:01
  7. */
  8. namespace Biz\Db\Redis;
  9. // ini_set('display_errors', 1);
  10. // error_reporting(E_ALL);
  11. class RedisOP
  12. {
  13. private $_redis;
  14. public $expire;
  15. private $timeout = 15;
  16. public function __construct()
  17. {
  18. if (class_exists ('Redis')) {
  19. $redis = new \Redis();
  20. $config = include ROOT_PATH . '/Config/Redis.php';
  21. if (!isset($config['host'])) return null;//throw new \ErrorException('服务器不存在!');
  22. $host = $config['host'];
  23. $port = isset($config['port']) ? $config['port'] : 6379;
  24. try{
  25. $redis->connect ($host, $port, $this->timeout);// or die('连接失败!');
  26. }catch (\ErrorException $e)
  27. {
  28. $this->_redis = null;
  29. }
  30. if (isset($config['password'])) {
  31. $password = $config['password'];
  32. $redis->auth ($password);
  33. }
  34. $this->_redis = $redis;
  35. $this->expire = 300;
  36. } else {
  37. $this->_redis = null;
  38. }
  39. }
  40. function get()
  41. {
  42. return $this->_redis;
  43. }
  44. }