Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CollectionCache | |
0.00% |
0 / 15 |
|
0.00% |
0 / 7 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLastRevisionFor | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getLastRevCacheKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onAfterClear | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onAfterInsert | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| onAfterUpdate | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| onAfterRemove | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Collection; |
| 4 | |
| 5 | use Flow\Data\Listener\AbstractListener; |
| 6 | use Flow\Model\AbstractRevision; |
| 7 | use MapCacheLRU; |
| 8 | |
| 9 | /** |
| 10 | * Cache any useful collection data. Listens to lifecycle events for |
| 11 | * insert/update/remove to keep the internal cache up to date and reduce |
| 12 | * requests deeper into the stack. |
| 13 | */ |
| 14 | class CollectionCache extends AbstractListener { |
| 15 | |
| 16 | /** |
| 17 | * Max to cache collection's last revision |
| 18 | */ |
| 19 | private const LAST_REV_CACHE_MAX = 50; |
| 20 | |
| 21 | /** |
| 22 | * The last revision for a collection |
| 23 | * |
| 24 | * @var MapCacheLRU |
| 25 | */ |
| 26 | protected $lastRevCache; |
| 27 | |
| 28 | /** |
| 29 | * Initialize any cache holder in here |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $this->lastRevCache = new MapCacheLRU( self::LAST_REV_CACHE_MAX ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the last revision of a collection that the requested revision belongs to |
| 37 | * @param AbstractRevision $revision current revision |
| 38 | * @return AbstractRevision the last revision |
| 39 | */ |
| 40 | public function getLastRevisionFor( AbstractRevision $revision ) { |
| 41 | $key = $this->getLastRevCacheKey( $revision ); |
| 42 | $lastRevision = $this->lastRevCache->get( $key ); |
| 43 | if ( $lastRevision === null ) { |
| 44 | $lastRevision = $revision->getCollection()->getLastRevision(); |
| 45 | $this->lastRevCache->set( $key, $lastRevision ); |
| 46 | } |
| 47 | |
| 48 | return $lastRevision; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Cache key for last revision |
| 53 | * |
| 54 | * @param AbstractRevision $revision |
| 55 | * @return string |
| 56 | */ |
| 57 | protected function getLastRevCacheKey( AbstractRevision $revision ) { |
| 58 | return $revision->getCollectionId()->getAlphadecimal() . '-' . $revision->getRevisionType() . '-last-rev'; |
| 59 | } |
| 60 | |
| 61 | /** @inheritDoc */ |
| 62 | public function onAfterClear() { |
| 63 | $this->lastRevCache = new MapCacheLRU( self::LAST_REV_CACHE_MAX ); |
| 64 | } |
| 65 | |
| 66 | /** @inheritDoc */ |
| 67 | public function onAfterInsert( $object, array $new, array $metadata ) { |
| 68 | if ( $object instanceof AbstractRevision ) { |
| 69 | $this->lastRevCache->clear( $this->getLastRevCacheKey( $object ) ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** @inheritDoc */ |
| 74 | public function onAfterUpdate( $object, array $old, array $new, array $metadata ) { |
| 75 | if ( $object instanceof AbstractRevision ) { |
| 76 | $this->lastRevCache->clear( $this->getLastRevCacheKey( $object ) ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** @inheritDoc */ |
| 81 | public function onAfterRemove( $object, array $old, array $metadata ) { |
| 82 | if ( $object instanceof AbstractRevision ) { |
| 83 | $this->lastRevCache->clear( $this->getLastRevCacheKey( $object ) ); |
| 84 | } |
| 85 | } |
| 86 | } |