<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>gfreezy_ (gfreezy)</title>
    <link>https://ruby-china.org/gfreezy_</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>CarrierWave 导致的性能问题</title>
      <description>&lt;p&gt;代码在： &lt;a href="http://pastebin.com/r7kzbr3t" rel="nofollow" target="_blank"&gt;http://pastebin.com/r7kzbr3t&lt;/a&gt;
访问日志：&lt;/p&gt;

&lt;p&gt;Started GET "/" for 127.0.0.1 at 2012-12-17 23:39:21 +0800
    Processing by TopicsController#index as HTML
      User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
      Topic Load (0.2ms)  SELECT "topics".* FROM "topics" LIMIT 12 OFFSET 0
      Item Load (0.2ms)  SELECT "items".* FROM "items" WHERE "items"."topic_id" = 2 ORDER BY item_votes_count desc LIMIT 9
       (0.2ms)  SELECT COUNT(*) FROM "topics" 
      Rendered topics/index.html.erb within layouts/application (2047.6ms)
    Completed 200 OK in 2068ms (Views: 2056.1ms | ActiveRecord: 0.7ms)&lt;/p&gt;

&lt;p&gt;View 有性能问题，用 Rack-mini-profiler 手动追踪，发现性能问题出在 CarrierWave 生成图片 URL 的地方，大家帮忙看看，怎么改或者怎么继续追踪下去？&lt;/p&gt;

&lt;p&gt;CarrierWave 的 storage 用的是七牛。&lt;/p&gt;</description>
      <author>gfreezy_</author>
      <pubDate>Mon, 17 Dec 2012 23:41:02 +0800</pubDate>
      <link>https://ruby-china.org/topics/7647</link>
      <guid>https://ruby-china.org/topics/7647</guid>
    </item>
    <item>
      <title>怎么用 Ruby 的方式 (Block) 来实现这个递归</title>
      <description>&lt;p&gt;def get_all_friends(uid, client)
      friends = client.friendships.friends(uid: uid)
      total = friends.total_number
      get_friends(total, 0, uid, client)
    end&lt;/p&gt;

&lt;p&gt;def get_friends(total, cursor, uid, client)
      left = total
      count = [200, left].min&lt;/p&gt;

&lt;p&gt;friends = client.friendships.friends(uid: uid, count: count, cursor: cursor)
      left -= count
      next_cursor = friends.next_cursor&lt;/p&gt;

&lt;p&gt;return friends if (left == 0 || next_cursor == 0)&lt;/p&gt;

&lt;p&gt;more_firends = get_friends(left, next_cursor, uid, client)
      more_firends.users &amp;lt;&amp;lt; friends.users
      return more_firends
    end&lt;/p&gt;

&lt;p&gt;get_all_friends(uid, observer.client).users.map do |info|
      user = User.find_or_create_by_uid_and_name(uid: info.id, name: info.name)
      follow(user)
      user
    end&lt;/p&gt;

&lt;p&gt;这段代码实现的是从微博获取所有关注（默认 API 只能根据 &lt;em&gt;cursor&lt;/em&gt; 来一页一页的获取）。但是接触 Ruby 不久，不会用 Ruby 的方式（ &lt;em&gt;block, do...end&lt;/em&gt; ）来实现。&lt;/p&gt;

&lt;p&gt;然后因为&lt;strong&gt;一次获取其他也是靠 cursor 和 count 来定位的东西&lt;/strong&gt;这种情况很普遍，可以通过将&lt;code&gt;client.friendships.friends(uid: uid, count: count, cursor: cursor)&lt;/code&gt;这个提取出来，以函数的方式传进去（函数式的想法）的方法来抽象。&lt;/p&gt;

&lt;p&gt;不知道 Ruby 的方式是怎么样来处理的？Ruby &lt;strong&gt;block&lt;/strong&gt;的思想转不过来。&lt;/p&gt;</description>
      <author>gfreezy_</author>
      <pubDate>Sat, 15 Dec 2012 22:21:10 +0800</pubDate>
      <link>https://ruby-china.org/topics/7596</link>
      <guid>https://ruby-china.org/topics/7596</guid>
    </item>
  </channel>
</rss>
