|
2 | 2 |
|
3 | 3 | namespace Hans\Sphinx\Models\Traits; |
4 | 4 |
|
5 | | - use Illuminate\Support\Facades\Cache; |
6 | | - use Throwable; |
| 5 | +use Illuminate\Support\Facades\Cache; |
| 6 | +use Throwable; |
7 | 7 |
|
8 | | - trait RoleMethods |
| 8 | +trait RoleMethods |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @param int $id |
| 12 | + * |
| 13 | + * @return static |
| 14 | + */ |
| 15 | + public static function findAndCache(int $id): self |
9 | 16 | { |
10 | | - /** |
11 | | - * @param int $id |
12 | | - * |
13 | | - * @return static |
14 | | - */ |
15 | | - public static function findAndCache(int $id): self |
16 | | - { |
17 | | - return Cache::rememberForever( |
18 | | - static::cacheKey($id), |
19 | | - fn () => self::query()->findOrFail($id) |
20 | | - ); |
21 | | - } |
22 | | - |
23 | | - /** |
24 | | - * @return int |
25 | | - */ |
26 | | - public function getVersion(): int |
27 | | - { |
28 | | - return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; |
29 | | - } |
| 17 | + return Cache::rememberForever( |
| 18 | + static::cacheKey($id), |
| 19 | + fn () => self::query()->findOrFail($id) |
| 20 | + ); |
| 21 | + } |
30 | 22 |
|
31 | | - /** |
32 | | - * @return bool |
33 | | - */ |
34 | | - public function increaseVersion(): bool |
35 | | - { |
36 | | - try { |
37 | | - $this->increment('version'); |
38 | | - $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); |
39 | | - Cache::forget(self::cacheKey($this->id)); |
40 | | - Cache::forever(self::cacheKey($this->id), $this); |
41 | | - } catch (Throwable $e) { |
42 | | - return false; |
43 | | - } |
| 23 | + /** |
| 24 | + * @return int |
| 25 | + */ |
| 26 | + public function getVersion(): int |
| 27 | + { |
| 28 | + return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; |
| 29 | + } |
44 | 30 |
|
45 | | - return true; |
| 31 | + /** |
| 32 | + * @return bool |
| 33 | + */ |
| 34 | + public function increaseVersion(): bool |
| 35 | + { |
| 36 | + try { |
| 37 | + $this->increment('version'); |
| 38 | + $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); |
| 39 | + Cache::forget(self::cacheKey($this->id)); |
| 40 | + Cache::forever(self::cacheKey($this->id), $this); |
| 41 | + } catch (Throwable $e) { |
| 42 | + return false; |
46 | 43 | } |
47 | 44 |
|
48 | | - /** |
49 | | - * Make the unique key for caching the instance. |
50 | | - * |
51 | | - * @param int $id |
52 | | - * |
53 | | - * @return string |
54 | | - */ |
55 | | - protected static function cacheKey(int $id): string |
56 | | - { |
57 | | - return "role_cache_$id"; |
58 | | - } |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Make the unique key for caching the instance. |
| 50 | + * |
| 51 | + * @param int $id |
| 52 | + * |
| 53 | + * @return string |
| 54 | + */ |
| 55 | + protected static function cacheKey(int $id): string |
| 56 | + { |
| 57 | + return "role_cache_$id"; |
59 | 58 | } |
| 59 | +} |
0 commit comments