You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.9 KiB

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