1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Laravel Cache Adapter for AWS Credential Caching. |
4
|
|
|
* |
5
|
|
|
* @author Luke Waite <[email protected]> |
6
|
|
|
* @copyright 2017 Luke Waite |
7
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/lukewaite/laravel-aws-cache-adapter |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LukeWaite\LaravelAwsCacheAdapter; |
13
|
|
|
|
14
|
|
|
use Aws\CacheInterface; |
15
|
|
|
use Illuminate\Cache\CacheManager; |
16
|
|
|
|
17
|
|
|
class LaravelCacheAdapter implements CacheInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $prefix; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $store; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CacheManager |
31
|
|
|
*/ |
32
|
|
|
private $manager; |
33
|
|
|
|
34
|
84 |
|
public function __construct(CacheManager $manager, $store, $prefix = null) |
35
|
|
|
{ |
36
|
84 |
|
$this->manager = $manager; |
37
|
84 |
|
$this->store = $store; |
38
|
84 |
|
$this->prefix = 'aws_credentials_' . $prefix; |
39
|
84 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
14 |
|
public function get($key) |
45
|
|
|
{ |
46
|
14 |
|
return $this->getCache()->get($this->generateKey($key)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
14 |
|
public function remove($key) |
53
|
|
|
{ |
54
|
14 |
|
$this->getCache()->forget($this->generateKey($key)); |
55
|
14 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
56 |
|
public function set($key, $value, $ttl = 0) |
61
|
|
|
{ |
62
|
56 |
|
$this->getCache()->put($this->generateKey($key), $value, $this->convertTtl($ttl)); |
63
|
56 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Generate a cache key which incorporates the prefix. |
67
|
|
|
* |
68
|
|
|
* @param $key |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
84 |
|
protected function generateKey($key) |
72
|
|
|
{ |
73
|
84 |
|
return $this->prefix . $key; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The AWS CacheInterface takes input in seconds, but the Laravel Cache classes use minutes. To support |
78
|
|
|
* this intelligently, we round up to one minute for any value less than 60 seconds, and round down to |
79
|
|
|
* the nearest whole minute for any value over one minute. First, if the passed in TTL is 0 we return |
80
|
|
|
* 0 to allow an unlimited cache lifetime. |
81
|
|
|
* |
82
|
|
|
* @param $ttl |
83
|
|
|
* @return float|int |
84
|
|
|
*/ |
85
|
56 |
|
protected function convertTtl($ttl) |
86
|
|
|
{ |
87
|
56 |
|
if ($ttl == 0) { |
88
|
14 |
|
return 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
42 |
|
$minutes = floor($ttl / 60); |
92
|
|
|
|
93
|
42 |
|
if ($minutes == 0) { |
94
|
14 |
|
return 1; |
95
|
|
|
} else { |
96
|
28 |
|
return $minutes; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the configured Laravel Cache Store |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
84 |
|
protected function getCache() |
106
|
|
|
{ |
107
|
84 |
|
return $this->manager->store($this->store); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|