Cache.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Class to store arbitrary data objects in smarty cache
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2010 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Cache
  9. */
  10. class GitPHP_Cache
  11. {
  12. /**
  13. * Cache strategy
  14. *
  15. * @var GitPHP_CacheStrategy_Interface
  16. */
  17. protected $strategy;
  18. /**
  19. * Cache lifetime in seconds
  20. *
  21. * @var int
  22. */
  23. protected $lifetime = 86400;
  24. /**
  25. * Constructor
  26. *
  27. * @param GitPHP_CacheStrategy_Interface $strategy cache strategy
  28. */
  29. public function __construct(GitPHP_CacheStrategy_Interface $strategy)
  30. {
  31. if (!$strategy)
  32. throw new Exception('Cache strategy is required');
  33. $this->SetStrategy($strategy);
  34. }
  35. /**
  36. * Set the cache strategy
  37. *
  38. * @param GitPHP_CacheStrategy_Interface $strategy cache strategy
  39. */
  40. public function SetStrategy(GitPHP_CacheStrategy_Interface $strategy)
  41. {
  42. if (!$strategy)
  43. return;
  44. $this->strategy = $strategy;
  45. }
  46. /**
  47. * Gets the cache lifetime
  48. *
  49. * @return int cache lifetime in seconds
  50. */
  51. public function GetLifetime()
  52. {
  53. return $this->lifetime;
  54. }
  55. /**
  56. * Sets the cache lifetime
  57. *
  58. * @param int $lifetime cache lifetime in seconds
  59. */
  60. public function SetLifetime($lifetime)
  61. {
  62. if (!is_int($lifetime))
  63. return;
  64. $this->lifetime = $lifetime;
  65. }
  66. /**
  67. * Get an item from the cache
  68. *
  69. * @param string $key cache key
  70. * @return mixed the cached object, or false
  71. */
  72. public function Get($key)
  73. {
  74. if (empty($key))
  75. return false;
  76. return $this->strategy->Get($key);
  77. }
  78. /**
  79. * Set an item in the cache
  80. *
  81. * @param string $key cache key
  82. * @param mixed $value value
  83. * @param int $lifetime override the lifetime for this data
  84. */
  85. public function Set($key, $value, $lifetime = null)
  86. {
  87. if (empty($key) || empty($value))
  88. return;
  89. if ($lifetime === null)
  90. $lifetime = $this->lifetime;
  91. $this->strategy->Set($key, $value, $lifetime);
  92. }
  93. /**
  94. * Tests if a key is cached
  95. *
  96. * @param string $key cache key
  97. * @return boolean true if cached, false otherwise
  98. */
  99. public function Exists($key)
  100. {
  101. if (empty($key))
  102. return false;
  103. return $this->strategy->Exists($key);
  104. }
  105. /**
  106. * Delete an item from the cache
  107. *
  108. * @param string $key cache key
  109. */
  110. public function Delete($key)
  111. {
  112. if (empty($key))
  113. return;
  114. $this->strategy->Delete($key);
  115. }
  116. /**
  117. * Clear the cache
  118. */
  119. public function Clear()
  120. {
  121. $this->strategy->Clear();
  122. }
  123. }