locomotivemtl /
charcoal-cache
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Charcoal\Cache; |
||
| 4 | |||
| 5 | use InvalidArgumentException; |
||
| 6 | |||
| 7 | // From 'charcoal-config' |
||
| 8 | use Charcoal\Config\AbstractConfig; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Cache Configuration |
||
| 12 | */ |
||
| 13 | class CacheConfig extends AbstractConfig |
||
| 14 | { |
||
| 15 | const DEFAULT_NAMESPACE = 'charcoal'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Default cache type and fallback for user preference. |
||
| 19 | */ |
||
| 20 | const DEFAULT_TYPES = [ |
||
| 21 | 'memory' => true |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Human-readable intervals in seconds. |
||
| 26 | */ |
||
| 27 | const HOUR_IN_SECONDS = 3600; |
||
| 28 | const DAY_IN_SECONDS = 86400; |
||
| 29 | const WEEK_IN_SECONDS = 604800; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Whether to enable or disable the cache service. |
||
| 33 | * |
||
| 34 | * Note: |
||
| 35 | * - When TRUE, the {@see self::$types} are used. |
||
| 36 | * - When FALSE, the "memory" type is used. |
||
| 37 | * |
||
| 38 | * @var boolean |
||
| 39 | */ |
||
| 40 | private $active = true; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Cache type(s) to use. |
||
| 44 | * |
||
| 45 | * Represents a cache driver. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $types; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Default maximum time an item will be cached. |
||
| 53 | * |
||
| 54 | * @var integer |
||
| 55 | */ |
||
| 56 | private $defaultTtl = self::WEEK_IN_SECONDS; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Cache namespace. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $prefix = self::DEFAULT_NAMESPACE; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Retrieve the default values. |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function defaults() |
||
| 71 | { |
||
| 72 | return [ |
||
| 73 | 'active' => true, |
||
| 74 | 'types' => $this->defaultTypes(), |
||
| 75 | 'default_ttl' => self::WEEK_IN_SECONDS, |
||
| 76 | 'prefix' => self::DEFAULT_NAMESPACE |
||
| 77 | ]; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Enable / Disable the cache service. |
||
| 82 | * |
||
| 83 | * @param boolean $active The active flag; |
||
| 84 | * TRUE to enable, FALSE to disable. |
||
| 85 | * @return CacheConfig Chainable |
||
| 86 | */ |
||
| 87 | public function setActive($active) |
||
| 88 | { |
||
| 89 | $this->active = !!$active; |
||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Determine if the cache service is enabled. |
||
| 95 | * |
||
| 96 | * @return boolean TRUE if enabled, FALSE if disabled. |
||
| 97 | */ |
||
| 98 | public function active() |
||
| 99 | { |
||
| 100 | return $this->active; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set the cache type(s) to use. |
||
| 105 | * |
||
| 106 | * The first cache actually available on the system will be the one used for caching. |
||
| 107 | * |
||
| 108 | * @param string[] $types One or more types to try as cache driver until success. |
||
| 109 | * @return CacheConfig Chainable |
||
| 110 | */ |
||
| 111 | public function setTypes(array $types) |
||
| 112 | { |
||
| 113 | $this->types = []; |
||
| 114 | $this->addTypes($types); |
||
| 115 | return $this; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Add cache type(s) to use. |
||
| 120 | * |
||
| 121 | * @param string[] $types One or more types to try as cache driver until success. |
||
| 122 | * @return CacheConfig Chainable |
||
| 123 | */ |
||
| 124 | public function addTypes(array $types) |
||
| 125 | { |
||
| 126 | foreach ($types as $type) { |
||
| 127 | $this->addType($type); |
||
| 128 | } |
||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Add a cache type to use. |
||
| 134 | * |
||
| 135 | * @param string $type The cache type. |
||
| 136 | * @throws InvalidArgumentException If the type is not a string or unsupported. |
||
| 137 | * @return CacheConfig Chainable |
||
| 138 | */ |
||
| 139 | public function addType($type) |
||
| 140 | { |
||
| 141 | if (!in_array($type, $this->validTypes())) { |
||
| 142 | throw new InvalidArgumentException( |
||
| 143 | sprintf('Invalid cache type: "%s"', $type) |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | $this->types[$type] = true; |
||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Retrieve the cache type(s) to use. |
||
| 153 | * |
||
| 154 | * Note: |
||
| 155 | * 1. The default cache type is always appended. |
||
| 156 | * 2. Duplicate types are removed. |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function types() |
||
| 161 | { |
||
| 162 | $types = $this->types + self::DEFAULT_TYPES; |
||
| 163 | return array_keys($types); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Retrieve the default cache types. |
||
| 168 | * |
||
| 169 | * @return string[] |
||
| 170 | */ |
||
| 171 | public function defaultTypes() |
||
| 172 | { |
||
| 173 | return array_keys(self::DEFAULT_TYPES); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Retrieve the available cache types. |
||
| 178 | * |
||
| 179 | * @return string[] |
||
| 180 | */ |
||
| 181 | public function validTypes() |
||
| 182 | { |
||
| 183 | return [ |
||
| 184 | 'apc', |
||
| 185 | 'file', |
||
| 186 | 'db', |
||
| 187 | 'memcache', |
||
| 188 | 'memory', |
||
| 189 | 'noop', |
||
| 190 | 'redis' |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set the default time-to-live for cached items. |
||
| 196 | * |
||
| 197 | * @param mixed $ttl A number representing time in seconds. |
||
| 198 | * @throws InvalidArgumentException If the TTL is not numeric. |
||
| 199 | * @return CacheConfig Chainable |
||
| 200 | */ |
||
| 201 | public function setDefaultTtl($ttl) |
||
| 202 | { |
||
| 203 | if (!is_numeric($ttl)) { |
||
| 204 | throw new InvalidArgumentException( |
||
| 205 | 'TTL must be an integer (seconds)' |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | |||
| 209 | $this->defaultTtl = intval($ttl); |
||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Retrieve the default time-to-live for cached items. |
||
| 215 | * |
||
| 216 | * @return integer |
||
| 217 | */ |
||
| 218 | public function defaultTtl() |
||
| 219 | { |
||
| 220 | return $this->defaultTtl; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set the cache namespace. |
||
| 225 | * |
||
| 226 | * @param string $prefix The cache prefix (or namespace). |
||
| 227 | * @throws InvalidArgumentException If the prefix is not a string. |
||
| 228 | * @return CacheConfig Chainable |
||
| 229 | */ |
||
| 230 | public function setPrefix($prefix) |
||
| 231 | { |
||
| 232 | if (!is_string($prefix)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 233 | throw new InvalidArgumentException( |
||
| 234 | 'Prefix must be a string' |
||
| 235 | ); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** @see \Stash\Pool\::setNamespace */ |
||
| 239 | if (!ctype_alnum($prefix)) { |
||
| 240 | throw new InvalidArgumentException( |
||
| 241 | 'Prefix must be alphanumeric' |
||
| 242 | ); |
||
| 243 | } |
||
| 244 | |||
| 245 | $this->prefix = $prefix; |
||
| 246 | return $this; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Retrieve the cache namespace. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function prefix() |
||
| 255 | { |
||
| 256 | return $this->prefix; |
||
| 257 | } |
||
| 258 | } |
||
| 259 |