Skip to content
This repository has been archived by the owner. It is now read-only.

Commit e551ba6

Browse files
authored
Cache for calculate configuration now take into account the configuration type (#2)
1 parent c2dae0c commit e551ba6

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/Scraper/Repositories/Configuration.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class Configuration
1616
*/
1717
private $configurator;
1818

19-
private $cacheKey = self::class . '-config';
20-
2119
/**
2220
* Cache TTL in minutes.
2321
*
@@ -37,7 +35,8 @@ public function findByType(string $type): Collection
3735

3836
public function calculate(string $type): Collection
3937
{
40-
$config = Cache::get($this->cacheKey);
38+
$cacheKey = $this->getCacheKey($type);
39+
$config = Cache::get($cacheKey);
4140
if (!$config) {
4241
Log::warning('Calculating configuration');
4342
$scrapedDataset = ScrapedDataset::withType($type)->get();
@@ -47,9 +46,14 @@ public function calculate(string $type): Collection
4746
}
4847

4948
$config = $this->configurator->configureFromDataset($scrapedDataset);
50-
Cache::put($this->cacheKey, $config, self::CACHE_TTL);
49+
Cache::put($cacheKey, $config, self::CACHE_TTL);
5150
}
5251

5352
return $config;
5453
}
54+
55+
protected function getCacheKey(string $type): string
56+
{
57+
return self::class . "-config-{$type}";
58+
}
5559
}

tests/Unit/Scraper/Repositories/ConfigurationTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ public function whenRecalculateItShouldStoreTheNewXpaths()
100100
]);
101101

102102
Cache::shouldReceive('get')
103-
->with($this->getCacheKey())
103+
->with(Configuration::class . '-config-post')
104104
->andReturnNull();
105105
Cache::shouldReceive('put')
106-
->with($this->getCacheKey(), $config, Configuration::CACHE_TTL);
106+
->with(Configuration::class . '-config-post', $config, Configuration::CACHE_TTL);
107107

108108
$configurator = \Mockery::mock(Configurator::class);
109109
$configurator->shouldReceive('configureFromDataset')
@@ -154,7 +154,7 @@ public function whenRecalculateFailsItShouldThrowAnException()
154154
]);
155155

156156
Cache::shouldReceive('get')
157-
->with($this->getCacheKey())
157+
->with(Configuration::class . '-config-post')
158158
->andReturnNull();
159159

160160
$configurator = \Mockery::mock(Configurator::class);
@@ -183,7 +183,7 @@ public function whenCalculateAfterAnotherCalculateItShouldUseThePrecalclatedConf
183183
$config = collect('configuration');
184184

185185
Cache::shouldReceive('get')
186-
->with($this->getCacheKey())
186+
->with(Configuration::class . '-config-post')
187187
->andReturn($config);
188188

189189
$configuration = new Configuration($configurator);
@@ -192,9 +192,4 @@ public function whenCalculateAfterAnotherCalculateItShouldUseThePrecalclatedConf
192192
$configuration->calculate('post')
193193
);
194194
}
195-
196-
private function getCacheKey()
197-
{
198-
return Configuration::class . '-config';
199-
}
200195
}

0 commit comments

Comments
 (0)