From: Hypolite Petovan Date: Thu, 23 Aug 2018 00:12:19 +0000 (-0400) Subject: [mastodoncustomemojis] Fix caching X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=0c73b90f041e51c29fa5ce5a36481131cc9464ee;p=friendica-addons.git [mastodoncustomemojis] Fix caching --- diff --git a/mastodoncustomemojis/mastodoncustomemojis.php b/mastodoncustomemojis/mastodoncustomemojis.php index 2502344a..6ba96f11 100644 --- a/mastodoncustomemojis/mastodoncustomemojis.php +++ b/mastodoncustomemojis/mastodoncustomemojis.php @@ -66,41 +66,40 @@ function mastodoncustomemojis_put_item_in_cache(App $a, array &$hook_data) function mastodoncustomemojis_get_custom_emojis_for_author($author_link) { - $return = ['texts' => [], 'icons' => []]; - $url_parts = parse_url($author_link); $api_base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['port']) ? ':' . $url_parts['port'] : ''); $cache_key = 'mastodoncustomemojis:' . $api_base_url; - $emojis = Cache::get($cache_key); - if (empty($emojis['texts']) || Config::get('system', 'ignore_cache')) { - // Reset the emojis array - $emojis = $return; + $return = Cache::get($cache_key); - $api_url = $api_base_url . '/api/v1/custom_emojis'; + if (empty($return) || Config::get('system', 'ignore_cache')) { + $return = mastodoncustomemojis_fetch_custom_emojis_for_url($api_base_url); - $ret = Network::fetchUrlFull($api_url); + Cache::set($cache_key, $return, empty($return['texts']) ? Cache::HALF_HOUR : Cache::WEEK); + } - if ($ret['success']) { - $emojis_array = json_decode($ret['body'], true); + return $return; +} - if (is_array($emojis_array)) { - foreach ($emojis_array as $emoji) { - $emojis['texts'][] = ':' . $emoji['shortcode'] . ':'; - $emojis['icons'][] = ':' . $emoji['shortcode'] . ':'; - } - } +function mastodoncustomemojis_fetch_custom_emojis_for_url($api_base_url) +{ + $return = ['texts' => [], 'icons' => []]; - $ttl = Cache::WEEK; - } else { - $ttl = Cache::HALF_HOUR; - } + $api_url = $api_base_url . '/api/v1/custom_emojis'; - Cache::set($cache_key, $emojis, $ttl); + $ret = Network::fetchUrlFull($api_url); - $return = $emojis; + if ($ret['success']) { + $emojis_array = json_decode($ret['body'], true); + + if (is_array($emojis_array) && count($emojis_array)) { + foreach ($emojis_array as $emoji) { + $return['texts'][] = ':' . $emoji['shortcode'] . ':'; + $return['icons'][] = ':' . $emoji['shortcode'] . ':'; + } + } } return $return;