PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix-legacy/plugins/kvdb/file2/file2.kvdriver.php

http://github.com/jelix/jelix
PHP | 292 lines | 180 code | 50 blank | 62 comment | 21 complexity | 387244bf63ef7162f17244066e5d1f53 MD5 | raw file
Possible License(s): BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage kvdb_plugin
  5. *
  6. * @author Yannick Le Guédart
  7. * @contributor Laurent Jouanneau
  8. *
  9. * @copyright 2009 Yannick Le Guédart, 2010 Laurent Jouanneau
  10. *
  11. * @see http://jelix.org
  12. * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  13. */
  14. /**
  15. * @deprecated TO REMOVE
  16. */
  17. class file2KVDriver extends jKVDriver
  18. {
  19. protected $dir;
  20. /**
  21. * "Connects" to the fileServer.
  22. *
  23. * @return fileServer object
  24. */
  25. protected function _connect()
  26. {
  27. return new fileServer(jApp::tempPath('filekv'));
  28. }
  29. protected function _disconnect()
  30. {
  31. }
  32. public function get($key)
  33. {
  34. return $this->_connection->get($key);
  35. }
  36. public function set($key, $value)
  37. {
  38. return $this->_connection->set(
  39. $key,
  40. $value
  41. );
  42. }
  43. public function insert($key, $value)
  44. {
  45. $val = $this->_connection->get($key);
  46. if ($val !== false) {
  47. return false;
  48. }
  49. return $this->_connection->set(
  50. $key,
  51. $value
  52. );
  53. }
  54. public function replace($key, $value)
  55. {
  56. $val = $this->_connection->get($key);
  57. if ($val === false) {
  58. return false;
  59. }
  60. return $this->_connection->set(
  61. $key,
  62. $value
  63. );
  64. }
  65. public function delete($key)
  66. {
  67. return $this->_connection->delete($key);
  68. }
  69. public function flush()
  70. {
  71. return $this->_connection->flush();
  72. }
  73. public function append($key, $value)
  74. {
  75. $val = $this->_connection->get($key);
  76. if ($val === false) {
  77. return false;
  78. }
  79. $val .= $value;
  80. if ($this->_connection->set(
  81. $key,
  82. $val
  83. )) {
  84. return $val;
  85. }
  86. return false;
  87. }
  88. public function prepend($key, $value)
  89. {
  90. $val = $this->_connection->get($key);
  91. if ($val === false) {
  92. return false;
  93. }
  94. $val = $value.$val;
  95. if ($this->_connection->set(
  96. $key,
  97. $val
  98. )) {
  99. return $val;
  100. }
  101. return false;
  102. }
  103. public function increment($key, $incr = 1)
  104. {
  105. $val = $this->_connection->get($key);
  106. if ($val === false || !is_numeric($val)) {
  107. return false;
  108. }
  109. $val += $incr;
  110. if ($this->_connection->set(
  111. $key,
  112. $val
  113. )) {
  114. return $val;
  115. }
  116. return false;
  117. }
  118. public function decrement($key, $decr = 1)
  119. {
  120. $val = $this->_connection->get($key);
  121. if ($val === false || !is_numeric($val)) {
  122. return false;
  123. }
  124. $val -= $decr;
  125. if ($this->_connection->set(
  126. $key,
  127. $val
  128. )) {
  129. return $val;
  130. }
  131. return false;
  132. }
  133. }
  134. class fileServer
  135. {
  136. protected $dir;
  137. public function __construct($directory)
  138. {
  139. $this->dir = $directory;
  140. // Create temp kvFile directory if necessary
  141. if (! file_exists($this->dir)) {
  142. jFile::createDir($this->dir);
  143. }
  144. }
  145. /**
  146. * set.
  147. *
  148. * @param string $key a key (unique name) to identify the cached info
  149. * @param mixed $value the value to cache
  150. * @param int $ttl how many seconds will the info be cached
  151. *
  152. * @return bool whether the action was successful or not
  153. */
  154. public function set($key, $value, $ttl = 0)
  155. {
  156. $r = false;
  157. if ($fl = @fopen($this->dir.'/.flock', 'w+')) {
  158. if (flock($fl, LOCK_EX)) {
  159. // mutex zone
  160. $md5 = md5($key);
  161. $subdir = $md5[0].$md5[1];
  162. if (!file_exists($this->dir.'/'.$subdir)) {
  163. jFile::createDir($this->dir.'/'.$subdir);
  164. }
  165. // write data to cache
  166. $fn = $this->dir.'/'.$subdir.'/'.$md5;
  167. if ($f = @gzopen($fn.'.tmp', 'w')) {
  168. // write temporary file
  169. fputs($f, base64_encode(serialize($value)));
  170. fclose($f);
  171. // change time of the file to the expiry time
  172. @touch("{$fn}.tmp", time() + $ttl);
  173. // rename the temporary file
  174. $r = @rename("{$fn}.tmp", $fn);
  175. chmod($fn, jApp::config()->chmodFile);
  176. }
  177. // end of mutex zone
  178. flock($fl, LOCK_UN);
  179. }
  180. }
  181. return $r;
  182. }
  183. /**
  184. * get.
  185. *
  186. * @param string $key the key (unique name) that identify the cached info
  187. *
  188. * @return mixed|false false if the cached info does not exist or has expired
  189. * or the data if the info exists and is valid
  190. */
  191. public function get($key)
  192. {
  193. $r = false;
  194. // the name of the file
  195. $md5 = md5($key);
  196. $subdir = $md5[0].$md5[1];
  197. $fn = $this->dir.'/'.$subdir.'/'.$md5;
  198. // file does not exists
  199. if (!file_exists($fn)) {
  200. return false;
  201. }
  202. // data has expired => delete file and return false
  203. if (@filemtime($fn) < time()) {
  204. @unlink($fn);
  205. return false;
  206. }
  207. // date is valid
  208. if ($f = @gzopen($fn, 'rb')) {
  209. $r = '';
  210. while ($read = fread($f, 1024)) {
  211. $r .= $read;
  212. }
  213. fclose($f);
  214. }
  215. // return cached info
  216. return @unserialize(base64_decode($r));
  217. }
  218. /**
  219. * delete.
  220. *
  221. * @param string $key a key (unique name) to identify the cached info
  222. *
  223. * @return bool whether the action was successful or not
  224. */
  225. public function delete($key)
  226. {
  227. // the name of the file
  228. $md5 = md5($key);
  229. $subdir = $md5[0].$md5[1];
  230. $fn = $this->dir.'/'.$subdir.'/'.$md5;
  231. return @unlink($fn);
  232. }
  233. /**
  234. * flush.
  235. *
  236. * @return bool whether the action was successful or not
  237. */
  238. public function flush()
  239. {
  240. return @unlink($this->dir);
  241. }
  242. }