Source of file ClearExpiredByFactor.php
Size: 1,550 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Cache/Storage/Plugin/ClearExpiredByFactor.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Cache\Storage\Plugin; use Zend\Cache\Storage\ClearExpiredInterface; use Zend\Cache\Storage\PostEvent; use Zend\EventManager\EventManagerInterface; class ClearExpiredByFactor extends AbstractPlugin { /** * {@inheritDoc} */ public function attach(EventManagerInterface $events, $priority = 1) { $callback = array($this, 'clearExpiredByFactor'); $this->listeners[] = $events->attach('setItem.post', $callback, $priority); $this->listeners[] = $events->attach('setItems.post', $callback, $priority); $this->listeners[] = $events->attach('addItem.post', $callback, $priority); $this->listeners[] = $events->attach('addItems.post', $callback, $priority); } /** * Clear expired items by factor after writing new item(s) * * @param PostEvent $event * @return void */ public function clearExpiredByFactor(PostEvent $event) { $storage = $event->getStorage(); if (!($storage instanceof ClearExpiredInterface)) { return; } $factor = $this->getOptions()->getClearingFactor(); if ($factor && mt_rand(1, $factor) == 1) { $storage->clearExpired(); } } } |