22
33namespace RuangDeveloper \LaravelSettings \Services ;
44
5+ use Illuminate \Support \Facades \Cache ;
6+ use RuangDeveloper \LaravelSettings \Supports \Support ;
7+
58class SettingsService
69{
710 /**
@@ -31,10 +34,7 @@ public function __construct(string $model)
3134 */
3235 public function set (string $ key , mixed $ value ): void
3336 {
34- $ this ->model ::updateOrCreate (
35- [config ('laravel-settings.key_name ' ) => $ key ],
36- [config ('laravel-settings.value_name ' ) => $ value ]
37- );
37+ $ this ->storeSetting ($ key , $ value );
3838 }
3939
4040 /**
@@ -46,25 +46,7 @@ public function set(string $key, mixed $value): void
4646 */
4747 public function get (string $ key , mixed $ default = null ): mixed
4848 {
49- $ setting = $ this ->model ::where ([
50- config ('laravel-settings.key_name ' ) => $ key ,
51- config ('laravel-settings.morph_type ' ) => null ,
52- config ('laravel-settings.morph_id ' ) => null ,
53- ])->first ();
54-
55- if ($ setting ) {
56- return $ setting ->value ;
57- }
58-
59- if (!is_null ($ default )) {
60- return $ default ;
61- }
62-
63- if (config ('laravel-settings.defaults ' ) && array_key_exists ($ key , config ('laravel-settings.defaults ' ))) {
64- return config ('laravel-settings.defaults ' )[$ key ];
65- }
66-
67- return $ default ;
49+ return $ this ->findSetting ($ key , null , null , $ default );
6850 }
6951
7052 /**
@@ -75,11 +57,7 @@ public function get(string $key, mixed $default = null): mixed
7557 */
7658 public function forget (string $ key ): void
7759 {
78- $ this ->model ::where ([
79- config ('laravel-settings.key_name ' ) => $ key ,
80- config ('laravel-settings.morph_type ' ) => null ,
81- config ('laravel-settings.morph_id ' ) => null ,
82- ])->delete ();
60+ $ this ->deleteSetting ($ key );
8361 }
8462
8563 /**
@@ -93,37 +71,95 @@ public function forget(string $key): void
9371 */
9472 public function setWithModel (string $ key , mixed $ value , string $ modelType , mixed $ modelId ): void
9573 {
74+ $ this ->storeSetting ($ key , $ value , $ modelType , $ modelId );
75+ }
76+
77+ /**
78+ * Get a setting value with model.
79+ *
80+ * @param string $key
81+ * @param string $modelType
82+ * @param mixed $modelId
83+ * @param mixed $default
84+ * @return mixed
85+ */
86+ public function getWithModel (string $ key , string $ modelType , mixed $ modelId , mixed $ default = null ): mixed
87+ {
88+ return $ this ->findSetting ($ key , $ modelType , $ modelId , $ default );
89+ }
90+
91+ /**
92+ * Forget a setting value with model.
93+ *
94+ * @param string $key
95+ * @param string $modelType
96+ * @param mixed $modelId
97+ * @return void
98+ */
99+ public function forgetWithModel (string $ key , string $ modelType , mixed $ modelId ): void
100+ {
101+ $ this ->deleteSetting ($ key , $ modelType , $ modelId );
102+ }
103+
104+ /**
105+ * Store a setting value.
106+ *
107+ * @param string $key
108+ * @param mixed $value
109+ * @param string $modelType
110+ * @param mixed $modelId
111+ * @return void
112+ */
113+ private function storeSetting (string $ key , mixed $ value , string $ modelType = null , mixed $ modelId = null ): void
114+ {
115+
96116 $ this ->model ::updateOrCreate (
97117 [
98118 config ('laravel-settings.key_name ' ) => $ key ,
99119 config ('laravel-settings.morph_type ' ) => $ modelType ,
100120 config ('laravel-settings.morph_id ' ) => $ modelId ,
101121 ],
102- [
103- 'value ' => $ value ,
104- ]
122+ [config ('laravel-settings.value_name ' ) => $ value ]
105123 );
124+
125+ if (config ('laravel-settings.with_cache ' )) {
126+ Cache::forget (Support::getCacheKey ($ key , $ modelType , $ modelId ));
127+ }
106128 }
107129
108130 /**
109- * Get a setting value with model .
131+ * Find a setting value.
110132 *
111133 * @param string $key
112134 * @param string $modelType
113135 * @param mixed $modelId
114136 * @param mixed $default
115137 * @return mixed
116138 */
117- public function getWithModel (string $ key , string $ modelType , mixed $ modelId , mixed $ default = null ): mixed
139+ private function findSetting (string $ key , string $ modelType = null , mixed $ modelId = null , mixed $ default = null ): mixed
118140 {
141+ if (config ('laravel-settings.with_cache ' )) {
142+ $ cacheKey = Support::getCacheKey ($ key , $ modelType , $ modelId );
143+
144+ if (Cache::has ($ cacheKey )) {
145+ return Cache::get ($ cacheKey );
146+ }
147+ }
148+
119149 $ setting = $ this ->model ::where ([
120150 config ('laravel-settings.key_name ' ) => $ key ,
121151 config ('laravel-settings.morph_type ' ) => $ modelType ,
122152 config ('laravel-settings.morph_id ' ) => $ modelId ,
123153 ])->first ();
124154
125155 if ($ setting ) {
126- return $ setting ->value ;
156+ $ value = $ setting ->value ;
157+
158+ if (config ('laravel-settings.with_cache ' )) {
159+ Cache::put ($ cacheKey , $ value , config ('laravel-settings.cache_lifetime ' ));
160+ }
161+
162+ return $ value ;
127163 }
128164
129165 if (!is_null ($ default )) {
@@ -135,26 +171,36 @@ public function getWithModel(string $key, string $modelType, mixed $modelId, mix
135171 array_key_exists ($ modelType , config ('laravel-settings.model_defaults ' )) &&
136172 array_key_exists ($ key , config ('laravel-settings.model_defaults ' )[$ modelType ])
137173 ) {
138- return config ('laravel-settings.model_defaults ' )[$ modelType ][$ key ];
174+ $ value = config ('laravel-settings.model_defaults ' )[$ modelType ][$ key ];
175+ return $ value ;
176+ }
177+
178+ if (config ('laravel-settings.defaults ' ) && array_key_exists ($ key , config ('laravel-settings.defaults ' ))) {
179+ $ value = config ('laravel-settings.defaults ' )[$ key ];
180+ return $ value ;
139181 }
140182
141183 return $ default ;
142184 }
143185
144186 /**
145- * Forget a setting value with model .
187+ * Delete a setting.
146188 *
147189 * @param string $key
148190 * @param string $modelType
149191 * @param mixed $modelId
150192 * @return void
151193 */
152- public function forgetWithModel (string $ key , string $ modelType , mixed $ modelId ): void
194+ private function deleteSetting (string $ key , string $ modelType = null , mixed $ modelId = null ): void
153195 {
154196 $ this ->model ::where ([
155197 config ('laravel-settings.key_name ' ) => $ key ,
156198 config ('laravel-settings.morph_type ' ) => $ modelType ,
157199 config ('laravel-settings.morph_id ' ) => $ modelId ,
158200 ])->delete ();
201+
202+ if (config ('laravel-settings.with_cache ' )) {
203+ Cache::forget (Support::getCacheKey ($ key , $ modelType , $ modelId ));
204+ }
159205 }
160206}
0 commit comments