|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bmatovu\Ussd; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheContract; |
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
|
|
8
|
|
|
class Store |
|
9
|
|
|
{ |
|
10
|
|
|
protected CacheContract $cache; |
|
11
|
|
|
protected int $ttl; |
|
12
|
|
|
protected string $prefix; |
|
13
|
|
|
|
|
14
|
28 |
|
public function __construct(string $driver, int $ttl, string $prefix) |
|
15
|
|
|
{ |
|
16
|
28 |
|
$this->cache = Cache::store($driver); |
|
17
|
28 |
|
$this->ttl = $ttl; |
|
18
|
28 |
|
$this->prefix = $prefix; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function __get(string $key) |
|
22
|
|
|
{ |
|
23
|
|
|
if (property_exists($this, $key)) { |
|
24
|
|
|
return $this->{$key}; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return $this->cache->get("{$this->prefix}{$key}"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __set(string $key, $value) |
|
31
|
|
|
{ |
|
32
|
|
|
if (property_exists($this, $key)) { |
|
33
|
|
|
$this->{$key} = $value; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->cache->put("{$this->prefix}{$key}", $value, $this->ttl); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
24 |
|
public function get(string $key, $default = null) |
|
40
|
|
|
{ |
|
41
|
24 |
|
return $this->cache->get("{$this->prefix}{$key}", $default); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function pull(string $key) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->cache->pull("{$this->prefix}{$key}"); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
22 |
|
public function put(string $key, $value): void |
|
50
|
|
|
{ |
|
51
|
|
|
// dd(['key' => "{$this->prefix}{$key}", 'value' => $value]); |
|
52
|
22 |
|
$this->cache->put("{$this->prefix}{$key}", $value, $this->ttl); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function append(string $key, string $extra): void |
|
56
|
|
|
{ |
|
57
|
|
|
$value = $this->cache->get("{$this->prefix}{$key}"); |
|
58
|
|
|
|
|
59
|
|
|
$this->cache->put("{$this->prefix}{$key}", "{$value}{$extra}", $this->ttl); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
28 |
|
public function flush() |
|
63
|
|
|
{ |
|
64
|
28 |
|
$this->cache->flush(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|