File.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace GatewayWorker\Lib\StoreDriver;
  3. /**
  4. *
  5. * 这里用php数组文件来存储数据,
  6. * 为了获取高性能需要用类似memcache的存储
  7. * @author walkor <walkor@workerman.net>
  8. *
  9. */
  10. class File
  11. {
  12. // 为了避免频繁读取磁盘,增加了缓存机制
  13. protected $dataCache = array();
  14. // 上次缓存时间
  15. protected $lastCacheTime = 0;
  16. // 保存数据的文件
  17. protected $dataFile = '';
  18. // 打开文件的句柄
  19. protected $dataFileHandle = null;
  20. /**
  21. * 构造函数
  22. * @param 配置名 $config_name
  23. */
  24. public function __construct($config_name)
  25. {
  26. $this->dataFile = \Config\Store::$storePath . "/$config_name.store.cache.php";
  27. if(!is_dir(\Config\Store::$storePath) && !@mkdir(\Config\Store::$storePath, 0777, true))
  28. {
  29. // 可能目录已经被其它进程创建
  30. clearstatcache();
  31. if(!is_dir(\Config\Store::$storePath))
  32. {
  33. // 避免狂刷日志
  34. sleep(1);
  35. throw new \Exception('cant not mkdir('.\Config\Store::$storePath.')');
  36. }
  37. }
  38. if(!is_file($this->dataFile))
  39. {
  40. touch($this->dataFile);
  41. }
  42. $this->dataFileHandle = fopen(__FILE__, 'r');
  43. if(!$this->dataFileHandle)
  44. {
  45. throw new \Exception("can not fopen($this->dataFile, 'r')");
  46. }
  47. }
  48. /**
  49. * 设置
  50. * @param string $key
  51. * @param mixed $value
  52. * @param int $ttl
  53. * @return number
  54. */
  55. public function set($key, $value, $ttl = 0)
  56. {
  57. flock($this->dataFileHandle, LOCK_EX);
  58. $this->readDataFromDisk();
  59. $this->dataCache[$key] = $value;
  60. $ret = $this->writeToDisk();
  61. flock($this->dataFileHandle, LOCK_UN);
  62. return $ret;
  63. }
  64. /**
  65. * 读取
  66. * @param string $key
  67. * @param bool $use_cache
  68. * @return Ambigous <NULL, multitype:>
  69. */
  70. public function get($key, $use_cache = true)
  71. {
  72. flock($this->dataFileHandle, LOCK_EX);
  73. $this->readDataFromDisk();
  74. flock($this->dataFileHandle, LOCK_UN);
  75. return isset($this->dataCache[$key]) ? $this->dataCache[$key] : null;
  76. }
  77. /**
  78. * 删除
  79. * @param string $key
  80. * @return number
  81. */
  82. public function delete($key)
  83. {
  84. flock($this->dataFileHandle, LOCK_EX);
  85. $this->readDataFromDisk();
  86. unset($this->dataCache[$key]);
  87. $ret = $this->writeToDisk();
  88. flock($this->dataFileHandle, LOCK_UN);
  89. return $ret;
  90. }
  91. /**
  92. * 自增
  93. * @param string $key
  94. * @return boolean|multitype:
  95. */
  96. public function increment($key)
  97. {
  98. flock($this->dataFileHandle, LOCK_EX);
  99. $this->readDataFromDisk();
  100. if(!isset($this->dataCache[$key]))
  101. {
  102. flock($this->dataFileHandle, LOCK_UN);
  103. return false;
  104. }
  105. $this->dataCache[$key] ++;
  106. $this->writeToDisk();
  107. flock($this->dataFileHandle, LOCK_UN);
  108. return $this->dataCache[$key];
  109. }
  110. /**
  111. * 清零销毁存储数据
  112. */
  113. public function destroy()
  114. {
  115. @unlink($this->dataFile);
  116. }
  117. /**
  118. * 写入磁盘
  119. * @return number
  120. */
  121. protected function writeToDisk()
  122. {
  123. return file_put_contents($this->dataFile, "<?php \n return " . var_export($this->dataCache, true). ';');
  124. }
  125. /**
  126. * 从磁盘读
  127. */
  128. protected function readDataFromDisk()
  129. {
  130. $cache = include $this->dataFile;
  131. if(is_array($cache))
  132. {
  133. $this->dataCache = $cache;
  134. }
  135. $this->lastCacheTime = time();
  136. }
  137. }