Skip to content

Commit

Permalink
Merge pull request #336 from wikimedia/ttl-env
Browse files Browse the repository at this point in the history
Change api cache ttl to be a .evn var
  • Loading branch information
samwilson committed Feb 26, 2021
2 parents 53ce0af + 3e589b2 commit 00f4548
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ MAILER_DSN=smtp://mail.tools.wmflabs.org:25
# APP_TEMP_PATH=/tmp/wsexport
APP_ENABLE_STATS=true
APP_ENABLE_CACHE=false
APP_CACHE_TTL=21600
APP_TIMEOUT=120
APP_MAIL_SENDER=tools.wsexport@tools.wmflabs.org
APP_LOG_RECIPIENT_1=admin1@example.org
Expand Down
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ services:
$epubCheckPath: '%env(string:EPUBCHECK_JAR)%'
public: true

App\Util\Api:
arguments:
$cacheTtl: '%env(int:APP_CACHE_TTL)%'

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
12 changes: 7 additions & 5 deletions src/Util/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ class Api {
/** @var string[][] */
private $namespaces = [];

public function __construct( LoggerInterface $logger, CacheItemPoolInterface $cacheItemPool, CacheInterface $cache, ClientInterface $client = null ) {
public function __construct( LoggerInterface $logger, CacheItemPoolInterface $cacheItemPool, CacheInterface $cache, ?ClientInterface $client, int $cacheTtl ) {
$this->logger = $logger;
$this->cache = $cache;

if ( $client === null ) {
$client = $this->createClient( $logger, $cacheItemPool );
$client = $this->createClient( $logger, $cacheItemPool, $cacheTtl );
}
$this->client = $client;
}
Expand Down Expand Up @@ -302,17 +303,18 @@ public function get( $url ) {

/**
* @param LoggerInterface $logger
* @param CacheItemPoolInterface $cache
* @param int $cacheTtl
* @return ClientInterface
*/
private function createClient( LoggerInterface $logger, CacheItemPoolInterface $cache ): ClientInterface {
private function createClient( LoggerInterface $logger, CacheItemPoolInterface $cache, int $cacheTtl ): ClientInterface {
$handler = HandlerStack::create();

// Logger.
$handler->push( LoggingMiddleware::forLogger( $logger ), 'logging' );

// Cache.
$ttl = 12 * 60 * 60;
$cacheStrategy = new GreedyCacheStrategy( new Psr6CacheStorage( $cache ), $ttl );
$cacheStrategy = new GreedyCacheStrategy( new Psr6CacheStorage( $cache ), $cacheTtl );
$handler->push( new CacheMiddleware( $cacheStrategy ), 'cache' );

return new Client( [
Expand Down
2 changes: 1 addition & 1 deletion tests/Book/BookProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function setUp(): void {
];
$this->mockHandler = new MockHandler( $responses );
$client = new Client( [ 'handler' => HandlerStack::create( $this->mockHandler ) ] );
$api = new Api( new NullLogger(), new NullAdapter(), new NullAdapter(), $client );
$api = new Api( new NullLogger(), new NullAdapter(), new NullAdapter(), $client, 0 );
$api->setLang( 'en' );
$this->bookProvider = new BookProvider( $api, [ 'categories' => false, 'credits' => true ] );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Book/RefreshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testRefreshUpdatesEpubCssWikisource() {
}

private function refresh( $lang ) {
$api = new Api( new NullLogger(), new NullAdapter(), new NullAdapter(), $this->mockClient( $this->defaultResponses() ) );
$api = new Api( new NullLogger(), new NullAdapter(), new NullAdapter(), $this->mockClient( $this->defaultResponses() ), 0 );
$api->setLang( $lang );
$refresh = new Refresh( $api, new NullAdapter() );
$refresh->refresh();
Expand Down
2 changes: 1 addition & 1 deletion tests/Util/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function apiWithJsonResponse( $data ) {
}

private function apiWithResponse( $status, $header, $body ) {
$api = new Api( new NullLogger(), new NullAdapter(), new NullAdapter(), $this->mockClient( [ new Response( $status, $header, $body ) ] ) );
$api = new Api( new NullLogger(), new NullAdapter(), new NullAdapter(), $this->mockClient( [ new Response( $status, $header, $body ) ] ), 60 );
$api->setLang( 'en' );
return $api;
}
Expand Down

0 comments on commit 00f4548

Please sign in to comment.