File.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Lib\StoreDriver;
  3. /**
  4. *
  5. * 这里用php数组文件来存储数据,
  6. * 为了获取高性能需要用类似memcache的存储
  7. * @author 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. const CACHE_EXP_TIME = 1;
  22. /**
  23. * 构造函数
  24. * @param 配置名 $config_name
  25. */
  26. public function __construct($config_name)
  27. {
  28. $this->dataFile = \Config\Store::$storePath . "/$config_name.store.cache.php";
  29. if(!is_dir(\Config\Store::$storePath) && !mkdir(\Config\Store::$storePath, 0777, true))
  30. {
  31. throw new \Exception('cant not mkdir('.\Config\Store::$storePath.')');
  32. }
  33. if(!is_file($this->dataFile))
  34. {
  35. touch($this->dataFile);
  36. }
  37. $this->dataFileHandle = fopen($this->dataFile, 'r+');
  38. if(!$this->dataFileHandle)
  39. {
  40. throw new \Exception("can not fopen($this->dataFile, 'r+')");
  41. }
  42. }
  43. /**
  44. * 设置
  45. * @param string $key
  46. * @param mixed $value
  47. * @param int $ttl
  48. * @return number
  49. */
  50. public function set($key, $value, $ttl = 0)
  51. {
  52. flock($this->dataFileHandle, LOCK_EX);
  53. $this->readDataFromDisk();
  54. $this->dataCache[$key] = $value;
  55. $ret = $this->writeToDisk();
  56. flock($this->dataFileHandle, LOCK_UN);
  57. return $ret;
  58. }
  59. /**
  60. * 读取
  61. * @param string $key
  62. * @param bool $use_cache
  63. * @return Ambigous <NULL, multitype:>
  64. */
  65. public function get($key, $use_cache = true)
  66. {
  67. if(!$use_cache || time() - $this->lastCacheTime > self::CACHE_EXP_TIME)
  68. {
  69. flock($this->dataFileHandle, LOCK_EX);
  70. $this->readDataFromDisk();
  71. flock($this->dataFileHandle, LOCK_UN);
  72. }
  73. return isset($this->dataCache[$key]) ? $this->dataCache[$key] : null;
  74. }
  75. /**
  76. * 删除
  77. * @param string $key
  78. * @return number
  79. */
  80. public function delete($key)
  81. {
  82. flock($this->dataFileHandle, LOCK_EX);
  83. $this->readDataFromDisk();
  84. unset($this->dataCache[$key]);
  85. $ret = $this->writeToDisk();
  86. flock($this->dataFileHandle, LOCK_UN);
  87. return $ret;
  88. }
  89. /**
  90. * 自增
  91. * @param string $key
  92. * @return boolean|multitype:
  93. */
  94. public function increment($key)
  95. {
  96. flock($this->dataFileHandle, LOCK_EX);
  97. $this->readDataFromDisk();
  98. if(!isset($this->dataCache[$key]))
  99. {
  100. flock($this->dataFileHandle, LOCK_UN);
  101. return false;
  102. }
  103. $this->dataCache[$key] ++;
  104. $this->writeToDisk();
  105. flock($this->dataFileHandle, LOCK_UN);
  106. return $this->dataCache[$key];
  107. }
  108. /**
  109. * 写入磁盘
  110. * @return number
  111. */
  112. protected function writeToDisk()
  113. {
  114. return file_put_contents($this->dataFile, "<?php \n return " . var_export($this->dataCache, true). ';');
  115. }
  116. /**
  117. * 从磁盘读
  118. */
  119. protected function readDataFromDisk()
  120. {
  121. $cache = include $this->dataFile;
  122. if(is_array($cache))
  123. {
  124. $this->dataCache = $cache;
  125. }
  126. $this->lastCacheTime = time();
  127. }
  128. }