Commit 69eaf167 authored by Albert Yi's avatar Albert Yi Committed by GitHub

Merge pull request #3717 from r888888888/autocomplete-cache-control

eliminate usage of localstorage to cache tag autocomplete results (fixes #3543)
parents cc0f2a9a da34c95d
......@@ -3,9 +3,6 @@
Danbooru.Autocomplete.AUTOCOMPLETE_VERSION = 1;
//Just under 5MB of 16-bit characters
Danbooru.Autocomplete.MAX_STORAGE_SIZE = 2500000;
Danbooru.Autocomplete.PREFIXES = /^(-|~|<%= TagCategory.mapping.keys.map {|category| category + ':'}.join('|') %>)(.*)$/i;
Danbooru.Autocomplete.METATAGS = /^(<%= Tag::METATAGS %>):(.*)$/i;
......@@ -25,7 +22,7 @@
_renderItem: Danbooru.Autocomplete.render_item,
});
Danbooru.Autocomplete.enable_local_storage = this.test_local_storage();
Danbooru.Autocomplete.enable_local_storage = false;
this.initialize_tag_autocomplete();
this.initialize_mention_autocomplete($(".autocomplete-mentions textarea"));
this.initialize_artist_autocomplete($('[data-autocomplete="artist"]'));
......@@ -35,29 +32,13 @@
}
}
Danbooru.Autocomplete.test_local_storage = function() {
try {
$.localStorage.set("test", "test");
$.localStorage.remove("test");
return true;
} catch(e) {
return false;
}
}
// todo: remove eventually
Danbooru.Autocomplete.prune_local_storage = function() {
if (this.enable_local_storage) {
var cached_autocomplete_version = $.localStorage.get("danbooru-autocomplete-version");
var current_cache_size = Object.keys(localStorage).reduce( function(total, key) { return total + localStorage[key].length; }, 0);
if (cached_autocomplete_version !== this.AUTOCOMPLETE_VERSION || current_cache_size > this.MAX_STORAGE_SIZE) {
$.each(Object.keys(localStorage), function(i, key) {
if (key.substr(0, 3) === "ac-") {
$.localStorage.remove(key);
}
});
$.localStorage.set("danbooru-autocomplete-version", this.AUTOCOMPLETE_VERSION);
$.each(Object.keys(localStorage), function(i, key) {
if (key.substr(0, 3) === "ac-") {
$.localStorage.remove(key);
}
}
});
}
Danbooru.Autocomplete.initialize_mention_autocomplete = function($fields) {
......@@ -213,7 +194,8 @@
"search[name]": req.term + "*",
"search[is_active]": true,
"search[order]": "post_count",
"limit": 10
"limit": 10,
"expiry": 7
},
method: "get",
success: function(data) {
......@@ -254,7 +236,8 @@
"search[title]": req.term + "*",
"search[hide_deleted]": "Yes",
"search[order]": "post_count",
"limit": 10
"limit": 10,
"expiry": 7
},
method: "get",
success: function(data) {
......@@ -274,22 +257,12 @@
Danbooru.Autocomplete.normal_source = function(term, resp) {
var key = "ac-" + term.replace(/\./g,'\uFFFF');
if (this.enable_local_storage) {
var cached = $.localStorage.get(key);
if (cached) {
if (Date.parse(cached.expires) < new Date().getTime()) {
$.localStorage.remove(key);
} else {
resp(cached.value);
return;
}
}
}
$.ajax({
url: "/tags/autocomplete.json",
data: {
"search[name_matches]": term
"search[name_matches]": term,
"expiry": 7
},
method: "get",
success: function(data) {
......@@ -304,11 +277,6 @@
};
});
if (Danbooru.Autocomplete.enable_local_storage) {
var expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
$.localStorage.set(key, {"value": d, "expires": expiry});
}
resp(d);
}
});
......
......@@ -44,6 +44,7 @@ class ArtistsController < ApplicationController
end
format.json do
render :json => @artists.to_json(:include => [:urls])
expires_in params[:expiry].to_i.days if params[:expiry]
end
end
end
......@@ -108,6 +109,7 @@ class ArtistsController < ApplicationController
end
format.json do
render :json => @artists.to_json(:include => [:sorted_urls])
expires_in params[:expiry].to_i.days if params[:expiry]
end
end
end
......
......@@ -22,6 +22,10 @@ class PoolsController < ApplicationController
format.xml do
render :xml => @pools.to_xml(:root => "pools")
end
format.json do
render json: @pool.to_json
expires_in params[:expiry].to_i.days if params[:expiry]
end
end
end
......
......@@ -8,6 +8,7 @@ class SourcesController < ApplicationController
respond_with(@source.to_h) do |format|
format.xml { render xml: @source.to_h.to_xml(root: "source") }
format.json { render json: @source.to_h.to_json }
end
end
......
......@@ -19,6 +19,7 @@ class TagsController < ApplicationController
def autocomplete
@tags = Tag.names_matches_with_aliases(params[:search][:name_matches])
expires_in 7.days
respond_with(@tags) do |format|
format.xml do
......
......@@ -27,6 +27,10 @@ class UsersController < ApplicationController
format.xml do
render :xml => @users.to_xml(:root => "users")
end
format.json do
render json: @users.to_json
expires_in params[:expiry].to_i.days if params[:expiry]
end
end
end
end
......
......@@ -29,6 +29,10 @@ class WikiPagesController < ApplicationController
format.xml do
render :xml => @wiki_pages.to_xml(:root => "wiki-pages")
end
format.json do
render json: @wiki_pages.to_json
expires_in params[:expiry].to_i.days if params[:expiry]
end
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment