1
|
|
|
<?php |
2
|
|
|
namespace Germania\IpstackClient; |
3
|
|
|
|
4
|
|
|
use Germania\IpstackClient\IpstackClientInterface; |
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
9
|
|
|
|
10
|
|
|
class IpstackClientPsr6CacheDecorator implements IpstackClientInterface |
11
|
|
|
{ |
12
|
|
|
use LoggerAwareTrait; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var CacheItemPoolInterface |
17
|
|
|
*/ |
18
|
|
|
public $cache; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int|null |
22
|
|
|
*/ |
23
|
|
|
public $cache_lifetime; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param IpstackClientInterface $ipstack_client |
28
|
|
|
* @param CacheItemPoolInterface $cache PSR-6 Cache ItemPool |
29
|
|
|
*/ |
30
|
24 |
|
public function __construct( IpstackClientInterface $ipstack_client, CacheItemPoolInterface $cache, LoggerInterface $logger = null ) |
31
|
|
|
{ |
32
|
24 |
|
$this->ipstack_client = $ipstack_client; |
|
|
|
|
33
|
24 |
|
$this->cache = $cache; |
34
|
24 |
|
$this->setLogger( $logger ?: new NullLogger ); |
35
|
24 |
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param int|null $lifetime Cache Lifetime in seconds (or NULL) |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
8 |
|
public function setCacheLifeTime( $lifetime ) |
43
|
|
|
{ |
44
|
8 |
|
$this->cache_lifetime = $lifetime; |
45
|
8 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return int|null |
50
|
|
|
*/ |
51
|
24 |
|
public function getCacheLifeTime() |
52
|
|
|
{ |
53
|
24 |
|
return $this->cache_lifetime; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
16 |
|
public function get( string $client_ip, array $custom_query = array() ) : array |
58
|
|
|
{ |
59
|
|
|
|
60
|
16 |
|
$item = $this->cache->getItem( sha1($client_ip) ); |
61
|
|
|
|
62
|
16 |
|
$ipstack = $item->get(); |
63
|
|
|
|
64
|
|
|
// No valid item? |
65
|
16 |
|
if(!$item->isHit()): |
66
|
16 |
|
$this->logger->debug("No ipstack info in cache"); |
67
|
|
|
|
68
|
|
|
// Run intensive code: Ask ipstack directly |
69
|
16 |
|
$ipstack = $this->ipstack_client->get( $client_ip, $custom_query ); |
70
|
|
|
|
71
|
|
|
// Store data for future use. |
72
|
16 |
|
$this->logger->info("Write ipstack info to cache", $ipstack); |
73
|
16 |
|
$item->set($ipstack); |
74
|
|
|
|
75
|
16 |
|
$lifetime = $this->getCacheLifeTime(); |
76
|
16 |
|
$item->expiresAfter($lifetime); |
77
|
|
|
|
78
|
16 |
|
$this->cache->save($item); |
79
|
|
|
|
80
|
|
|
// Valid item found |
81
|
|
|
else: |
82
|
|
|
$this->logger->notice("Found ipstack info in cache", $ipstack); |
83
|
|
|
endif; |
84
|
|
|
|
85
|
16 |
|
return $ipstack; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: