|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\WalletVacuum; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Mathable; |
|
6
|
|
|
use Bavix\Wallet\Interfaces\Storable; |
|
7
|
|
|
use Bavix\Wallet\Services\LockService; |
|
8
|
|
|
use Bavix\Wallet\Simple\Store as SimpleStore; |
|
9
|
|
|
use Bavix\WalletVacuum\Services\StoreService; |
|
10
|
|
|
use Illuminate\Cache\TaggedCache; |
|
11
|
|
|
use Illuminate\Support\Facades\Cache; |
|
12
|
|
|
|
|
13
|
|
|
class Store implements Storable |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $tags; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $ttl; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Store constructor. |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->tags = config('wallet-vacuum.tags', ['wallets', 'vacuum']); |
|
31
|
4 |
|
$this->ttl = config('wallet-vacuum.ttl', 600); |
|
32
|
4 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get the balance from the cache. |
|
36
|
|
|
* |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function getBalance($object) |
|
40
|
|
|
{ |
|
41
|
4 |
|
$key = app(StoreService::class) |
|
42
|
4 |
|
->getCacheKey($object); |
|
43
|
|
|
|
|
44
|
4 |
|
$balance = $this->taggedCache() |
|
45
|
4 |
|
->get($key); |
|
46
|
|
|
|
|
47
|
4 |
|
if ($balance === null) { |
|
48
|
4 |
|
$balance = (new SimpleStore()) |
|
49
|
4 |
|
->getBalance($object); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
4 |
|
return $balance; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Increases the wallet balance in the cache array. |
|
57
|
|
|
* |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function incBalance($object, $amount) |
|
61
|
|
|
{ |
|
62
|
4 |
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($object, $amount) { |
|
63
|
4 |
|
$balance = $this->getBalance($object); |
|
64
|
4 |
|
$newBalance = app(Mathable::class) |
|
65
|
4 |
|
->add($balance, $amount); |
|
66
|
|
|
|
|
67
|
4 |
|
$this->setBalance($object, $newBalance); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* When your project grows to high loads and situations arise with a race condition, |
|
71
|
|
|
* you understand that an extra request to |
|
72
|
|
|
* the cache will save you from many problems when |
|
73
|
|
|
* checking the balance. |
|
74
|
|
|
*/ |
|
75
|
4 |
|
return $newBalance; |
|
76
|
4 |
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* sets the cache value directly. |
|
81
|
|
|
* |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function setBalance($object, $amount): bool |
|
85
|
|
|
{ |
|
86
|
4 |
|
return $this->taggedCache()->put( |
|
87
|
4 |
|
app(StoreService::class)->getCacheKey($object), |
|
88
|
4 |
|
app(Mathable::class)->round($amount), |
|
89
|
4 |
|
$this->ttl |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
4 |
|
public function fresh(): bool |
|
97
|
|
|
{ |
|
98
|
4 |
|
return $this->taggedCache()->flush(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return TaggedCache |
|
103
|
|
|
*/ |
|
104
|
4 |
|
public function taggedCache(): TaggedCache |
|
105
|
|
|
{ |
|
106
|
4 |
|
return Cache::tags($this->tags); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|