Created
March 22, 2012 19:42
-
-
Save thinkphp/2162772 to your computer and use it in GitHub Desktop.
YQLCache minified
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var yqlcache=function(){var cacheid,cb,cancache=(("localStorage" in window)&&(window.localStorage!==null));function get(transaction){if(!transaction.id||!transaction.callback||!transaction.yql||!transaction.cacheage){return}var current,data;cb=transaction.callback;cacheid=transaction.id;if(!cancache){loadYQL(transaction.yql)}if(cancache){current=JSON.parse(localStorage.getItem(cacheid));if(current!==null){if((new Date().getTime()-current.time)<transaction.cacheage){cb({type:"cached",data:current.data})}else{loadYQL(transaction.yql)}}else{loadYQL(transaction.yql)}}}function loadYQL(yql){var old=document.getElementById("yqloadscript");if(old){old.parentNode.removeChild(old)}var YQL="http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(yql)+"&diagnostics=false&format=json&callback=yqlcache.cache";var s=document.createElement("script");s.setAttribute("type","text/javascript");s.id="yqloadscript";s.setAttribute("src",YQL);document.getElementsByTagName("head")[0].appendChild(s)}function cache(data){if(data.query&&data.query.results){var out=data.query.results;if(cancache){var timestamp=new Date().getTime();localStorage.setItem(cacheid,JSON.stringify({time:timestamp,data:out}));cb({type:"freshcache",data:out})}else{cb({type:"live",data:out})}}}return{cache:cache,get:get}}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment