1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Firewall\Repositories\Cache; |
4
|
|
|
|
5
|
|
|
use Illuminate\Cache\CacheManager; |
6
|
|
|
use PragmaRX\Firewall\Support\ServiceInstances; |
7
|
|
|
|
8
|
|
|
class Cache |
9
|
|
|
{ |
10
|
|
|
use ServiceInstances; |
11
|
|
|
|
12
|
|
|
const CACHE_BASE_NAME = 'firewall.'; |
13
|
|
|
|
14
|
|
|
private $cache; |
15
|
|
|
|
16
|
49 |
|
public function __construct(CacheManager $cache) |
17
|
|
|
{ |
18
|
49 |
|
$this->cache = $cache; |
19
|
49 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Cache remember. |
23
|
|
|
* |
24
|
|
|
* @param $model |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
33 |
|
public function remember($model) |
29
|
|
|
{ |
30
|
33 |
|
if (($timeout = $this->expireTime()) > 0) { |
31
|
33 |
|
$this->put($model->ip_address, $model, $timeout); |
32
|
|
|
} |
33
|
33 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Make a cache key. |
37
|
|
|
* |
38
|
|
|
* @param $key |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
49 |
|
public function key($key) |
43
|
|
|
{ |
44
|
49 |
|
return sha1(static::CACHE_BASE_NAME."ip_address.$key"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check if cache has key. |
49
|
|
|
* |
50
|
|
|
* @param $key |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
48 |
|
public function has($key) |
55
|
|
|
{ |
56
|
48 |
|
if ($this->enabled()) { |
57
|
48 |
|
return $this->cache->has($this->key($key)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a value from the cache. |
65
|
|
|
* |
66
|
|
|
* @param $key |
67
|
|
|
* |
68
|
|
|
* @return mixed|null |
69
|
|
|
*/ |
70
|
23 |
|
public function get($key) |
71
|
|
|
{ |
72
|
23 |
|
if ($this->enabled()) { |
73
|
23 |
|
return $this->cache->get($this->key($key)); |
|
|
|
|
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Remove an ip address from cache. |
79
|
|
|
* |
80
|
|
|
* @param $key |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
7 |
|
public function forget($key) |
85
|
|
|
{ |
86
|
7 |
|
if ($this->enabled()) { |
87
|
7 |
|
$this->cache->forget($this->key($key)); |
88
|
|
|
} |
89
|
7 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Store an item in the cache for a given number of minutes. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @param mixed $value |
96
|
|
|
* @param int|null|bool $minutes |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
36 |
|
public function put($key, $value, $minutes = null) |
101
|
|
|
{ |
102
|
36 |
|
if ($timeout = $this->enabled()) { |
103
|
36 |
|
$this->cache->put($this->key($key), $value, $minutes ?: $timeout); |
104
|
|
|
} |
105
|
36 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get cache expire time. |
109
|
|
|
* |
110
|
|
|
* @return int|bool |
111
|
|
|
*/ |
112
|
49 |
|
public function expireTime() |
113
|
|
|
{ |
114
|
49 |
|
return $this->config()->get('cache_expire_time'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get enabled state. |
119
|
|
|
* |
120
|
|
|
* @return int|bool |
121
|
|
|
*/ |
122
|
49 |
|
public function enabled() |
123
|
|
|
{ |
124
|
49 |
|
return $this->config()->get('cache_expire_time') !== false && |
125
|
49 |
|
$this->expireTime() > 0; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.