SugiPHP /
Cache
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Cache Class. |
||
| 4 | * |
||
| 5 | * @package SugiPHP.Cache |
||
| 6 | * @author Plamen Popov <[email protected]> |
||
| 7 | * @license http://opensource.org/licenses/mit-license.php (MIT License) |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace SugiPHP\Cache; |
||
| 11 | |||
| 12 | class Cache |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The driver used by a cache. |
||
| 16 | * @var StoreInterface |
||
| 17 | */ |
||
| 18 | protected $driver; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Key prefix to use. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $prefix = ""; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Class constructor. |
||
| 29 | * |
||
| 30 | * @param StoreInterface $driver |
||
| 31 | */ |
||
| 32 | public function __construct(StoreInterface $driver) |
||
| 33 | { |
||
| 34 | $this->driver = $driver; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Sets key prefix. |
||
| 39 | * |
||
| 40 | * @param string $prefix |
||
| 41 | */ |
||
| 42 | public function setPrefix($prefix) |
||
| 43 | { |
||
| 44 | $this->prefix = $prefix; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns key prefix. |
||
| 49 | * |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | public function getPrefix() |
||
| 53 | { |
||
| 54 | return $this->prefix; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Stores an item in the cache for a specified period of time only if it is not already stored. |
||
| 59 | * Cache::add() is similar to Cache::set(), but the operation fails if the key already exists. |
||
| 60 | * |
||
| 61 | * @param string $key |
||
| 62 | * @param mixed $value The value to be stored. |
||
| 63 | * @param integer $ttl Time to live in seconds. 0 means to store it for a maximum time possible |
||
| 64 | * |
||
| 65 | * @return boolean TRUE if the value is set, FALSE on failure |
||
| 66 | */ |
||
| 67 | public function add($key, $value, $ttl = 0) |
||
| 68 | { |
||
| 69 | $key = $this->prefix.$key; |
||
| 70 | |||
| 71 | return $this->driver->add($key, $value, $ttl); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Stores an item in the data store |
||
| 76 | * Cache::set() is similar to Cache::add(), but the operation will not fail if the key already exist. |
||
| 77 | * |
||
| 78 | * @param string $key The key under which to store the value |
||
| 79 | * @param mixed $value The value to store |
||
| 80 | * @param integer $ttl Expiration time in seconds, after which the value is invalidated (deleted) |
||
| 81 | * |
||
| 82 | * @return boolean TRUE on success or FALSE on failure |
||
| 83 | */ |
||
| 84 | public function set($key, $value, $ttl = 0) |
||
| 85 | { |
||
| 86 | $key = $this->prefix.$key; |
||
| 87 | |||
| 88 | return $this->driver->set($key, $value, $ttl); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Fetches a stored variable from the cache |
||
| 93 | * |
||
| 94 | * @param string $key The key used to store the value |
||
| 95 | * |
||
| 96 | * @return mixed Returns NULL if the key does not exist in the store or the value was expired (see $ttl) |
||
| 97 | */ |
||
| 98 | public function get($key, $defaultValue = null) |
||
| 99 | { |
||
| 100 | $key = $this->prefix.$key; |
||
| 101 | $result = $this->driver->get($key); |
||
| 102 | |||
| 103 | return is_null($result) ? $defaultValue : $result; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Checks if the key exists |
||
| 108 | * |
||
| 109 | * @param string $key |
||
| 110 | * |
||
| 111 | * @return boolean TRUE if the key exists, otherwise FALSE |
||
| 112 | */ |
||
| 113 | public function has($key) |
||
| 114 | { |
||
| 115 | $key = $this->prefix.$key; |
||
| 116 | |||
| 117 | return $this->driver->has($key); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Removes a stored variable from the cache |
||
| 122 | * |
||
| 123 | * @param string $key |
||
| 124 | */ |
||
| 125 | public function delete($key) |
||
| 126 | { |
||
| 127 | $key = $this->prefix.$key; |
||
| 128 | |||
| 129 | $this->driver->delete($key); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Invalidate all items in the cache |
||
| 134 | */ |
||
| 135 | public function flush() |
||
| 136 | { |
||
| 137 | $this->driver->flush(); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Increment numeric item's value. |
||
| 142 | * If there is no such key or the stored value is not numeric FALSE is returned |
||
| 143 | * |
||
| 144 | * @param string $key |
||
| 145 | * @param integer $step |
||
| 146 | * |
||
| 147 | * @return integer|false Returns the new incremented value or FALSE on failure |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function inc($key, $step = 1) |
|
| 150 | { |
||
| 151 | $key = $this->prefix.$key; |
||
| 152 | |||
| 153 | if ($this->driver instanceof IncrementorInterface) { |
||
| 154 | return $this->driver->inc($key, $step); |
||
| 155 | } |
||
| 156 | |||
| 157 | $value = $this->driver->get($key); |
||
| 158 | if (is_null($value) || !is_numeric($value)) { |
||
| 159 | return false; |
||
| 160 | } |
||
| 161 | $newValue = $value + $step; |
||
| 162 | |||
| 163 | return ($this->driver->set($key, $newValue)) ? $newValue : false; |
||
|
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Decrements numeric item's value. |
||
| 168 | * If there is no such key or the stored value is not numeric FALSE is returned |
||
| 169 | * |
||
| 170 | * @param string $key |
||
| 171 | * @param integer $step |
||
| 172 | * |
||
| 173 | * @return integer|false Returns the new decremented value or FALSE on failure |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function dec($key, $step = 1) |
|
| 176 | { |
||
| 177 | $key = $this->prefix.$key; |
||
| 178 | |||
| 179 | if ($this->driver instanceof IncrementorInterface) { |
||
| 180 | return $this->driver->dec($key, $step); |
||
| 181 | } |
||
| 182 | |||
| 183 | $value = $this->driver->get($key); |
||
| 184 | if (is_null($value) || !is_numeric($value)) { |
||
| 185 | return false; |
||
| 186 | } |
||
| 187 | $newValue = $value - $step; |
||
| 188 | |||
| 189 | return ($this->driver->set($key, $newValue)) ? $newValue : false; |
||
|
0 ignored issues
–
show
|
|||
| 190 | } |
||
| 191 | } |
||
| 192 |