Store.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. self::$instance[$config_name] = new \Memcache;
  32. foreach(\Config\Store::$$config_name as $address)
  33. {
  34. list($ip, $port) = explode(':', $address);
  35. self::$instance[$config_name] ->addServer($ip, $port);
  36. }
  37. }
  38. return self::$instance[$config_name];
  39. }
  40. // 文件驱动
  41. else
  42. {
  43. if(!isset(self::$instance[$config_name]))
  44. {
  45. self::$instance[$config_name] = new \Lib\StoreDriver\File($config_name);
  46. }
  47. return self::$instance[$config_name];
  48. }
  49. }
  50. }