Skip to content

Instantly share code, notes, and snippets.

@derpixler
Created August 17, 2011 08:10
Show Gist options
  • Save derpixler/1151078 to your computer and use it in GitHub Desktop.
Save derpixler/1151078 to your computer and use it in GitHub Desktop.
Clint Side Caching
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Clint Side Caching</title>
</head>
<body>
<div id="iframe"></div>
</body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script>
// Cache Leeren
// window['name'] = '';
var cacheObj = {"array_elm1":"content_array_elm1",
"array_elm2":"content_array_elm2",
"array_elm3":"content_array_elm3",
"array_elm4":"content_array_elm4"
};
//var cacheObj = 'Chache me';
// Timestamp erzeugen
var timeStamp = new Date();
// WindowCache einschalten
var windowCache = true;
if(windowCache == true){
// WindowCache Optionen
var windowCacheLifetime = 36000;
var windowCacheName = 'myCache';
// WindowCache abrufen
cacheData = jQuery.parseJSON(window.name);
console.debug(cacheData);
/*
* Prüfen ob bereits Daten im WindowCache vorhanden sind.
* Wenn Daten im WindowCache vorhanden sind, werden diese in enstprechende
* Variablen zwischen gespeichert
*/
if(cacheData){
cacheData = cacheData.windowCache[windowCacheName];
if(!cacheData) cacheData = false;
data = cacheData.cacheData;
cacheTimeStamp = parseInt(cacheData.timeStamp);
nowTimeStamp = parseInt(timeStamp.getTime());
lifeTime = cacheData.LifeTime;
}
/*
* Prüfen ob bereits Daten im WindowCache vorhanden sind und ob die Cache
* Lifetime abglaufen ist
*/
if(!cacheData || nowTimeStamp - cacheTimeStamp > windowCacheLifetime){
// json-String im window.name Objekt ablegen
window['name'] = '{"windowCache":{"'+windowCacheName+'":{"lifeTime":"'+windowCacheLifetime+'","timeStamp":"'+timeStamp.getTime()+'","cacheData":'+JSON.stringify(cacheObj)+'}}}' ;
}
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment