Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaSearchMemoryEntitiesFetcher
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\MediaInfo\Search;
4
5class MediaSearchMemoryEntitiesFetcher extends MediaSearchEntitiesFetcher {
6    /** @var array */
7    protected static $cache = [];
8
9    public function __construct(
10        protected readonly MediaSearchEntitiesFetcher $fetcher,
11    ) {
12    }
13
14    /**
15     * This is a wrapper around fetch, where the actual data is fetched.
16     * This simply reads from/writes to 2 intermediate caching levels:
17     * - in a property to allow for repeated calls within the same execution thread
18     * - in a separate key-value store, for follow-up requests (e.g. loading next
19     *   batch of results for the same term)
20     *
21     * @param array $searchQueries
22     * @return array
23     */
24    public function get( array $searchQueries ): array {
25        $emptyData = array_fill_keys( $searchQueries, [] );
26
27        // fetch from memory, in case it was already requested in this process
28        $fromMemory = array_intersect_key( static::$cache, $emptyData );
29
30        // fetch remaining queries from cache
31        $queriesToFetch = array_keys( array_diff_key( $emptyData, $fromMemory ) );
32        $results = $this->fetcher->get( $queriesToFetch );
33
34        // store in memory
35        static::$cache += $results;
36
37        return $fromMemory + $results + $emptyData;
38    }
39}