Sae.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: luofei614 <weibo.com/luofei614>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Storage\Driver;
  12. use Think\Storage;
  13. // SAE环境文件写入存储类
  14. class Sae extends Storage
  15. {
  16. /**
  17. * 架构函数
  18. * @access public
  19. */
  20. private $mc;
  21. private $kvs = array();
  22. private $htmls = array();
  23. private $contents = array();
  24. public function __construct()
  25. {
  26. if (!function_exists('memcache_init')) {
  27. header('Content-Type:text/html;charset=utf-8');
  28. exit('请在SAE平台上运行代码。');
  29. }
  30. $this->mc = @memcache_init();
  31. if (!$this->mc) {
  32. header('Content-Type:text/html;charset=utf-8');
  33. exit('您未开通Memcache服务,请在SAE管理平台初始化Memcache服务');
  34. }
  35. }
  36. /**
  37. * 获得SaeKv对象
  38. */
  39. private function getKv()
  40. {
  41. static $kv;
  42. if (!$kv) {
  43. $kv = new \SaeKV();
  44. if (!$kv->init()) {
  45. E('您没有初始化KVDB,请在SAE管理平台初始化KVDB服务');
  46. }
  47. }
  48. return $kv;
  49. }
  50. /**
  51. * 文件内容读取
  52. * @access public
  53. * @param string $filename 文件名
  54. * @return string
  55. */
  56. public function read($filename, $type = '')
  57. {
  58. switch (strtolower($type)) {
  59. case 'f':
  60. $kv = $this->getKv();
  61. if (!isset($this->kvs[$filename])) {
  62. $this->kvs[$filename] = $kv->get($filename);
  63. }
  64. return $this->kvs[$filename];
  65. default:
  66. return $this->get($filename, 'content', $type);
  67. }
  68. }
  69. /**
  70. * 文件写入
  71. * @access public
  72. * @param string $filename 文件名
  73. * @param string $content 文件内容
  74. * @return boolean
  75. */
  76. public function put($filename, $content, $type = '')
  77. {
  78. switch (strtolower($type)) {
  79. case 'f':
  80. $kv = $this->getKv();
  81. $this->kvs[$filename] = $content;
  82. return $kv->set($filename, $content);
  83. case 'html':
  84. $kv = $this->getKv();
  85. $content = time() . $content;
  86. $this->htmls[$filename] = $content;
  87. return $kv->set($filename, $content);
  88. default:
  89. $content = time() . $content;
  90. if (!$this->mc->set($filename, $content, MEMCACHE_COMPRESSED, 0)) {
  91. E(L('_STORAGE_WRITE_ERROR_') . ':' . $filename);
  92. } else {
  93. $this->contents[$filename] = $content;
  94. return true;
  95. }
  96. }
  97. }
  98. /**
  99. * 文件追加写入
  100. * @access public
  101. * @param string $filename 文件名
  102. * @param string $content 追加的文件内容
  103. * @return boolean
  104. */
  105. public function append($filename, $content, $type = '')
  106. {
  107. if ($old_content = $this->read($filename, $type)) {
  108. $content = $old_content . $content;
  109. }
  110. return $this->put($filename, $content, $type);
  111. }
  112. /**
  113. * 加载文件
  114. * @access public
  115. * @param string $_filename 文件名
  116. * @param array $vars 传入变量
  117. * @return void
  118. */
  119. public function load($_filename, $vars = null)
  120. {
  121. if (!is_null($vars)) {
  122. extract($vars, EXTR_OVERWRITE);
  123. }
  124. eval('?>' . $this->read($_filename));
  125. }
  126. /**
  127. * 文件是否存在
  128. * @access public
  129. * @param string $filename 文件名
  130. * @return boolean
  131. */
  132. public function has($filename, $type = '')
  133. {
  134. if ($this->read($filename, $type)) {
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. /**
  141. * 文件删除
  142. * @access public
  143. * @param string $filename 文件名
  144. * @return boolean
  145. */
  146. public function unlink($filename, $type = '')
  147. {
  148. switch (strtolower($type)) {
  149. case 'f':
  150. $kv = $this->getKv();
  151. unset($this->kvs[$filename]);
  152. return $kv->delete($filename);
  153. case 'html':
  154. $kv = $this->getKv();
  155. unset($this->htmls[$filename]);
  156. return $kv->delete($filename);
  157. default:
  158. unset($this->contents[$filename]);
  159. return $this->mc->delete($filename);
  160. }
  161. }
  162. /**
  163. * 读取文件信息
  164. * @access public
  165. * @param string $filename 文件名
  166. * @param string $name 信息名 mtime或者content
  167. * @return boolean
  168. */
  169. public function get($filename, $name, $type = '')
  170. {
  171. switch (strtolower($type)) {
  172. case 'html':
  173. if (!isset($this->htmls[$filename])) {
  174. $kv = $this->getKv();
  175. $this->htmls[$filename] = $kv->get($filename);
  176. }
  177. $content = $this->htmls[$filename];
  178. break;
  179. default:
  180. if (!isset($this->contents[$filename])) {
  181. $this->contents[$filename] = $this->mc->get($filename);
  182. }
  183. $content = $this->contents[$filename];
  184. }
  185. if (false === $content) {
  186. return false;
  187. }
  188. $info = array(
  189. 'mtime' => substr($content, 0, 10),
  190. 'content' => substr($content, 10),
  191. );
  192. return $info[$name];
  193. }
  194. }