CacheAdapterFile.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. class LtCacheAdapterFile implements LtCacheAdapter
  3. {
  4. public function connect($hostConf)
  5. {
  6. $fileStore = new LtStoreFile;
  7. $fileStore->prefix = 'LtCache-file';
  8. $fileStore->useSerialize = true;
  9. $fileStore->init();
  10. return $fileStore;
  11. }
  12. public function add($key, $value, $ttl = 0, $tableName, $connectionResource)
  13. {
  14. if (0 != $ttl)
  15. {
  16. $ttl += time();
  17. }
  18. if (true == $connectionResource->add($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value)))
  19. {
  20. return true;
  21. }
  22. else
  23. {
  24. if ($this->get($key,$tableName,$connectionResource))
  25. {
  26. return false;
  27. }
  28. else
  29. {
  30. $this->del($key,$tableName,$connectionResource);
  31. return $connectionResource->add($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value));
  32. }
  33. }
  34. }
  35. public function del($key, $tableName, $connectionResource)
  36. {
  37. return $connectionResource->del($this->getRealKey($tableName, $key));
  38. }
  39. public function get($key, $tableName, $connectionResource)
  40. {
  41. $cachedArray = $connectionResource->get($this->getRealKey($tableName, $key));
  42. if (is_array($cachedArray) && (0 == $cachedArray["ttl"] || $cachedArray["ttl"] > time()))
  43. {
  44. return $cachedArray["value"];
  45. }
  46. else
  47. {
  48. return false;
  49. }
  50. }
  51. public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
  52. {
  53. if (0 != $ttl)
  54. {
  55. $ttl += time();
  56. }
  57. return $connectionResource->update($this->getRealKey($tableName, $key), array("ttl" => $ttl, "value" => $value));
  58. }
  59. protected function getRealKey($tableName, $key)
  60. {
  61. return $tableName . "-" . $key;
  62. }
  63. }