| @@ -24,9 +24,9 @@ | ||
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Cache entry encapsulation. |
| 28 | - * | |
| 28 | + * | |
| 29 | 29 | * @property string $key |
| 30 | 30 | * @property mixed $value |
| 31 | 31 | */ |
| 32 | 32 | class Groups_Cache_Object { |
| @@ -32,8 +32,9 @@ | ||
| 32 | 32 | class Groups_Cache_Object { |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Cache key. |
| 36 | + * | |
| 36 | 37 | * @var string |
| 37 | 38 | */ |
| 38 | 39 | private $key = null; |
| 39 | 40 | |
| @@ -38,28 +39,55 @@ | ||
| 38 | 39 | private $key = null; |
| 39 | 40 | |
| 40 | 41 | /** |
| 41 | 42 | * Cached value. |
| 43 | + * | |
| 42 | 44 | * @var mixed |
| 43 | 45 | */ |
| 44 | 46 | private $value = null; |
| 45 | 47 | |
| 46 | 48 | /** |
| 49 | + * Timestamp. | |
| 50 | + * | |
| 51 | + * @since 4.0.0 | |
| 52 | + * | |
| 53 | + * @var int | |
| 54 | + */ | |
| 55 | + private $created = null; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Expiration. | |
| 59 | + * | |
| 60 | + * @since 4.0.0 | |
| 61 | + * | |
| 62 | + * @var int | |
| 63 | + */ | |
| 64 | + private $expires = null; | |
| 65 | + | |
| 66 | + /** | |
| 47 | 67 | * Create a cache entry object that holds a value for the given key. |
| 48 | - * | |
| 68 | + * | |
| 49 | 69 | * @param string $key |
| 50 | 70 | * @param mixed $value |
| 71 | + * @param int $expires | |
| 51 | 72 | */ |
| 52 | - public function __construct( $key, $value ) { | |
| 53 | - $this->key = $key; | |
| 73 | + public function __construct( $key, $value, $expires = null ) { | |
| 74 | + if ( is_string( $key ) ) { | |
| 75 | + $this->key = $key; | |
| 76 | + } | |
| 54 | 77 | $this->value = $value; |
| 78 | + $this->created = time(); | |
| 79 | + $this->expires = is_numeric( $expires ) ? max( 0, intval( $expires ) ) : null; | |
| 55 | 80 | } |
| 56 | 81 | |
| 57 | 82 | /** |
| 58 | 83 | * Getter implementation for key and value properties. |
| 59 | - * | |
| 84 | + * | |
| 85 | + * @deprecated use proper getters | |
| 86 | + * | |
| 60 | 87 | * @param string $name |
| 61 | - * @return property value or null | |
| 88 | + * | |
| 89 | + * @return mixed property value or null | |
| 62 | 90 | */ |
| 63 | 91 | public function __get( $name ) { |
| 64 | 92 | $result = null; |
| 65 | 93 | switch ( $name ) { |
| @@ -72,17 +100,113 @@ | ||
| 72 | 100 | } |
| 73 | 101 | |
| 74 | 102 | /** |
| 75 | 103 | * Setter for key and value properties. |
| 76 | - * | |
| 104 | + * | |
| 105 | + * @deprecated use proper setters | |
| 106 | + * | |
| 77 | 107 | * @param string $name |
| 78 | 108 | * @param mixed $value |
| 79 | 109 | */ |
| 80 | 110 | public function __set( $name, $value ) { |
| 81 | - switch( $name ) { | |
| 111 | + switch ( $name ) { | |
| 82 | 112 | case 'key' : |
| 113 | + if ( is_string( $value ) ) { | |
| 114 | + $this->key = $value; | |
| 115 | + } | |
| 116 | + break; | |
| 83 | 117 | case 'value' : |
| 84 | - $this->$name = $value; | |
| 118 | + $this->value = $value; | |
| 85 | 119 | break; |
| 86 | 120 | } |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Provide the key of this object. | |
| 125 | + * | |
| 126 | + * @since 3.6.0 | |
| 127 | + * | |
| 128 | + * @return string | |
| 129 | + */ | |
| 130 | + public function get_key() { | |
| 131 | + return $this->key; | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Set the key of this object. | |
| 136 | + * | |
| 137 | + * @since 3.6.0 | |
| 138 | + * | |
| 139 | + * @param string $key | |
| 140 | + */ | |
| 141 | + public function set_key( $key ) { | |
| 142 | + if ( is_string( $key ) ) { | |
| 143 | + $this->key = $key; | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Provide the value of this object. | |
| 149 | + * | |
| 150 | + * @since 3.6.0 | |
| 151 | + * | |
| 152 | + * @return mixed | |
| 153 | + */ | |
| 154 | + public function get_value() { | |
| 155 | + return $this->value; | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Set the value of this object. | |
| 160 | + * | |
| 161 | + * @since 3.6.0 | |
| 162 | + * | |
| 163 | + * @param mixed $value | |
| 164 | + */ | |
| 165 | + public function set_value( $value ) { | |
| 166 | + $this->value = $value; | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Set expires. | |
| 171 | + * | |
| 172 | + * @since 4.0.0 | |
| 173 | + * | |
| 174 | + * @param int|null $expires | |
| 175 | + */ | |
| 176 | + public function set_expires( $expires ) { | |
| 177 | + $this->expires = is_numeric( $expires ) ? max( 0, intval( $expires ) ) : null; | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Provide expires. | |
| 182 | + * | |
| 183 | + * @since 4.0.0 | |
| 184 | + * | |
| 185 | + * @return int|null | |
| 186 | + */ | |
| 187 | + public function get_expires() { | |
| 188 | + return $this->expires; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Expired. | |
| 193 | + * | |
| 194 | + * @since 4.0.0 | |
| 195 | + * | |
| 196 | + * @return boolean | |
| 197 | + */ | |
| 198 | + public function has_expired() { | |
| 199 | + $expired = false; | |
| 200 | + // auto-invalidate objects without timestamp | |
| 201 | + if ( $this->created === null ) { | |
| 202 | + $expired = true; | |
| 203 | + } else { | |
| 204 | + if ( $this->expires !== null ) { | |
| 205 | + if ( time() > ( $this->created + $this->expires ) ) { | |
| 206 | + $expired = true; | |
| 207 | + } | |
| 208 | + } | |
| 209 | + } | |
| 210 | + return $expired; | |
| 87 | 211 | } |
| 88 | 212 | } |