Skip to content

Commit

Permalink
cache last entries in session, not an app process. closes #194.
Browse files Browse the repository at this point in the history
  • Loading branch information
nahi committed Jul 9, 2009
1 parent 32b3d9b commit d5b2ec5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 39 deletions.
4 changes: 3 additions & 1 deletion app/controllers/entry_controller.rb
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions app/controllers/setting_controller.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/entry_helper.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/models/cached_entries.rb
@@ -0,0 +1,3 @@
class CachedEntries < Array
attr_accessor :opt
end
53 changes: 25 additions & 28 deletions app/models/entry_thread.rb
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -179,31 +176,31 @@ 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)
opt.delete(:updated_id)
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

Expand Down
2 changes: 0 additions & 2 deletions app/models/setting.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/views/setting/index.html.erb
Expand Up @@ -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 />
Expand Down
2 changes: 1 addition & 1 deletion config/environment.rb
Expand Up @@ -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
3 changes: 2 additions & 1 deletion test/unit/entry_thread_test.rb
Expand Up @@ -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 }
Expand Down

0 comments on commit d5b2ec5

Please sign in to comment.