Store.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Lib;
  3. /**
  4. * 存储类
  5. * 这里用memcache实现
  6. * @author 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. throw new \Exception('\Config\Store::$config_name not set');
  28. }
  29. if(!isset(self::$instance[$config_name]))
  30. {
  31. if(extension_loaded('Memcached'))
  32. {
  33. self::$instance[$config_name] = new \Memcached;
  34. }
  35. elseif(extension_loaded('Memcache'))
  36. {
  37. self::$instance[$config_name] = new \Memcache;
  38. }
  39. else
  40. {
  41. sleep(2);
  42. exit("extension memcached is not installed\n");
  43. }
  44. foreach(\Config\Store::$$config_name as $address)
  45. {
  46. list($ip, $port) = explode(':', $address);
  47. self::$instance[$config_name] ->addServer($ip, $port);
  48. }
  49. }
  50. return self::$instance[$config_name];
  51. }
  52. // 文件驱动
  53. else
  54. {
  55. if(!isset(self::$instance[$config_name]))
  56. {
  57. self::$instance[$config_name] = new \Lib\StoreDriver\File($config_name);
  58. }
  59. return self::$instance[$config_name];
  60. }
  61. }
  62. }