Permalink
Browse files

do not cache room/list profiles. closes #119.

still caches user profile for now.
do thread fetching profiles.
  • Loading branch information...
1 parent 0486f98 commit f98ce4741325ce936195b11589e63426a87505fe @nahi committed May 28, 2009
@@ -172,6 +172,13 @@ def default_action
end
end
+ def initialize(*arg)
+ super
+ @user_profiles = {}
+ @room_profiles = {}
+ @list_profiles = {}
+ end
+
verify :only => :list,
:method => [:get, :post],
:add_flash => {:error => 'verify failed'},
@@ -181,7 +188,9 @@ def list
@ctx = restore_ctx { |ctx|
ctx.parse(params, @setting)
}
- @entries = find_entry_thread(find_opt)
+ with_profile_cache(@ctx) do
+ @entries = find_entry_thread(find_opt)
+ end
initialize_checked_modified
end
@@ -203,7 +212,9 @@ def inbox
ctx.fold = param(:fold) != 'no'
}
retry_times = @ctx.start.zero? ? 0 : F2P::Config.max_skip_empty_inbox_pages
- @entries = find_entry_thread(find_opt)
+ with_profile_cache(@ctx) do
+ @entries = find_entry_thread(find_opt)
+ end
retry_times.times do
break unless @entries.empty?
if param(:direction) == 'rewind'
@@ -245,7 +256,9 @@ def show
if ctx = session[:ctx]
ctx.eid = @ctx.eid
end
- @entries = find_entry_thread(find_opt)
+ with_profile_cache(@ctx) do
+ @entries = find_entry_thread(find_opt)
+ end
render :action => 'list'
end
@@ -597,4 +610,27 @@ def do_location_search
def find_entry_thread(opt)
EntryThread.find(opt) || []
end
+
+ def with_profile_cache(ctx)
+ tasks = []
+ if user = ctx.user_for
+ tasks << Task.run {
+ @user_profiles[user] = User.ff_profile(auth, user)
+ }
+ end
+ if room = ctx.room_for
+ tasks << Task.run {
+ @room_profiles[room] = Room.ff_profile(auth, room)
+ }
+ end
+ if list = ctx.list
+ tasks << Task.run {
+ @list_profiles[list] = List.ff_profile(auth, list)
+ }
+ end
+ yield
+ tasks.each do |task|
+ task.result # just pull the result
+ end
+ end
end
@@ -199,6 +199,18 @@ def auth
@auth
end
+ def user_profiles
+ @user_profiles
+ end
+
+ def room_profiles
+ @room_profiles
+ end
+
+ def list_profiles
+ @list_profiles
+ end
+
def service_icon(service, link = nil)
icon_url = service.icon_url
name = service.name
@@ -243,13 +255,8 @@ def list_name(nickname)
end
end
- def room_profiles(nicknames)
- nicknames.map { |nickname| room_profile(nickname) }
- end
-
def room_profile(nickname)
- @room_profile ||= {}
- @room_profile[nickname] ||= Room.ff_profile(auth, nickname)
+ room_profiles[nickname] ||= Room.ff_profile(auth, nickname)
end
def room_name(nickname)
@@ -266,10 +273,16 @@ def room_description(nickname)
end
def room_picture(nickname, size = 'small')
- name = room_name(nickname)
+ name = nickname
image_url = Room.ff_picture_url(nickname, size)
- url = room_profile(nickname)['url']
- link_to(profile_image_tag(image_url, name, name), url)
+ profile_image_tag(image_url, name, name)
+ end
+
+ def room_picture_with_link(nickname, size = 'small')
+ if image = room_picture(nickname, size)
+ url = room_profile(nickname)['url']
+ link_to(image, url)
+ end
end
def room_status_icon(nickname)
@@ -281,8 +294,7 @@ def room_members(nickname)
end
def list_profile(nickname)
- @list_profile ||= {}
- @list_profile[nickname] ||= List.ff_profile(auth, nickname)
+ list_profiles[nickname] ||= List.ff_profile(auth, nickname)
end
def list_rooms(nickname)
@@ -297,23 +309,8 @@ def imaginary?(nickname)
/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ =~ nickname
end
- def user_profiles(nicknames)
- @user_profile ||= {}
- if nicknames.any? { |e| @user_profile[e].nil? }
- User.ff_profiles(auth, nicknames).each do |profile|
- @user_profile[profile['nickname']] = profile
- end
- end
- nicknames.map { |nickname| @user_profile[nickname] }
- end
-
def user_profile(nickname)
- @user_profile ||= {}
- @user_profile[nickname] ||= User.ff_profile(auth, nickname)
- end
-
- def user_id(nickname)
- user_profile(nickname)['id']
+ user_profiles[nickname] ||= User.ff_profile(auth, nickname)
end
def user_name(nickname)
@@ -327,13 +324,19 @@ def user_status(nickname)
def user_picture(nickname, size = 'small')
return if imaginary?(nickname)
- name = user_name(nickname)
- if nickname == auth.name
+ name = nickname
+ if name == auth.name
name = self_label
end
image_url = User.ff_picture_url(nickname, size)
- url = user_profile(nickname)['profileUrl']
- link_to(profile_image_tag(image_url, name, name), url)
+ profile_image_tag(image_url, name, name)
+ end
+
+ def user_picture_with_link(nickname, size = 'small')
+ if image = user_picture(nickname, size)
+ url = user_profile(nickname)['profileUrl']
+ link_to(image, url)
+ end
end
def user_status_icon(nickname)
@@ -55,7 +55,7 @@ def cache_profile(entries)
rooms = []
# TODO: do not show status icon for query result page now for performance
# reason.
- unless ctx.query
+ if ctx.query.nil? and ctx.link.nil?
entries.each do |t|
t.entries.each do |e|
if e.room
@@ -138,7 +138,7 @@ def icon(entry)
end
# TODO: do not show status icon for query result page now for performance
# reason.
- if ctx.query.nil? and entry_status(entry) != 'public'
+ if ctx.query.nil? and ctx.link.nil? and entry_status(entry) != 'public'
str = icon_tag(:private) + str
end
str
View
@@ -145,6 +145,7 @@ def initialize(hash)
@twitter_reply_to = $1
end
end
+ @modified = nil
end
def similar?(rhs)
View
@@ -1,4 +1,4 @@
-require 'timeout'
+require 'task'
class EntryThread
@@ -8,37 +8,6 @@ class EntryThreads < Array
attr_accessor :pins
end
- class Task
- class << self
- def run(&block)
- new(&block)
- end
- private :new
- end
-
- attr_accessor :logger
-
- def initialize(logger = nil, &block)
- @logger = logger || ActiveRecord::Base.logger
- @result = nil
- @thread = Thread.new {
- @result = yield
- }
- end
-
- def result(timeout = F2P::Config.friendfeed_api_timeout)
- begin
- timeout(timeout) do
- @thread.join
- @result
- end
- rescue Timeout::Error => e
- logger.warn(e)
- @result = nil
- end
- end
- end
-
class << self
EMPTY = [].freeze
View
@@ -39,12 +39,6 @@ def ff_profile(auth, user)
convert_profile(ff_client.get_profile(auth.name, auth.remote_key, user) || {})
end
- def ff_profiles(auth, users)
- (ff_client.get_profiles(auth.name, auth.remote_key, users) || []).map { |profile|
- convert_profile(profile)
- }
- end
-
def ff_status_map(auth, users)
ff_client.get_user_status(auth.name, auth.remote_key, users)
end
@@ -1,5 +1,5 @@
<hr />
-<p><a name="profile"><%= user_picture(user) %><%= user_status_icon(user) %></a> <%= h(user_name(user)) %></p>
+<p><a name="profile"><%= user_picture_with_link(user) %><%= user_status_icon(user) %></a> <%= h(user_name(user)) %></p>
<p><%= user_page_links(ctx.user_for) %></p>
<p><%= service_links(user) %></p>
<p><%= user_links(user) %></p>
@@ -1,4 +1,4 @@
<hr />
-<p><a name="profile"><%= room_picture(room) %><%= room_status_icon(room) %></a> <%= h(room_name(room)) %></p>
+<p><a name="profile"><%= room_picture_with_link(room) %><%= room_status_icon(room) %></a> <%= h(room_name(room)) %></p>
<p><%= room_description(room) %></p>
<p><%= member_links(room) %></p>
View
@@ -64,7 +64,6 @@ class APIClientProxy
define_proxy_method :get_user_picture_url
define_proxy_method :get_room_picture_url
define_proxy_method :get_profile
- define_proxy_method :get_profiles
define_proxy_method :get_user_status
define_proxy_method :get_room_profile
define_proxy_method :get_list_profile
@@ -211,12 +210,11 @@ def clear_inbox
define_proxy_method :like
define_proxy_method :unlike
- define_cached_proxy_method :get_user_picture_url
- define_cached_proxy_method :get_room_picture_url
+ define_proxy_method :get_user_picture_url
+ define_proxy_method :get_room_picture_url
#define_cached_proxy_method :get_profile
- #define_cached_proxy_method :get_profiles
- #define_cached_proxy_method :get_room_profile
- #define_cached_proxy_method :get_list_profile
+ define_proxy_method :get_room_profile
+ define_proxy_method :get_list_profile
def initialize(logger = nil)
@client = FriendFeed::APIClient.new(logger)
@@ -247,33 +245,12 @@ def get_profile(name, remote_key, user = nil)
}
end
- def get_profiles(name, remote_key, users)
- users.collect { |user|
- get_profile(name, remote_key, user)
- }
- end
-
- def get_room_profile(name, remote_key, room)
- basekey = name
- cache = ((@cache ||= {})[basekey] ||= {})[:get_room_profile] ||= {}
- update_profile_cache(cache, room) {
- ClientProxy.proxy(@client, :get_room_profile, name, remote_key, room)
- }
- end
-
- def get_list_profile(name, remote_key, list)
- basekey = name
- cache = ((@cache ||= {})[basekey] ||= {})[:get_list_profile] ||= {}
- update_profile_cache(cache, list) {
- ClientProxy.proxy(@client, :get_list_profile, name, remote_key, list)
- }
- end
-
def get_user_status(name, remote_key, users)
basekey = "\0_system"
cache = ((@cache ||= {})[basekey] ||= {})[:user_status] ||= {}
users.inject({}) { |r, user|
r[user] = update_profile_cache(cache, user) {
+ @logger.info("updating user status cache for #{user}")
if profile = ClientProxy.proxy(@client, :get_profile, name, remote_key, user, :include => 'status')
profile['status']
end
@@ -287,6 +264,7 @@ def get_room_status(name, remote_key, rooms)
cache = ((@cache ||= {})[basekey] ||= {})[:room_status] ||= {}
rooms.inject({}) { |r, room|
r[room] = update_profile_cache(cache, room) {
+ @logger.info("updating room status cache for #{room}")
if profile = ClientProxy.proxy(@client, :get_room_profile, name, remote_key, room, :include => 'status')
profile['status']
end
View
@@ -0,0 +1,33 @@
+require 'timeout'
+
+
+class Task
+ class << self
+ def run(&block)
+ new(&block)
+ end
+ private :new
+ end
+
+ attr_accessor :logger
+
+ def initialize(logger = nil, &block)
+ @logger = logger || ActiveRecord::Base.logger
+ @result = nil
+ @thread = Thread.new {
+ @result = yield
+ }
+ end
+
+ def result(timeout = F2P::Config.friendfeed_api_timeout)
+ begin
+ timeout(timeout) do
+ @thread.join
+ @result
+ end
+ rescue Timeout::Error => e
+ logger.warn(e)
+ @result = nil
+ end
+ end
+end
@@ -6,7 +6,6 @@ def setup
@ff = mock('ff_client')
profile = read_profile('profile')
@ff.stubs(:get_profile).returns(profile)
- @ff.stubs(:get_profiles).returns([profile])
@ff.stubs(:get_user_status).returns({})
@ff.stubs(:get_room_status).returns({})
ApplicationController.ff_client = @ff
Oops, something went wrong.

0 comments on commit f98ce47

Please sign in to comment.