55use App \Exceptions \TwitchFormatException ;
66use Illuminate \Http \Request ;
77
8- use App \Http \Requests ;
98use App \Http \Controllers \Controller ;
109
1110use App \User ;
1817use App \Repositories \TwitchEmotesApiRepository ;
1918
2019use Carbon \Carbon ;
21- use Carbon \CarbonInterval ;
2220use DateTimeZone ;
2321
24- use Symfony \Component \DomCrawler \Crawler ;
25- use Symfony \Component \CssSelector ;
26- use GuzzleHttp \Client ;
27-
2822use Crypt ;
2923use Illuminate \Contracts \Encryption \DecryptException ;
3024
@@ -248,6 +242,14 @@ public function avatar(Request $request, $user = null)
248242 return Helper::text (__ ('generic.username_required ' ));
249243 }
250244
245+ /**
246+ * Return avatar URL from cache if it exists.
247+ */
248+ $ cacheKey = sprintf ('twitch_avatar_%s ' , md5 ($ user ));
249+ if (Cache::has ($ cacheKey )) {
250+ return Helper::text (Cache::get ($ cacheKey ));
251+ }
252+
251253 $ id = $ request ->input ('id ' , 'false ' );
252254 try {
253255 $ data = $ id === 'true ' ? $ this ->api ->userById ($ user ) : $ this ->api ->userByUsername ($ user );
@@ -268,11 +270,13 @@ public function avatar(Request $request, $user = null)
268270 ]));
269271 }
270272
271- if (empty ($ data ['avatar ' ])) {
272- return Helper::text ($ this ->defaultAvatar );
273- }
273+ // Fallback to the default avatar if necessary.
274+ $ avatar = $ data ['avatar ' ] ?? $ this ->defaultAvatar ;
274275
275- return Helper::text ($ data ['avatar ' ]);
276+ // Cache the avatar URL for 5 minutes.
277+ Cache::put ($ cacheKey , $ avatar , config ('twitch.cache.avatar ' ));
278+
279+ return Helper::text ($ avatar );
276280 }
277281
278282 /**
@@ -851,6 +855,18 @@ public function gameOrStatus(Request $request, $route, $channel = null)
851855 }
852856 }
853857
858+ /**
859+ * Check cache for game/status.
860+ */
861+ $ cacheId = md5 ($ channel );
862+ $ cacheGame = sprintf ('twitch_game_%s ' , $ cacheId );
863+ $ cacheStatus = sprintf ('twitch_status_%s ' , $ cacheId );
864+
865+ $ cacheKey = $ route === 'game ' ? $ cacheGame : $ cacheStatus ;
866+ if (Cache::has ($ cacheKey )) {
867+ return Helper::text (Cache::get ($ cacheKey ));
868+ }
869+
854870 try {
855871 $ getChannel = $ this ->api ->channelById ($ channel );
856872 } catch (TwitchApiException $ ex ) {
@@ -859,11 +875,20 @@ public function gameOrStatus(Request $request, $route, $channel = null)
859875 return Helper::text ($ ex ->getMessage ());
860876 }
861877
878+ $ game = $ getChannel ['game ' ]['name ' ];
879+ $ status = $ getChannel ['title ' ];
880+
881+ /**
882+ * We can cache both values as it's from the same request anyways.
883+ */
884+ Cache::put ($ cacheGame , $ game , config ('twitch.cache.game ' ));
885+ Cache::put ($ cacheStatus , $ status , config ('twitch.cache.status ' ));
886+
862887 if ($ route === 'game ' ) {
863- return Helper::text ($ getChannel [ ' game ' ][ ' name ' ] ?: '' );
888+ return Helper::text ($ game ?: '' );
864889 }
865890
866- return Helper::text ($ getChannel [ ' title ' ] );
891+ return Helper::text ($ status );
867892 }
868893
869894 /**
@@ -1342,6 +1367,12 @@ public function subList(Request $request, $channel = null)
13421367 $ channel = $ channel ?: $ request ->input ('channel ' , null );
13431368 $ token = $ request ->input ('token ' , null );
13441369 $ amount = intval ($ request ->input ('count ' , 1 ));
1370+
1371+ // Fallback to 1
1372+ if ($ amount < 1 ) {
1373+ $ amount = 1 ;
1374+ }
1375+
13451376 $ field = $ request ->input ('field ' , 'name ' );
13461377 $ separator = $ request ->input ('separator ' , ', ' );
13471378 $ needToReAuth = '' ;
@@ -1409,7 +1440,7 @@ public function subList(Request $request, $channel = null)
14091440 }
14101441
14111442 $ limit = 100 ;
1412- $ data = $ this ->twitchApi ->channelSubscriptions ($ tokenData ['user_id ' ], $ token , $ limit , 0 , $ direction = 'desc ' , $ this ->version );
1443+ $ data = $ this ->twitchApi ->channelSubscriptions ($ tokenData ['user_id ' ], $ token , $ limit , 0 , 'desc ' , $ this ->version );
14131444
14141445 if (!empty ($ data ['error ' ])) {
14151446 return Helper::text (sprintf ('%s - %s (%s) ' , __ ('generic.error_loading_data_api ' ), $ data ['error ' ], $ data ['message ' ]));
@@ -1805,7 +1836,7 @@ public function subpoints(Request $request, $channel = null)
18051836 * Cache subpoints for one minute,
18061837 * to prevent excessive requests to the Twitch API.
18071838 */
1808- Cache::put ($ cacheKey , $ subpoints , 60 );
1839+ Cache::put ($ cacheKey , $ subpoints , config ( ' twitch.cache.subpoints ' ) );
18091840
18101841 /**
18111842 * Subtract user-supplied value.
@@ -2184,6 +2215,14 @@ public function viewercount(Request $request, $channel = null)
21842215 }
21852216 }
21862217
2218+ /**
2219+ * Load viewercount from cache to prevent unneccessary API request.
2220+ */
2221+ $ cacheKey = sprintf ('twitch_viewercount_%s ' , md5 ($ channel ));
2222+ if (Cache::has ($ cacheKey )) {
2223+ return Helper::text (Cache::get ($ cacheKey ));
2224+ }
2225+
21872226 $ stream = $ this ->twitchApi ->streams ($ channel , $ this ->version );
21882227
21892228 if (!empty ($ stream ['status ' ])) {
@@ -2196,6 +2235,8 @@ public function viewercount(Request $request, $channel = null)
21962235 }
21972236
21982237 $ viewers = $ stream ['stream ' ]['viewers ' ];
2238+ // Add viewercount to the cache and cache it for 60 seconds.
2239+ Cache::put ($ cacheKey , $ viewers , config ('twitch.cache.viewercount ' ));
21992240 return Helper::text ($ viewers );
22002241 }
22012242
0 commit comments