Skip to content

Commit e8f0e35

Browse files
committed
Add to and retrieve films from the cache
1 parent a22a8c0 commit e8f0e35

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/Cache.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111
class Cache
1212
{
1313

14-
/**
15-
* Initiate cache database
16-
*
17-
* @var \Filebase\Database
18-
*/
19-
private $cache = new \Filebase\Database([
20-
'dir' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/',
21-
'backupLocation' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/backups/',
22-
'format' => \Filebase\Format\Json::class,
23-
'cache' => true,
24-
'cache_expires' => 31540000,
25-
'pretty' => false
26-
]);
14+
function __construct()
15+
{
16+
/**
17+
* Initiate cache database
18+
*
19+
* @var \Filebase\Database
20+
*/
21+
$this->cache = new \Filebase\Database([
22+
'dir' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/',
23+
'backupLocation' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/backups/',
24+
'format' => \Filebase\Format\Json::class,
25+
'cache' => true,
26+
'cache_expires' => 31540000,
27+
'pretty' => false
28+
]);
29+
}
2730

2831
/**
2932
* Add (or modify) an item in the cache
@@ -47,7 +50,7 @@ public function add(string $key, $value)
4750
*/
4851
public function count(): int
4952
{
50-
return $this->$cache->count();
53+
return $this->cache->count();
5154
}
5255

5356
/**
@@ -63,25 +66,25 @@ public function delete(string $key): bool
6366
}
6467

6568
/**
66-
* Check if an item exists in the cache
69+
* Get an item from the cache
6770
*
6871
* @param string $key
69-
* @return bool
72+
* @return object
7073
*/
71-
public function exists(string $key): bool
74+
public function get(string $key): object
7275
{
73-
return $this->$cache->has($key);
76+
return $this->cache->get($key);
7477
}
7578

7679
/**
77-
* Get an item from the cache
80+
* Check if an item exists in the cache
7881
*
7982
* @param string $key
80-
* @return array
83+
* @return bool
8184
*/
82-
public function get(string $key): array
85+
public function has(string $key): bool
8386
{
84-
return $this->$cache->get($key);
87+
return $this->cache->has($key);
8588
}
8689

8790
}

src/imdb.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public function film(string $filmId, array $options = []): array
5252
// -> handles what the api returns
5353
$response = new Response;
5454

55+
// Initiate cache object
56+
// -> handles storing/retrieving cached results
57+
$cache = new Cache;
58+
5559
// Initiate dom object
5660
// -> handles page scraping
5761
$dom = new Dom;
@@ -77,6 +81,11 @@ public function film(string $filmId, array $options = []): array
7781
}
7882
}
7983

84+
// Check cache for film
85+
if ($cache->has($filmId)) {
86+
return $cache->get($filmId)->film;
87+
}
88+
8089
// Load imdb film page and parse the dom
8190
$page = $dom->fetch("https://www.imdb.com/title/".$filmId, $options);
8291

@@ -102,6 +111,9 @@ public function film(string $filmId, array $options = []): array
102111
$response->add("technical_specs", []);
103112
}
104113

114+
// Add result to the cache
115+
$cache->add($filmId, $response->return());
116+
105117
// Return the response $store
106118
return $response->return();
107119
}

0 commit comments

Comments
 (0)