-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheable.php
More file actions
147 lines (130 loc) · 4.18 KB
/
Cacheable.php
File metadata and controls
147 lines (130 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\ModelCache;
use Hyperf\Context\ApplicationContext;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Model;
use Hyperf\Database\Query\Builder as QueryBuilder;
use Hyperf\ModelCache\Builder as ModelCacheBuilder;
trait Cacheable
{
protected bool $useCacheBuilder = false;
/**
* Fetch a model from cache.
* @param mixed $id
* @return null|self
*/
public static function findFromCache($id): ?Model
{
$container = ApplicationContext::getContainer();
$manager = $container->get(Manager::class);
return $manager->findFromCache($id, static::class);
}
/**
* Fetch models from cache.
* @return Collection<int, self>
*/
public static function findManyFromCache(array $ids): Collection
{
$container = ApplicationContext::getContainer();
$manager = $container->get(Manager::class);
$ids = array_unique($ids);
return $manager->findManyFromCache($ids, static::class);
}
/**
* Delete model from cache.
*/
public function deleteCache(): bool
{
$manager = $this->getContainer()->get(Manager::class);
return $manager->destroy([$this->getKey()], static::class);
}
/**
* Get the expired time for cache.
*/
public function getCacheTTL(): ?int
{
return null;
}
/**
* Increment a column's value by a given amount.
* @param string $column
* @param float|int $amount
* @return int
*/
public function increment($column, $amount = 1, array $extra = [])
{
$res = parent::increment($column, $amount, $extra);
if ($res > 0) {
if ($this->getConnection()->transactionLevel() && $this instanceof CacheableInterface) {
InvalidCacheManager::instance()->push($this);
} elseif (empty($extra)) {
// Only increment a column's value.
/** @var Manager $manager */
$manager = $this->getContainer()->get(Manager::class);
$manager->increment($this->getKey(), $column, $amount, static::class);
} else {
// Update other columns, when increment a column's value.
$this->deleteCache();
}
}
return $res;
}
/**
* Decrement a column's value by a given amount.
* @param string $column
* @param float|int $amount
* @return int
*/
public function decrement($column, $amount = 1, array $extra = [])
{
$res = parent::decrement($column, $amount, $extra);
if ($res > 0) {
if ($this->getConnection()->transactionLevel() && $this instanceof CacheableInterface) {
InvalidCacheManager::instance()->push($this);
} elseif (empty($extra)) {
// Only decrement a column's value.
/** @var Manager $manager */
$manager = $this->getContainer()->get(Manager::class);
$manager->increment($this->getKey(), $column, -$amount, static::class);
} else {
// Update other columns, when decrement a column's value.
$this->deleteCache();
}
}
return $res;
}
/**
* Create a new Model query builder for the model.
* @param QueryBuilder $query
*/
public function newModelBuilder($query): Builder
{
if ($this->useCacheBuilder) {
return new ModelCacheBuilder($query);
}
return parent::newModelBuilder($query);
}
public function newQuery(bool $cache = false): Builder
{
$this->useCacheBuilder = $cache;
return parent::newQuery();
}
/**
* @param bool $cache Whether to delete the model cache when batch update
* @return Builder|static
*/
public static function query(bool $cache = false): Builder
{
return (new static())->newQuery($cache);
}
}