7
7
use Jenssegers \Agent \Agent ;
8
8
use Coderstm \Models \Permission ;
9
9
use Coderstm \Models \PaymentMethod ;
10
+ use Illuminate \Support \Facades \Cache ;
10
11
use Illuminate \Support \Facades \Config ;
11
12
use Stevebauman \Location \Facades \Location ;
12
13
@@ -34,6 +35,22 @@ public static function location()
34
35
public static function loadConfigFromDatabase (...$ keys ): void
35
36
{
36
37
try {
38
+ $ cacheKey = 'app_config_ ' . md5 (implode ('_ ' , $ keys ));
39
+ $ cacheDuration = 60 ; // Cache for 60 minutes
40
+
41
+ // Return from cache if available
42
+ if (Cache::has ($ cacheKey )) {
43
+ $ cachedConfigs = Cache::get ($ cacheKey );
44
+ foreach ($ cachedConfigs as $ key => $ configs ) {
45
+ foreach ($ configs as $ alias => $ items ) {
46
+ foreach ($ items as $ attr => $ value ) {
47
+ Config::set ("$ alias. $ attr " , $ value );
48
+ }
49
+ }
50
+ }
51
+ return ;
52
+ }
53
+
37
54
$ options = [
38
55
'config ' => [
39
56
'alias ' => 'app ' ,
@@ -47,13 +64,20 @@ public static function loadConfigFromDatabase(...$keys): void
47
64
]
48
65
];
49
66
67
+ $ cachedConfigs = [];
68
+
50
69
foreach ($ keys as $ key ) {
51
70
// Determine the alias to use, defaulting to the key if not specified
52
71
$ option = $ options [$ key ] ?? [];
53
72
$ alias = $ option ['alias ' ] ?? $ key ;
73
+ $ cachedConfigs [$ key ] = [];
74
+ $ cachedConfigs [$ key ][$ alias ] = [];
54
75
55
76
// Fetch settings from the database
56
77
foreach (app_settings ($ key ) as $ attr => $ value ) {
78
+ // Store for caching
79
+ $ cachedConfigs [$ key ][$ alias ][$ attr ] = $ value ;
80
+
57
81
// Set the configuration value in the application's config
58
82
Config::set ("$ alias. $ attr " , $ value );
59
83
@@ -76,6 +100,9 @@ public static function loadConfigFromDatabase(...$keys): void
76
100
}
77
101
}
78
102
}
103
+
104
+ // Store in cache
105
+ Cache::put ($ cacheKey , $ cachedConfigs , now ()->addMinutes ($ cacheDuration ));
79
106
} catch (\Exception $ e ) {
80
107
throw $ e ;
81
108
}
@@ -84,33 +111,67 @@ public static function loadConfigFromDatabase(...$keys): void
84
111
public static function loadPaymentMethodsConfig (): void
85
112
{
86
113
try {
114
+ $ cacheKey = 'payment_methods_config ' ;
115
+ $ cacheDuration = 60 ; // Cache for 60 minutes
116
+
117
+ // Return from cache if available
118
+ if (Cache::has ($ cacheKey )) {
119
+ $ paymentConfigs = Cache::get ($ cacheKey );
120
+
121
+ if (!empty ($ paymentConfigs ['stripe ' ])) {
122
+ config ($ paymentConfigs ['stripe ' ]);
123
+ }
124
+
125
+ if (!empty ($ paymentConfigs ['paypal ' ])) {
126
+ config ($ paymentConfigs ['paypal ' ]);
127
+ }
128
+
129
+ if (!empty ($ paymentConfigs ['razorpay ' ])) {
130
+ config ($ paymentConfigs ['razorpay ' ]);
131
+ }
132
+
133
+ return ;
134
+ }
135
+
136
+ $ paymentConfigs = [
137
+ 'stripe ' => [],
138
+ 'paypal ' => [],
139
+ 'razorpay ' => []
140
+ ];
141
+
87
142
// Load cashier config from app payment methods table
88
143
if ($ stripe = PaymentMethod::stripe ()) {
89
- config ( [
144
+ $ paymentConfigs [ ' stripe ' ] = [
90
145
'cashier.key ' => $ stripe ->configs ['API_KEY ' ],
91
146
'cashier.secret ' => $ stripe ->configs ['API_SECRET ' ],
92
147
'cashier.webhook.secret ' => $ stripe ->configs ['WEBHOOK_SECRET ' ],
93
- ]);
148
+ ];
149
+ config ($ paymentConfigs ['stripe ' ]);
94
150
}
95
151
96
152
// Load paypal config from app payment methods table
97
153
if ($ paypal = PaymentMethod::paypal ()) {
98
154
$ mode = $ paypal ->test_mode ? 'sandbox ' : 'live ' ;
99
- config ( [
155
+ $ paymentConfigs [ ' paypal ' ] = [
100
156
'paypal.mode ' => $ mode ,
101
157
"paypal. {$ mode }.client_id " => $ paypal ->configs ['CLIENT_ID ' ],
102
158
"paypal. {$ mode }.client_secret " => $ paypal ->configs ['CLIENT_SECRET ' ],
103
159
'paypal.notify_url ' => $ paypal ->webhook ,
104
- ]);
160
+ ];
161
+ config ($ paymentConfigs ['paypal ' ]);
105
162
}
106
163
107
164
// Load razorpay config from app payment methods table
108
165
if ($ razorpay = PaymentMethod::razorpay ()) {
109
- config ( [
166
+ $ paymentConfigs [ ' razorpay ' ] = [
110
167
"razorpay.key_id " => $ razorpay ->configs ['API_KEY ' ],
111
168
"razorpay.key_secret " => $ razorpay ->configs ['API_SECRET ' ],
112
- ]);
169
+ ];
170
+ config ($ paymentConfigs ['razorpay ' ]);
113
171
}
172
+
173
+ // Store in cache
174
+ Cache::put ($ cacheKey , $ paymentConfigs , now ()->addMinutes ($ cacheDuration ));
114
175
} catch (\Exception $ e ) {
115
176
throw $ e ;
116
177
}
0 commit comments