forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsRepository.php
More file actions
95 lines (82 loc) · 2.33 KB
/
Copy pathSettingsRepository.php
File metadata and controls
95 lines (82 loc) · 2.33 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
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Setting;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
class SettingsRepository extends EloquentRepository implements SettingsRepositoryInterface
{
/**
* @var array
*/
private static $cache = [];
/**
* @var array
*/
private static $databaseMiss = [];
/**
* Return the model backing this repository.
*
* @return string
*/
public function model()
{
return Setting::class;
}
/**
* Store a new persistent setting in the database.
*
* @param string $key
* @param string|null $value
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function set(string $key, string $value = null)
{
// Clear item from the cache.
$this->clearCache($key);
$this->withoutFreshModel()->updateOrCreate(['key' => $key], ['value' => $value ?? '']);
self::$cache[$key] = $value;
}
/**
* Retrieve a persistent setting from the database.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
// If item has already been requested return it from the cache. If
// we already know it is missing, immediately return the default value.
if (array_key_exists($key, self::$cache)) {
return self::$cache[$key];
} elseif (array_key_exists($key, self::$databaseMiss)) {
return value($default);
}
$instance = $this->getBuilder()->where('key', $key)->first();
if (is_null($instance)) {
self::$databaseMiss[$key] = true;
return value($default);
}
return self::$cache[$key] = $instance->value;
}
/**
* Remove a key from the database cache.
*
* @param string $key
*/
public function forget(string $key)
{
$this->clearCache($key);
$this->deleteWhere(['key' => $key]);
}
/**
* Remove a key from the cache.
*
* @param string $key
*/
private function clearCache(string $key)
{
unset(self::$cache[$key], self::$databaseMiss[$key]);
}
}