Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CachedSparqlClient
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 query
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch;
4
5use MediaWiki\Sparql\SparqlClient;
6use Wikimedia\ObjectCache\WANObjectCache;
7
8class CachedSparqlClient {
9    // Service container key for the categories SPARQL client.
10    public const SERVICE = 'CirrusCategoriesClient';
11
12    private SparqlClient $client;
13    private WANObjectCache $cache;
14    private int $ttl;
15    private string $cacheGroup;
16
17    public function __construct( SparqlClient $client, WANObjectCache $cache, int $ttl, string $cacheGroup ) {
18        $this->client = $client;
19        $this->cache = $cache;
20        $this->ttl = $ttl;
21        $this->cacheGroup = $cacheGroup;
22    }
23
24    public function query( string $sparql, bool $rawData = false ): array {
25        return $this->cache->getWithSetCallback(
26            $this->cache->makeKey( 'cirrussearch', 'sparql', $this->cacheGroup, md5( $sparql ), (int)$rawData ),
27            $this->ttl,
28            function () use ( $sparql, $rawData ) {
29                // Throws on failure, which won't be cached.
30                return $this->client->query( $sparql, $rawData );
31            }
32        );
33    }
34}