CacheAdapterPhps.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. class LtCacheAdapterPhps implements LtCacheAdapter
  3. {
  4. public function connect($hostConf)
  5. {
  6. $fileStore = new LtStoreFile;
  7. if (isset($hostConf['host']) && is_string($hostConf['host']))
  8. {
  9. $fileStore->cacheFileRoot = $hostConf['host'];
  10. $fileStore->prefix = 'Ltcache-phps-';
  11. $fileStore->init();
  12. return $fileStore;
  13. }
  14. else
  15. {
  16. trigger_error("Must set [host]");
  17. return false;
  18. }
  19. }
  20. public function add($key, $value, $ttl = 0, $tableName, $connectionResource)
  21. {
  22. return $connectionResource->add($this->getRealKey($tableName, $key), $this->valueToString($value), $ttl);
  23. }
  24. public function del($key, $tableName, $connectionResource)
  25. {
  26. return $connectionResource->del($this->getRealKey($tableName, $key));
  27. }
  28. public function get($key, $tableName, $connectionResource)
  29. {
  30. return $this->stringToValue($connectionResource->get($this->getRealKey($tableName, $key)));
  31. }
  32. public function update($key, $value, $ttl = 0, $tableName, $connectionResource)
  33. {
  34. return $connectionResource->update($this->getRealKey($tableName, $key), $this->valueToString($value), $ttl);
  35. }
  36. protected function getRealKey($tableName, $key)
  37. {
  38. return $tableName . "-" . $key;
  39. }
  40. protected function valueToString($value)
  41. {
  42. return serialize($value);
  43. }
  44. protected function stringToValue($str)
  45. {
  46. return unserialize($str);
  47. }
  48. }