mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
cache non-existing UUIDs
This commit is contained in:
parent
1464604536
commit
b59d6ced07
@ -44,7 +44,14 @@ exp.save_hash = function(uuid, hash) {
|
|||||||
// null when uuid unkown
|
// null when uuid unkown
|
||||||
exp.get_details = function(uuid, callback) {
|
exp.get_details = function(uuid, callback) {
|
||||||
redis.hgetall(uuid, function(err, data) {
|
redis.hgetall(uuid, function(err, data) {
|
||||||
callback(err, data);
|
var details = null;
|
||||||
|
if (data) {
|
||||||
|
details = {
|
||||||
|
hash: (data.h == "null" ? null : data.h),
|
||||||
|
time: data.t
|
||||||
|
};
|
||||||
|
}
|
||||||
|
callback(err, details);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,11 @@ function get_hash(url) {
|
|||||||
function store_images(uuid, details, callback) {
|
function store_images(uuid, details, callback) {
|
||||||
// get profile for +uuid+
|
// get profile for +uuid+
|
||||||
networking.get_profile(uuid, function(err, profile) {
|
networking.get_profile(uuid, function(err, profile) {
|
||||||
if (err) {
|
if (err === 0) {
|
||||||
|
// uuid does not exist
|
||||||
|
cache.save_hash(uuid, null);
|
||||||
|
callback(null, null);
|
||||||
|
} else if (err) {
|
||||||
callback(err, null);
|
callback(err, null);
|
||||||
} else {
|
} else {
|
||||||
var skinurl = skin_url(profile);
|
var skinurl = skin_url(profile);
|
||||||
@ -23,7 +27,7 @@ function store_images(uuid, details, callback) {
|
|||||||
console.log(uuid + " " + skinurl);
|
console.log(uuid + " " + skinurl);
|
||||||
// set file paths
|
// set file paths
|
||||||
var hash = get_hash(skinurl);
|
var hash = get_hash(skinurl);
|
||||||
if (details && details.h == hash) {
|
if (details && details.hash == hash) {
|
||||||
// hash hasn't changed
|
// hash hasn't changed
|
||||||
console.log(uuid + " hash has not changed");
|
console.log(uuid + " hash has not changed");
|
||||||
cache.update_timestamp(uuid);
|
cache.update_timestamp(uuid);
|
||||||
@ -79,15 +83,15 @@ function get_image_hash(uuid, callback) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
callback(err, -1, null);
|
callback(err, -1, null);
|
||||||
} else {
|
} else {
|
||||||
if (details && details.t + config.local_cache_time >= new Date().getTime()) {
|
if (details && details.time + config.local_cache_time >= new Date().getTime()) {
|
||||||
// uuid known + recently updated
|
// uuid known + recently updated
|
||||||
console.log(uuid + " uuid known & recently updated");
|
console.log(uuid + " uuid known & recently updated");
|
||||||
callback(null, 1, details.h);
|
callback(null, (details.hash ? 1 : 0), details.hash);
|
||||||
} else {
|
} else {
|
||||||
console.log(uuid + " uuid not known or too old");
|
console.log(uuid + " uuid not known or too old");
|
||||||
store_images(uuid, details, function(err, hash) {
|
store_images(uuid, details, function(err, hash) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err, -1, details && details.h);
|
callback(err, -1, details && details.hash);
|
||||||
} else {
|
} else {
|
||||||
console.log(uuid + " hash: " + hash);
|
console.log(uuid + " hash: " + hash);
|
||||||
callback(null, (hash ? 2 : 3), hash);
|
callback(null, (hash ? 2 : 3), hash);
|
||||||
|
|||||||
@ -17,22 +17,21 @@ exp.get_profile = function(uuid, callback) {
|
|||||||
// profile downloaded successfully
|
// profile downloaded successfully
|
||||||
console.log(uuid + " profile downloaded");
|
console.log(uuid + " profile downloaded");
|
||||||
callback(null, JSON.parse(body));
|
callback(null, JSON.parse(body));
|
||||||
|
} else if (error) {
|
||||||
|
callback(error, null);
|
||||||
|
} else if (response.statusCode == 204 || response.statusCode == 404) {
|
||||||
|
// we get 204 No Content when UUID doesn't exist (including 404 in case they change that)
|
||||||
|
console.log(uuid + " uuid does not exist");
|
||||||
|
callback(0, null);
|
||||||
|
} else if (response.statusCode == 429) {
|
||||||
|
// Too Many Requests
|
||||||
|
console.warn(uuid + " Too many requests");
|
||||||
|
console.warn(body);
|
||||||
|
callback(null, null);
|
||||||
} else {
|
} else {
|
||||||
if (error) {
|
console.error(uuid + " Unknown error:");
|
||||||
callback(error, null);
|
console.error(response);
|
||||||
return;
|
console.error(body);
|
||||||
} else if (response.statusCode == 204 || response.statusCode == 404) {
|
|
||||||
// we get 204 No Content when UUID doesn't exist (including 404 in case they change that)
|
|
||||||
console.log(uuid + " uuid does not exist");
|
|
||||||
} else if (response.statusCode == 429) {
|
|
||||||
// Too Many Requests
|
|
||||||
console.warn(uuid + " Too many requests");
|
|
||||||
console.warn(body);
|
|
||||||
} else {
|
|
||||||
console.error(uuid + " Unknown error:");
|
|
||||||
console.error(response);
|
|
||||||
console.error(body);
|
|
||||||
}
|
|
||||||
callback(null, null);
|
callback(null, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -35,7 +35,7 @@ router.get('/:uuid', function(req, res) {
|
|||||||
}
|
}
|
||||||
} else if (status == 1 || status == 2) {
|
} else if (status == 1 || status == 2) {
|
||||||
sendimage(200, status == 1, image);
|
sendimage(200, status == 1, image);
|
||||||
} else if (status == 3) {
|
} else if (status == 0 || status == 3) {
|
||||||
handle_404(def);
|
handle_404(def);
|
||||||
} else {
|
} else {
|
||||||
console.error("unexpected error/status");
|
console.error("unexpected error/status");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user