Skip to content

Commit 6f3a038

Browse files
brockbolandJake Bell
authored andcommitted
Caching gist requests.
1 parent 9eb50a1 commit 6f3a038

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

gist_filter.module

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,20 @@ function _gist_display_link($matches) {
143143
* The data from the Github API.
144144
*/
145145
function gist_filter_get_gist($id) {
146-
static $gists = array();
147-
148-
if (!isset($gists[$id])) {
146+
// Cache ID
147+
$cid = 'gist:' . $id;
148+
// Check if this gist is already in the cache
149+
$gist = cache_get($cid);
150+
151+
if (!$gist) {
152+
// Not available in the cache, so retrive the gist from Github
149153
$url = 'https://api.github.com/gists/' . $id;
150154
$response = drupal_http_request($url, array('headers' => array('Content-Type' => 'application/json')));
151-
$gists[$id] = drupal_json_decode($response->data);
155+
$gist = drupal_json_decode($response->data);
156+
157+
// Cache the gist until the next cache flush
158+
cache_set($cid, $gist, 'cache', CACHE_TEMPORARY);
152159
}
153160

154-
return $gists[$id];
161+
return $gist;
155162
}

0 commit comments

Comments
 (0)