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 | public function onAfterClear() { |
62 | $this->lastRevCache = new MapCacheLRU( self::LAST_REV_CACHE_MAX ); |
63 | } |
64 | |
65 | public function onAfterInsert( $object, array $new, array $metadata ) { |
66 | if ( $object instanceof AbstractRevision ) { |
67 | $this->lastRevCache->clear( $this->getLastRevCacheKey( $object ) ); |
68 | } |
69 | } |
70 | |
71 | public function onAfterUpdate( $object, array $old, array $new, array $metadata ) { |
72 | if ( $object instanceof AbstractRevision ) { |
73 | $this->lastRevCache->clear( $this->getLastRevCacheKey( $object ) ); |
74 | } |
75 | } |
76 | |
77 | public function onAfterRemove( $object, array $old, array $metadata ) { |
78 | if ( $object instanceof AbstractRevision ) { |
79 | $this->lastRevCache->clear( $this->getLastRevCacheKey( $object ) ); |
80 | } |
81 | } |
82 | } |