| @@ 23-60 (lines=38) @@ | ||
| 20 | * THE SOFTWARE. |
|
| 21 | */ |
|
| 22 | ||
| 23 | trait TimeTrait { |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Relative cache time |
|
| 27 | * |
|
| 28 | * @var int |
|
| 29 | */ |
|
| 30 | protected $current_time; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * {@inheritdoc} |
|
| 34 | */ |
|
| 35 | public function getTime() { |
|
| 36 | ||
| 37 | return $this->current_time; |
|
| 38 | ||
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * {@inheritdoc} |
|
| 43 | */ |
|
| 44 | public function setTime($time = null) { |
|
| 45 | ||
| 46 | if ( is_null($time) ) $this->current_time = time(); |
|
| 47 | ||
| 48 | else if ( preg_match('/^[0-9]{10}$/', $time) ) $this->current_time = $time; |
|
| 49 | ||
| 50 | else { |
|
| 51 | ||
| 52 | throw new CacheException("Invalid time"); |
|
| 53 | ||
| 54 | } |
|
| 55 | ||
| 56 | return $this; |
|
| 57 | ||
| 58 | } |
|
| 59 | ||
| 60 | } |
|
| 61 | ||
| @@ 23-71 (lines=49) @@ | ||
| 20 | * THE SOFTWARE. |
|
| 21 | */ |
|
| 22 | ||
| 23 | trait TtlTrait { |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Default cache ttl |
|
| 27 | * |
|
| 28 | * @var int |
|
| 29 | */ |
|
| 30 | public static $default_ttl = 3600; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * Cache ttl (in seconds) |
|
| 34 | * |
|
| 35 | * @var int |
|
| 36 | */ |
|
| 37 | protected $ttl; |
|
| 38 | ||
| 39 | /** |
|
| 40 | * {@inheritdoc} |
|
| 41 | */ |
|
| 42 | final public function getTtl() { |
|
| 43 | ||
| 44 | return $this->ttl; |
|
| 45 | ||
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * {@inheritdoc} |
|
| 50 | */ |
|
| 51 | public function setTtl($ttl = null) { |
|
| 52 | ||
| 53 | if ( is_null($ttl) ) { |
|
| 54 | ||
| 55 | $this->ttl = self::$default_ttl; |
|
| 56 | ||
| 57 | } else if ( is_int($ttl) ) { |
|
| 58 | ||
| 59 | $this->ttl = $ttl; |
|
| 60 | ||
| 61 | } else { |
|
| 62 | ||
| 63 | throw new CacheException("Invalid time to live"); |
|
| 64 | ||
| 65 | } |
|
| 66 | ||
| 67 | return $this; |
|
| 68 | ||
| 69 | } |
|
| 70 | ||
| 71 | } |
|
| 72 | ||