Skip to content

Commit 891bf6c

Browse files
author
Jake Bell
committed
- Re-implementing static caching.
- Adding tests for code tag display style.
1 parent 6f3a038 commit 891bf6c

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

gist_filter.module

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ function _gist_display_code($matches) {
9999
}
100100
// Otherwise, render all files.
101101
else {
102+
module_load_include('php', 'devel', 'krumo/class.krumo');
103+
krumo($data);
102104
foreach ($data['files'] as $file) {
103105
$output .= theme('gist_filter_code', array(
104106
'file' => $file,
@@ -143,20 +145,30 @@ function _gist_display_link($matches) {
143145
* The data from the Github API.
144146
*/
145147
function gist_filter_get_gist($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
153-
$url = 'https://api.github.com/gists/' . $id;
154-
$response = drupal_http_request($url, array('headers' => array('Content-Type' => 'application/json')));
155-
$gist = drupal_json_decode($response->data);
148+
static $gists = array();
149+
150+
// First, try the static cache.
151+
if (!isset($gists[$id])) {
152+
// Cache ID
153+
$cid = 'gist_filter:gist:' . $id;
154+
// Check if this gist is already in the cache
155+
$gist = cache_get($cid);
156156

157-
// Cache the gist until the next cache flush
158-
cache_set($cid, $gist, 'cache', CACHE_TEMPORARY);
157+
if ($cached = cache_get($cid)) {
158+
$gist = $cached->data;
159+
}
160+
else {
161+
// Not available in the cache, so retrive the gist from Github
162+
$url = 'https://api.github.com/gists/' . $id;
163+
$response = drupal_http_request($url, array('headers' => array('Content-Type' => 'application/json')));
164+
$gist = drupal_json_decode($response->data);
165+
166+
// Cache the gist until the next cache flush
167+
cache_set($cid, $gist, 'cache', CACHE_TEMPORARY);
168+
}
169+
170+
$gists[$id] = $gist;
159171
}
160172

161-
return $gist;
173+
return $gists[$id];
162174
}

gist_filter.test

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ class GistFilterTestCase extends DrupalWebTestCase {
4949
$langcode = LANGUAGE_NONE;
5050
$edit = array(
5151
"title" => $this->randomName(),
52-
"body[$langcode][0][value]" => 'Hello! [gist:788436]',
52+
"body[$langcode][0][value]" => 'Hello! [gist:865412]',
5353
);
5454
$result = $this->drupalPost('node/add/' . $this->contentType->type, $edit, t('Save'));
5555
$this->assertResponse(200);
5656
$this->assertRaw("Hello! ");
57-
$this->assertRaw('<script src="http://gist.github.com/788436.js"></script>');
57+
$this->assertRaw('<script src="http://gist.github.com/865412.js"></script>');
5858

5959
}
6060

@@ -99,11 +99,11 @@ class GistFilterTestCase extends DrupalWebTestCase {
9999
$langcode = LANGUAGE_NONE;
100100
$edit = array(
101101
"title" => $this->randomName(),
102-
"body[$langcode][0][value]" => 'Hello! [gist:788436]',
102+
"body[$langcode][0][value]" => 'Hello! [gist:865412]',
103103
);
104104
$result = $this->drupalPost('node/add/' . $this->contentType->type, $edit, t('Save'));
105105
$this->assertResponse(200);
106-
$this->assertRaw('Hello! <a href="http://gist.github.com/788436">http://gist.github.com/788436</a>');
106+
$this->assertRaw('Hello! <a href="http://gist.github.com/865412">http://gist.github.com/865412</a>');
107107

108108
}
109109

@@ -131,4 +131,44 @@ class GistFilterTestCase extends DrupalWebTestCase {
131131

132132
}
133133

134+
/**
135+
* Testing the code tag option.
136+
*/
137+
function testCodeTagStyle() {
138+
139+
// Turn on our input filter and set the option to link.
140+
$edit = array(
141+
'filters[gist_filter][status]' => 1,
142+
'filters[gist_filter][settings][gist_filter_display_method]' => 'code',
143+
);
144+
$this->drupalPost('admin/config/content/formats/plain_text', $edit, t('Save configuration'));
145+
146+
// Create a test node
147+
$langcode = LANGUAGE_NONE;
148+
$edit = array(
149+
"title" => $this->randomName(),
150+
"body[$langcode][0][value]" => 'Hello! [gist:865412]',
151+
);
152+
$result = $this->drupalPost('node/add/' . $this->contentType->type, $edit, t('Save'));
153+
$this->assertResponse(200);
154+
$this->assertPattern("@<pre type=\"php\">(.*)echo(.*)</pre>@sm");
155+
$this->assertPattern("@<pre type=\"ruby\">(.*)a = 1\nputs a</pre>@");
156+
157+
}
158+
159+
/**
160+
* Test that our API retrieval function caches calls to the Github API.
161+
*/
162+
function testGistCachingTest() {
163+
// Make sure our cache is all cleared first.
164+
cache_clear_all('gist_filter:gist', 'cache', TRUE);
165+
$this->assertFalse(cache_get('gist_filter:gist:865412'));
166+
167+
gist_filter_get_gist(865412);
168+
169+
// Now the cache should be set.
170+
$cached = cache_get('gist_filter:gist:865412');
171+
$this->assertTrue($cached->data);
172+
}
173+
134174
}

0 commit comments

Comments
 (0)