cache_factory.class.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * cache_factory.class.php 缓存工厂类
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-6-1
  8. */
  9. final class cache_factory {
  10. /**
  11. * 当前缓存工厂类静态实例
  12. */
  13. private static $cache_factory;
  14. /**
  15. * 缓存配置列表
  16. */
  17. protected $cache_config = array();
  18. /**
  19. * 缓存操作实例化列表
  20. */
  21. protected $cache_list = array();
  22. /**
  23. * 构造函数
  24. */
  25. public function __construct() {
  26. }
  27. /**
  28. * 返回当前终级类对象的实例
  29. * @param $cache_config 缓存配置
  30. * @return object
  31. */
  32. public static function get_instance($cache_config = '') {
  33. if(cache_factory::$cache_factory == '' || $cache_config !='') {
  34. cache_factory::$cache_factory = new cache_factory();
  35. if(!empty($cache_config)) {
  36. cache_factory::$cache_factory->cache_config = $cache_config;
  37. }
  38. }
  39. return cache_factory::$cache_factory;
  40. }
  41. /**
  42. * 获取缓存操作实例
  43. * @param $cache_name 缓存配置名称
  44. */
  45. public function get_cache($cache_name) {
  46. if(!isset($this->cache_list[$cache_name]) || !is_object($this->cache_list[$cache_name])) {
  47. $this->cache_list[$cache_name] = $this->load($cache_name);
  48. }
  49. return $this->cache_list[$cache_name];
  50. }
  51. /**
  52. * 加载缓存驱动
  53. * @param $cache_name 缓存配置名称
  54. * @return object
  55. */
  56. public function load($cache_name) {
  57. $object = null;
  58. if(isset($this->cache_config[$cache_name]['type'])) {
  59. switch($this->cache_config[$cache_name]['type']) {
  60. case 'file' :
  61. $object = pc_base::load_sys_class('cache_file');
  62. break;
  63. case 'memcache' :
  64. define('MEMCACHE_HOST', $this->cache_config[$cache_name]['hostname']);
  65. define('MEMCACHE_PORT', $this->cache_config[$cache_name]['port']);
  66. define('MEMCACHE_TIMEOUT', $this->cache_config[$cache_name]['timeout']);
  67. define('MEMCACHE_DEBUG', $this->cache_config[$cache_name]['debug']);
  68. $object = pc_base::load_sys_class('cache_memcache');
  69. break;
  70. case 'apc' :
  71. $object = pc_base::load_sys_class('cache_apc');
  72. break;
  73. default :
  74. $object = pc_base::load_sys_class('cache_file');
  75. }
  76. } else {
  77. $object = pc_base::load_sys_class('cache_file');
  78. }
  79. return $object;
  80. }
  81. }
  82. ?>