Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cache last entries in session, not an app process. closes #194.

  • Loading branch information...
commit d5b2ec5650d7bc18e14e8e3953a5adda2cf404c4 1 parent 32b3d9b
@nahi authored
View
4 app/controllers/entry_controller.rb
@@ -559,10 +559,12 @@ def unpin
private
def find_opt(ctx = @ctx)
+ cache = session[:cached_entries] ||= CachedEntries.new
updated_id = updated_id_in_flash()
ctx.find_opt.merge(
:allow_cache => updated_id.nil? && flash[:allow_cache],
- :updated_id => updated_id
+ :updated_id => updated_id,
+ :cached_entries => cache
)
end
View
4 app/controllers/setting_controller.rb
@@ -15,7 +15,6 @@ def index
@twitter_comment_hack = param(:twitter_comment_hack) || @setting.twitter_comment_hack
@list_view_media_rendering = param(:list_view_media_rendering) || @setting.list_view_media_rendering
@list_view_profile_picture = param(:list_view_profile_picture) || @setting.list_view_profile_picture
- @disable_status_icon = param(:disable_status_icon) || @setting.disable_status_icon
@link_open_new_window = param(:link_open_new_window) || @setting.link_open_new_window
@link_type = param(:link_type) || @setting.link_type
@google_maps_geocoding_lang = param(:google_maps_geocoding_lang) || @setting.google_maps_geocoding_lang
@@ -31,7 +30,6 @@ def update
:twitter_comment_hack,
:list_view_media_rendering,
:list_view_profile_picture,
- :disable_status_icon,
:link_open_new_window,
:link_type,
:google_maps_geocoding_lang
@@ -46,7 +44,7 @@ def update
end
end
# bool settings
- [:twitter_comment_hack, :list_view_media_rendering, :list_view_profile_picture, :disable_status_icon, :link_open_new_window].each do |key|
+ [:twitter_comment_hack, :list_view_media_rendering, :list_view_profile_picture, :link_open_new_window].each do |key|
instance_variable_set('@' + key.to_s, param(key) == 'checked')
@setting.send(key.to_s + '=', param(key) == 'checked')
end
View
4 app/helpers/entry_helper.rb
@@ -55,7 +55,7 @@ def cache_profile(entries)
rooms = []
# TODO: do not show status icon for query result page now for performance
# reason.
- if ctx.query.nil? and ctx.link.nil? and !setting.disable_status_icon
+ if ctx.query.nil? and ctx.link.nil? and !F2P::Config.disable_status_icon
entries.each do |t|
t.entries.each do |e|
if e.room
@@ -132,7 +132,7 @@ def icon(entry)
end
# TODO: do not show status icon for query result page now for performance
# reason.
- if ctx.query.nil? and ctx.link.nil? and !ctx.user_for and !setting.disable_status_icon
+ if ctx.query.nil? and ctx.link.nil? and !ctx.user_for and !F2P::Config.disable_status_icon
if entry_status(entry) != 'public'
str = icon_tag(:private) + str
end
View
3  app/models/cached_entries.rb
@@ -0,0 +1,3 @@
+class CachedEntries < Array
+ attr_accessor :opt
+end
View
53 app/models/entry_thread.rb
@@ -13,13 +13,15 @@ class << self
def find(opt = {})
auth = opt[:auth]
+ cache = opt[:cached_entries]
return nil unless auth
unless opt.key?(:merge_entry)
opt[:merge_entry] = true
end
opt.delete(:auth)
+ opt.delete(:cached_entries)
logger.info('[perf] start entries fetch')
- original = entries = fetch_entries(auth, opt)
+ original = entries = fetch_entries(auth, opt, cache)
logger.info('[perf] start internal data handling')
record_last_modified(entries)
logger.info('[perf] record_last_modified done')
@@ -96,15 +98,15 @@ def logger
ActiveRecord::Base.logger
end
- def fetch_entries(auth, opt)
+ def fetch_entries(auth, opt, cache)
if opt[:id]
- fetch_single_entry_as_array(auth, opt)
+ fetch_single_entry_as_array(auth, opt, cache)
else
- entries = fetch_list_entries(auth, opt)
+ entries = fetch_list_entries(auth, opt, cache)
if updated_id = opt[:updated_id]
entry = wrap(get_entry(auth, :id => updated_id)).first
if entry
- update_cache_entry(auth, entry)
+ update_cache_entry(cache, entry)
if entries.find { |e| e.id == updated_id }
replace_entry(entries, entry)
else
@@ -116,23 +118,18 @@ def fetch_entries(auth, opt)
end
end
- def fetch_single_entry_as_array(auth, opt)
- @entries_cache ||= {}
- allow_cache = opt[:allow_cache]
- if allow_cache
- if cached = @entries_cache[auth.name]
- entries = cached[1]
- if found = entries.find { |e| e.id == opt[:id] }
- logger.info("[cache] entry cache found for #{opt[:id]}")
- return [found]
- end
+ def fetch_single_entry_as_array(auth, opt, cache)
+ if opt[:allow_cache] and cache
+ if found = cache.find { |e| e.id == opt[:id] }
+ logger.info("[cache] entry cache found for #{opt[:id]}")
+ return [found]
end
end
wrap(Task.run { get_entry(auth, opt) }.result)
end
- def fetch_list_entries(auth, opt)
- cache_entries(auth, opt) {
+ def fetch_list_entries(auth, opt, cache)
+ cache_entries(opt, cache) {
if opt[:inbox]
start = opt[:start]
num = opt[:num]
@@ -179,8 +176,7 @@ def fetch_list_entries(auth, opt)
}
end
- def cache_entries(auth, opt, &block)
- @entries_cache ||= {}
+ def cache_entries(opt, cache, &block)
allow_cache = opt[:allow_cache]
opt = opt.dup
opt.delete(:allow_cache)
@@ -188,22 +184,23 @@ def cache_entries(auth, opt, &block)
opt.delete(:merge_entry)
opt.delete(:merge_service)
opt.delete(:filter_inbox_except)
- if allow_cache and @entries_cache[auth.name]
- cached_opt, entries = @entries_cache[auth.name]
- if opt == cached_opt
+ if allow_cache and cache
+ if opt == cache.opt
logger.info("[cache] entries cache found for #{opt.inspect}")
- return entries
+ return cache
end
end
entries = yield
- @entries_cache[auth.name] = [opt, entries]
+ if cache
+ cache.opt = opt
+ cache.replace(entries)
+ end
entries
end
- def update_cache_entry(auth, entry)
- opt, entries = @entries_cache[auth.name]
- if entries
- replace_entry(entries, entry)
+ def update_cache_entry(cache, entry)
+ if cache
+ replace_entry(cache, entry)
end
end
View
2  app/models/setting.rb
@@ -10,7 +10,6 @@ class Setting
attr_accessor :google_maps_geocoding_lang
attr_accessor :google_maps_zoom
attr_accessor :list_view_profile_picture
- attr_accessor :disable_status_icon
def initialize
super
@@ -25,7 +24,6 @@ def initialize
@google_maps_geocoding_lang = F2P::Config.google_maps_geocoding_lang
@google_maps_zoom = F2P::Config.google_maps_zoom
@list_view_profile_picture = F2P::Config.list_view_profile_picture
- @disable_status_icon = F2P::Config.disable_status_icon
end
def validate
View
1  app/views/setting/index.html.erb
@@ -11,7 +11,6 @@
show twitter username: <%= check_box_tag('twitter_comment_hack', 'checked', @twitter_comment_hack) %><br />
show medias in list view: <%= check_box_tag('list_view_media_rendering', 'checked', @list_view_media_rendering) %><br />
show profile picture in list view: <%= check_box_tag('list_view_profile_picture', 'checked', @list_view_profile_picture) %><br />
- disable status icon: <%= check_box_tag('disable_status_icon', 'checked', @disable_status_icon) %><br />
open links in new window: <%= check_box_tag('link_open_new_window', 'checked', @link_open_new_window) %><br />
open links via mobile proxy(gwt): <%= check_box_tag('link_type_gwt', 'checked', @link_type == 'gwt') %><br />
Google Maps geocoding language: <%= text_field_tag('google_maps_geocoding_lang', @google_maps_geocoding_lang) %><br />
View
2  config/environment.rb
@@ -165,5 +165,5 @@ class << self
F2P::Config.list_view_profile_picture = false
F2P::Config.medias_in_thread = 4
F2P::Config.next_entry_text_folding_size = 40
- F2P::Config.disable_status_icon = false
+ F2P::Config.disable_status_icon = true
end
View
3  test/unit/entry_thread_test.rb
@@ -88,8 +88,9 @@ class EntryThreadTest < ActiveSupport::TestCase
ff.expects(:get_inbox_entries).with('user1', nil, nil, nil).
returns(read_entries('entries', 'f2ptest')).times(1) # 1 time only
ff.stubs(:get_profiles)
+ cache = CachedEntries.new
2.times do
- threads = EntryThread.find(:auth => user, :inbox => true, :start => nil, :allow_cache => true)
+ threads = EntryThread.find(:auth => user, :inbox => true, :start => nil, :allow_cache => true, :cached_entries => cache)
assert_equal(
[1, 2, 1, 1, 1, 4, 2, 3, 3, 3, 1, 3, 1, 1, 1, 1, 1],
threads.map { |t| t.entries.size }
Please sign in to comment.
Something went wrong with that request. Please try again.