Skip to content
Browse files

added caching.

In general getting content by filepath's was sending 2 requests,
which reaches the request-limit of 60 very fast. Even though the
limits are on an IP basis this caching scheme seems best.

I also realize that ajax calls have a `cache: true`. However, this
was not working for `$.getGithubFile` for some reason, and this
method works absolutely.
  • Loading branch information...
1 parent 2a4607a commit 9ed573039285fc622e06f6c02e650cb93b5e48d9 @brntbeer committed Oct 30, 2012
Showing with 33 additions and 16 deletions.
  1. +33 −16 src/main/javascript/github-files.js
View
49 src/main/javascript/github-files.js
@@ -1,4 +1,7 @@
;(function ($) {
+ var githubCacheFilePath = [];
+ var githubCacheSha = [];
+
var fnSuccess =
function (data, startLineNum, endLineNum, callback) {
if (data.data.content && data.data.encoding === "base64") {
@@ -14,24 +17,38 @@
};
$.getGithubFileByFilePath =
- function(user, repo, filePath, callback, startLineNum, endLineNum) {
- $.ajax({
- type: "GET"
- ,url: "https://api.github.com/repos/" + user + "/" + repo + "/contents/"+filePath
- ,dataType: "jsonp"
- ,success: function(data){
- $.getGithubFile(user, repo, data.data.sha, callback, startLineNum, endLineNum)
- }
- });
+ function(user, repo, filePath, cacheIt, callback, startLineNum, endLineNum) {
+ if(cacheIt && githubCacheFilePath[filePath]){
+ $.getGithubFile(user, repo, githubCacheFilePath[filePath], true, callback, startLineNum, endLineNum)
+ }else{
+ delete githubCacheFilePath[filePath]
+ $.ajax({
+ type: "GET"
+ ,url: "https://api.github.com/repos/" + user + "/" + repo + "/contents/"+filePath
+ ,dataType: "jsonp"
+ ,success: function(data){
+ githubCacheFilePath[filePath] = data.data.sha;
+ $.getGithubFile(user, repo, githubCacheFilePath[filePath], cacheIt, callback, startLineNum, endLineNum)
+ }
+ });
+ }
};
$.getGithubFile =
- function(user, repo, sha, callback, startLineNum, endLineNum) {
- $.ajax({
- type: "GET"
- ,url: "https://api.github.com/repos/" + user + "/" + repo + "/git/blobs/" + sha
- ,dataType: "jsonp"
- ,success: function(data) {fnSuccess(data, +startLineNum || 1, +endLineNum || 0, callback);}
- });
+ function(user, repo, sha, cacheIt, callback, startLineNum, endLineNum) {
+ if(cacheIt && githubCacheSha[sha]){
+ fnSuccess(githubCacheSha[sha], +startLineNum || 1, +endLineNum || 0, callback);
+ }else{
+ delete githubCacheSha[sha]
+ $.ajax({
+ type: "GET"
+ ,url: "https://api.github.com/repos/" + user + "/" + repo + "/git/blobs/" + sha
+ ,dataType: "jsonp"
+ ,success: function(data) {
+ githubCacheSha[sha] = data
+ fnSuccess(githubCacheSha[sha], +startLineNum || 1, +endLineNum || 0, callback);
+ }
+ });
+ }
};
}(jQuery));

0 comments on commit 9ed5730

Please sign in to comment.
Something went wrong with that request. Please try again.