You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.5 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage Cache
  7. * @since 2010-03-10
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class CacheKey
  12. {
  13. protected $key;
  14. protected $params = '';
  15. protected $expire = 0;
  16. /**
  17. * @param string $key
  18. * @param mixed $params
  19. * @param iCacheable $cacheable
  20. * @return CacheKey
  21. */
  22. public function __construct($key, $params = array(), $cacheable)
  23. {
  24. $this->key = $key;
  25. if (!$cacheable instanceof iCacheable) {
  26. throw new GeneralException('CacheKey depends on iCacheable instance');
  27. }
  28. $this->cache = $cacheable->getCache();
  29. $this->expire = $cacheable->getKeyExpire($this->key);
  30. $this->params = (is_array($params)) ? implode('|', $params) : $params;
  31. }
  32. protected function getCacheKey()
  33. {
  34. $params = ($this->params) ? ('_' . $this->params) : '';
  35. return $this->key . $params;
  36. }
  37. protected function getExpire()
  38. {
  39. return $this->expire;
  40. }
  41. /**
  42. * @param int $expire
  43. */
  44. public function setExpire($expire)
  45. {
  46. $this->expire = $expire;
  47. }
  48. public function get()
  49. {
  50. return $this->cache->get($this->getCacheKey());
  51. }
  52. /**
  53. * @param mixed $value
  54. */
  55. public function set($value)
  56. {
  57. return $this->cache->set($this->getCacheKey(), $value, $this->expire);
  58. }
  59. public function del()
  60. {
  61. return $this->cache->del($this->getCacheKey());
  62. }
  63. }