| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | class Cache_Handler_Resource_MemCache implements Cache_Handler_Resource_Interface { |
|---|
| 10 | public $memcache; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | public function __construct( $ini ) |
|---|
| 18 | { |
|---|
| 19 | $this->ttl = 3600; |
|---|
| 20 | foreach( $ini as $key => $val ){ |
|---|
| 21 | $this->$key = $val; |
|---|
| 22 | } |
|---|
| 23 | $this->memcache = new MemCache(); |
|---|
| 24 | $this->memcache->connect( $this->host, $this->port ); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | public function _insert( $id, $data, $ttl ) |
|---|
| 32 | { |
|---|
| 33 | $this->memcache->delete( $id ); |
|---|
| 34 | $ret = $this->memcache->set( $id, $data, 0, $ttl ); |
|---|
| 35 | return $ret; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | public function _delete( $id ) |
|---|
| 42 | { |
|---|
| 43 | return $this->memcache->delete( $id ); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | public function save( $data, $id, $group = null ) |
|---|
| 54 | { |
|---|
| 55 | $ret[]= $this->_insert( $id, $data, $this->ttl ); |
|---|
| 56 | $ret[]= $this->addGroup( $id, $group ); |
|---|
| 57 | $ret[]= $this->setTimeToLive( $id, $this->ttl ); |
|---|
| 58 | $ret[]= $this->setLastModified( $id, date("r") ); |
|---|
| 59 | return ($ret[0] && $ret[1] && $ret[2] && $ret[3] ); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | public function remove( $id ) |
|---|
| 69 | { |
|---|
| 70 | $ret[] = $this->_delete( $id ); |
|---|
| 71 | $ret[] = $this->_leaveGroup( $id ); |
|---|
| 72 | $ret[] = $this->_deleteLastModified( $id ); |
|---|
| 73 | $ret[] = $this->_deleteTTL( $id ); |
|---|
| 74 | return ( $ret[0] && $ret[1] && $ret[2] && $ret[3] ); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | public function clean( $group =null, &$ids = null ) |
|---|
| 86 | { |
|---|
| 87 | if( $group == null ){ |
|---|
| 88 | return $this->_cleanAll(); |
|---|
| 89 | }else{ |
|---|
| 90 | return $this->_removeGroup( $group, &$ids ); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | public function _cleanAll(){ |
|---|
| 94 | return $this->memcache->flush(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | public function get( $id ) |
|---|
| 105 | { |
|---|
| 106 | $content = $this->memcache->get( $id ); |
|---|
| 107 | if( !$content === false ){ |
|---|
| 108 | return $content; |
|---|
| 109 | }else{ |
|---|
| 110 | return null; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | public function _getModifiedKeyName() |
|---|
| 114 | { |
|---|
| 115 | return ".modified"; |
|---|
| 116 | } |
|---|
| 117 | public function _deleteLastModified( $id ) |
|---|
| 118 | { |
|---|
| 119 | return $this->_delete( $id.$this->_getModifiedKeyName() ); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | public function getLastModified( $id ) |
|---|
| 129 | { |
|---|
| 130 | return $this->get( $id.$this->_getModifiedKeyName() ); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | public function setLastModified( $id, $date ) |
|---|
| 142 | { |
|---|
| 143 | if( $date == null){ |
|---|
| 144 | $date = date("r"); |
|---|
| 145 | } |
|---|
| 146 | return $this->_insert( $id.$this->_getModifiedKeyName(), |
|---|
| 147 | $date, $this->ttl |
|---|
| 148 | ); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | public function setTimeToLive( $id, $int = null ) |
|---|
| 160 | { |
|---|
| 161 | if( $int == null ){ |
|---|
| 162 | $int = $this->ttl; |
|---|
| 163 | }else if( $int != $this->ttl ){ |
|---|
| 164 | $this->ttl = $int; |
|---|
| 165 | $this->memcache->delete( $id, $this->ttl ); |
|---|
| 166 | } |
|---|
| 167 | return $this->_insert( $id.$this->_getTTLKeyName(), |
|---|
| 168 | $this->ttl, |
|---|
| 169 | $this->ttl |
|---|
| 170 | ); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | public function getTimeToLive( $id ) |
|---|
| 181 | { |
|---|
| 182 | return $this->get( $id.$this->_getTTLKeyName() ); |
|---|
| 183 | } |
|---|
| 184 | public function _getTTLKeyName() |
|---|
| 185 | { |
|---|
| 186 | return ".ttl"; |
|---|
| 187 | } |
|---|
| 188 | public function _deleteTTL( $id ) |
|---|
| 189 | { |
|---|
| 190 | return $this->_delete( $id.$this->_getTTLKeyName() ); |
|---|
| 191 | } |
|---|
| 192 | public function getGroupList() |
|---|
| 193 | { |
|---|
| 194 | $ret = $this->get( $this->getGroupKeyName() ); |
|---|
| 195 | if( $ret == null ){ |
|---|
| 196 | return array(); |
|---|
| 197 | }else{ |
|---|
| 198 | return unserialize( $ret ); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | public function getGroupKeyName() |
|---|
| 202 | { |
|---|
| 203 | return md5(".cache.groups"); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | public function saveGroupList( $array ) |
|---|
| 207 | { |
|---|
| 208 | $str = serialize( $array ); |
|---|
| 209 | return $this->memcache->set( $groupname.$this->getGroupKeyName(), $str ); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | public function addIntoGroup( $id, $group ){ |
|---|
| 220 | return $this->addGroup( $id, $group ); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | public function addGroup( $id, $groupnames ) |
|---|
| 224 | { |
|---|
| 225 | if( $groupnames == null || $groupnames == "" ){ |
|---|
| 226 | return true; |
|---|
| 227 | } |
|---|
| 228 | if( !is_array( $groupnames ) ){ |
|---|
| 229 | $groupnames = array( $groupnames ); |
|---|
| 230 | } |
|---|
| 231 | $list = $this->getGroupList(); |
|---|
| 232 | foreach( $groupnames as $groupname ){ |
|---|
| 233 | if( sizeof( $list ) == 0 ){ |
|---|
| 234 | $list[] = array( $id => $groupname ); |
|---|
| 235 | continue; |
|---|
| 236 | } |
|---|
| 237 | foreach( $list as $pair ){ |
|---|
| 238 | list( $_key, $_val ) = each( $pair ); |
|---|
| 239 | if( $_key == $id && $_val == $groupname ){ |
|---|
| 240 | continue; |
|---|
| 241 | }else{ |
|---|
| 242 | $list[] = array( $id => $groupname ); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | return $this->saveGroupList( $list ); |
|---|
| 247 | } |
|---|
| 248 | public function _removeGroup( $groupname, &$ids = null ) |
|---|
| 249 | { |
|---|
| 250 | $list = $this->getGroupList(); |
|---|
| 251 | foreach( $list as $idx => $pair ){ |
|---|
| 252 | list( $_key, $_val ) = each( $pair ); |
|---|
| 253 | if( $_val == $groupname ){ |
|---|
| 254 | $ids[] = $_key; |
|---|
| 255 | unset( $list[$idx] ); |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | $list = array_values( $list ); |
|---|
| 259 | $ret[] = $this->saveGroupList( $list ); |
|---|
| 260 | foreach( $ids as $id ){ |
|---|
| 261 | $ret[] = $this->remove( $id ); |
|---|
| 262 | } |
|---|
| 263 | return !in_array( $ret, false, true ); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | public function removeFromGroup( $id, $group = null ) |
|---|
| 272 | { |
|---|
| 273 | $ret[] = $this->remove( $id ); |
|---|
| 274 | $ret[] = $this->_leaveGroup( $id, $group ); |
|---|
| 275 | return !in_array( $ret, false, true ); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | public function _leaveGroup( $id, $group = null ) |
|---|
| 285 | { |
|---|
| 286 | $list = $this->getGroupList(); |
|---|
| 287 | foreach( $list as $idx => $pair ){ |
|---|
| 288 | list( $_key, $_val ) = each( $pair ); |
|---|
| 289 | if( $_key == $id ){ |
|---|
| 290 | if( $group == null || $group == $_val ){ |
|---|
| 291 | $ids[] = $_key; |
|---|
| 292 | unset( $list[$idx] ); |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | $list = array_values( $list ); |
|---|
| 297 | $ret[] = $this->saveGroupList( $list ); |
|---|
| 298 | return $ret[0] && $ret[1]; |
|---|
| 299 | |
|---|
| 300 | } |
|---|
| 301 | } |
|---|