nosql.init.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Dao-Nosql
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * $Author:zhuli
  10. * $Dtime:2013-5-29
  11. ***********************************************************************************/
  12. class nosqlInit {
  13. private static $instance = array(); //单例模式获取nosql类
  14. private $nosql_type = array('MONGO', 'REDIS');
  15. /**
  16. * 获取Nosql对象
  17. * @param string $type
  18. */
  19. public function init($type = 'MONGO', $server = 'default') {
  20. $InitPHP_conf = InitPHP::getConfig(); //需要设置文件缓存目录
  21. $type = strtoupper($type);
  22. $type = (in_array($type, $this->nosql_type)) ? $type : 'MONGO';
  23. switch ($type) {
  24. case 'MONGO' :
  25. $instance_name = 'mongo_' . $server;
  26. if (isset(nosqlInit::$instance[$instance_name])) return nosqlInit::$instance[$instance_name];
  27. $mongo = $this->load_nosql('mongo.init.php', 'mongoInit', $server);
  28. $mongo->init($InitPHP_conf['mongo'][$server]);
  29. nosqlInit::$instance[$instance_name] = $mongo;
  30. return $mongo;
  31. break;
  32. case 'REDIS' :
  33. $instance_name = 'redis_' . $server;
  34. if (isset(nosqlInit::$instance[$instance_name])) return nosqlInit::$instance[$instance_name];
  35. $redis = $this->load_nosql('redis.init.php', 'redisInit', $server);
  36. $redis->init($InitPHP_conf['redis'][$server]);
  37. nosqlInit::$instance[$instance_name] = $redis;
  38. return $redis;
  39. break;
  40. }
  41. }
  42. /**
  43. * 加载不同NOSQL类文件
  44. * @param string $file 缓存文件名
  45. * @param string $class 缓存类名
  46. * @param String $server 服务器
  47. * @return obj
  48. */
  49. private function load_nosql($file, $class, $server) {
  50. if (nosqlInit::$instance['require'][$file] != TRUE) {
  51. require('driver/' . $file);
  52. nosqlInit::$instance['require'][$file] = TRUE;
  53. }
  54. $tag = $class . "_" . $server;
  55. if (!nosqlInit::$instance['class'][$tag]) {
  56. nosqlInit::$instance['class'][$tag] = new $class;
  57. return nosqlInit::$instance['class'][$tag];
  58. } else {
  59. return nosqlInit::$instance['class'][$tag];
  60. }
  61. }
  62. }