PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ThinkPHP/Extend/Engine/Sae/Lib/Core/SaeMC.class.php

https://gitlab.com/cbdfocus/lukas
PHP | 120 lines | 95 code | 13 blank | 12 comment | 22 complexity | 6ab20b4e32929d9f63ded66e2d31def4 MD5 | raw file
  1. <?php
  2. /**
  3. * +---------------------------------------------------
  4. * | 对Memcache操作的封装,用于文件缓存
  5. * +---------------------------------------------------
  6. * @author luofei614<www.3g4k.com>
  7. */
  8. if (!class_exists('SaeMC')) {
  9. //[sae]对Memcache操作的封装,编译缓存,模版缓存存入Memcache中(非wrappers的形式)
  10. class SaeMC {
  11. static public $handler;
  12. static private $current_include_file = null;
  13. static private $contents = array();
  14. static private $filemtimes = array();
  15. //设置文件内容
  16. static public function set($filename, $content) {
  17. if(SAE_RUNTIME){
  18. file_put_contents($filename, $content);
  19. return ;
  20. }
  21. $time=time();
  22. self::$handler->set($_SERVER['HTTP_APPVERSION'] . '/' . $filename, $time . $content, MEMCACHE_COMPRESSED, 0);
  23. self::$contents[$filename]=$content;
  24. self::$filemtimes[$filename]=$time;
  25. }
  26. //载入文件
  27. static public function include_file($_filename,$_vars=null) {
  28. if(!is_null($_vars))
  29. extract($_vars, EXTR_OVERWRITE);
  30. if(SAE_RUNTIME){
  31. include $_filename;
  32. return ;
  33. }
  34. self::$current_include_file = 'saemc://' . $_SERVER['HTTP_APPVERSION'] . '/' . $_filename;
  35. $_content = isset(self::$contents[$_filename]) ? self::$contents[$_filename] : self::getValue($_filename, 'content');
  36. if (!$_content)
  37. exit('<br /><b>SAE_Parse_error</b>: failed to open stream: No such file ' . self::$current_include_file);
  38. if (@(eval(' ?>' . $_content)) === false)
  39. self::error();
  40. self::$current_include_file = null;
  41. unset(self::$contents[$_filename]); //释放内存
  42. }
  43. static private function getValue($filename, $type='mtime') {
  44. $content = self::$handler->get($_SERVER['HTTP_APPVERSION'] . '/' . $filename);
  45. if (!$content)
  46. return false;
  47. $ret = array(
  48. 'mtime' => substr($content, 0, 10),
  49. 'content' => substr($content, 10)
  50. );
  51. self::$contents[$filename] = $ret['content'];
  52. self::$filemtimes[$filename] = $ret['mtime'];
  53. return $ret[$type];
  54. }
  55. //获得文件修改时间
  56. static public function filemtime($filename) {
  57. if(SAE_RUNTIME){
  58. return filemtime($filename);
  59. }
  60. if (!isset(self::$filemtimes[$filename]))
  61. return self::getValue($filename, 'mtime');
  62. return self::$filemtimes[$filename];
  63. }
  64. //删除文件
  65. static public function unlink($filename) {
  66. if(SAE_RUNTIME){
  67. unlink($filename);//在SAE上会参数报错,方便开发者找到错误
  68. return ;
  69. }
  70. if (isset(self::$contents[$filename]))
  71. unset(self::$contents[$filename]);
  72. if (isset(self::$filemtimes[$filename]))
  73. unset(self::$filemtimes[$filename]);
  74. return self::$handler->delete($_SERVER['HTTP_APPVERSION'] . '/' . $filename);
  75. }
  76. static public function file_exists($filename) {
  77. if(SAE_RUNTIME){
  78. return file_exists($filename);
  79. }
  80. return self::filemtime($filename) === false ? false : true;
  81. }
  82. static function error() {
  83. $error = error_get_last();
  84. if (!is_null($error) && strpos($error['file'], 'eval()') !== false) {
  85. if(!class_exists('Think')){
  86. ob_end_clean();
  87. if(C('OUTPUT_ENCODE')){
  88. $zlib = ini_get('zlib.output_compression');
  89. if(empty($zlib)) ob_start('ob_gzhandler');
  90. }
  91. if(C('SMS_ALERT_ON')) Sms::send('程序出现致命错误,请在SAE日志中心查看详情',$error['message'].'[file:'.self::$current_include_file.'][line:'.$error['line'].']',Sms::ERR);
  92. exit("<br /><b>SAE_error</b>: {$error['message']} in <b>" . self::$current_include_file . "</b> on line <b>{$error['line']}</b><br />");
  93. }else{
  94. Think::appError($error['type'], $error['message'], self::$current_include_file, $error['line']);
  95. }
  96. }
  97. }
  98. }
  99. if(!SAE_RUNTIME) register_shutdown_function(array('SaeMC', 'error'));
  100. //[sae] 初始化memcache
  101. if(!SAE_RUNTIME){
  102. if (!(SaeMC::$handler = @(memcache_init()))) {
  103. header('Content-Type:text/html; charset=utf-8');
  104. exit('<div style=\'font-weight:bold;float:left;width:430px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;color:red;font-size:14px;font-family:Tahoma\'>您的Memcache还没有初始化,请登录SAE平台进行初始化~</div>');
  105. }
  106. }
  107. }