Преглед изворни кода

Switched to hash based template expiration.

Nathaniel van Diepen пре 10 година
родитељ
комит
883243b2d2
3 измењених фајлова са 26 додато и 20 уклоњено
  1. 6 2
      api.php
  2. 1 1
      index.php
  3. 19 17
      js/index.js

+ 6 - 2
api.php

@@ -196,7 +196,10 @@
 								$files = array_diff(scandir(PATH_DATA.'/'.$_GET['id']),array('..', '.','.htaccess','version'));
 								foreach($files as $k => $file){
 									if(pathinfo(PATH_DATA.'/'.$_GET['id'].'/'.$file,PATHINFO_EXTENSION) == 'template'){
-										array_push($manifest,basename($file,'.template'));
+										array_push($manifest,array(
+											'name'=>basename($file,'.template'),
+											'hash'=>md5_file(PATH_DATA.'/'.$_GET['id'].'/'.$file)
+										));
 									}
 								}
 								retj(array(
@@ -221,7 +224,8 @@
 							retj(array(
 								'template'=>file_get_contents(PATH_DATA.'/'.$_GET['id'].'/'.$_GET['name'].'.template'),
 								'name'=>$_GET['name'],
-								'type'=>$_GET['id']
+								'type'=>$_GET['id'],
+								'hash'=>md5_file(PATH_DATA.'/'.$_GET['id'].'/'.$_GET['name'].'.template')
 							));
 						}else{
 							retj(array(

+ 1 - 1
index.php

@@ -35,7 +35,7 @@
 			break;
 			case 'settings':
 				$settings = array();
-				$keys = array('expire');
+				$keys = array();
 				foreach($keys as $key){
 					$settings[$key] = get($key);
 				}

+ 19 - 17
js/index.js

@@ -41,9 +41,16 @@
 		getKey = window.getKey = function(){
 			return Key;
 		},
+		templateHash = function(type,name){
+			for(var i in templates){
+				if(templates[i].name == name && templates[i].type == type){
+					return templates[i].hash;
+				}
+			}
+			return false;
+		},
 		template = window.template = function(type,name,template){
-			var d = +new Date,
-				id = (function(type,name){
+			var id = (function(type,name){
 					for(var i in templates){
 						if(templates[i].name == name && templates[i].type == type){
 							return i;
@@ -57,31 +64,27 @@
 						templates.splice(id,1);
 					}
 					$.localStorage('templates',templates);
-					console.log('Dropping template for: '+name);
+					console.log('Dropping template for '+type+':'+name);
 					return '';
 				}else{
 					var o = {
 						name: name,
 						template: template,
 						type: type,
-						date: Number(get('expire'))+Number(d)
+						hash: templates[id].hash
 					}
 					if(id===false){
-						console.log('Storing new template for: '+name);
+						console.log('Storing new template for '+type+':'+name);
 						templates.push(o);
 					}else{
-						console.log('Replacing old template for: '+name);
+						console.log('Replacing old template for '+type+':'+name);
 						templates[id] = o;
 					}
 					$.localStorage('templates',templates);
 				}
 			}else if(id!==false){
-				console.log('Using cached template for: '+name);
+				console.log('Using cached template for '+type+':'+name);
 				var template = templates[id].template;
-				if(templates[id].date < d){
-					delete templates[name];
-					$.localStorage('templates',templates);
-				}
 				return template;
 			}else{
 				console.log('No cached template stored for: '+type+':'+name);
@@ -647,21 +650,20 @@
 						id: type
 					},function(d){
 					if(!exists(d.error)){
-						var count = d.manifest.length,
-							m = +new Date;
+						var count = d.manifest.length;
 						for(var i in d.manifest){
-							console.log('Loading template('+(Number(i)+1)+'/'+d.manifest.length+'): '+d.manifest[i]);
-							if(template(type,d.manifest[i]) === ''){
+							console.log('Loading template('+(Number(i)+1)+'/'+d.manifest.length+'): '+d.manifest[i].name);
+							if(templateHash(type,d.manifest[i].name) !== d.manifest[i].hash){
 								$.get('api.php',{
 									type: 'template',
 									id: type,
-									name: d.manifest[i]
+									name: d.manifest[i].name
 								},function(d){
 									templates.push({
 										name: d.name,
 										template: d.template,
 										type: d.type,
-										date: Number(get('expire'))+Number(m)
+										hash: d.hash
 									});
 									$.localStorage('templates',templates);
 									count--;