Redis.php 816 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace app\common;
  3. use think\facade\Config;
  4. use think\cache\driver\Redis as TpRedis;
  5. /**
  6. * Description of Redis
  7. *
  8. * @author sgq
  9. */
  10. class Redis extends TpRedis {
  11. protected $db;
  12. protected static $instance = [];
  13. public function __construct($db) {
  14. $options = Config::get("cache.stores.redis");
  15. $this->db = $db;
  16. $options["select"] = $db;
  17. $this->options = array_merge($this->options, $options);
  18. parent::__construct();
  19. }
  20. public static function instance($db = 0) {
  21. if (!isset(self::$instance[$db])) {
  22. self::$instance[$db] = new self($db);
  23. }
  24. return self::$instance[$db];
  25. }
  26. public function __destruct() {
  27. self::$instance[$this->db]->close();
  28. unset(self::$instance[$this->db]);
  29. }
  30. }