Store.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // memcache 驱动
  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. self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\File($config_name);
  59. }
  60. return self::$instance[$config_name];
  61. }
  62. }
  63. }