Store.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace GatewayWorker\Lib;
  3. /**
  4. * 存储类
  5. * 这里用memcache实现
  6. * @author walkor <walkor@workerman.net>
  7. */
  8. class Store
  9. {
  10. /**
  11. * 实例数组
  12. * @var array
  13. */
  14. protected static $instance = array();
  15. /**
  16. * 获取实例
  17. * @param string $config_name
  18. * @throws \Exception
  19. */
  20. public static function instance($config_name)
  21. {
  22. // memcached 驱动
  23. if(\Config\Store::$driver == \Config\Store::DRIVER_MC)
  24. {
  25. if(!isset(\Config\Store::$$config_name))
  26. {
  27. echo "\\Config\\Store::$config_name not set\n";
  28. throw new \Exception("\\Config\\Store::$config_name not set\n");
  29. }
  30. if(!isset(self::$instance[$config_name]))
  31. {
  32. if(extension_loaded('Memcached'))
  33. {
  34. self::$instance[$config_name] = new \Memcached;
  35. }
  36. elseif(extension_loaded('Memcache'))
  37. {
  38. self::$instance[$config_name] = new \Memcache;
  39. }
  40. else
  41. {
  42. sleep(2);
  43. exit("extension memcached is not installed\n");
  44. }
  45. foreach(\Config\Store::$$config_name as $address)
  46. {
  47. list($ip, $port) = explode(':', $address);
  48. self::$instance[$config_name] ->addServer($ip, $port);
  49. }
  50. }
  51. return self::$instance[$config_name];
  52. }
  53. // 文件驱动
  54. else
  55. {
  56. if(!isset(self::$instance[$config_name]))
  57. {
  58. // 关闭opcache
  59. ini_set('opcache.enable', false);
  60. self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\File($config_name);
  61. }
  62. return self::$instance[$config_name];
  63. }
  64. }
  65. }