changeset 28:03f904c070f7

Basic caching support.
author edogawaconan <me@myconan.net>
date Sat, 06 Sep 2014 00:46:25 +0900
parents efc548be0a3a
children cf5c379d133f
files Gemfile Gemfile.lock README.md app/controllers/tweets_controller.rb app/models/tweet.rb config/application.rb
diffstat 6 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Gemfile	Sat Sep 06 00:08:18 2014 +0900
+++ b/Gemfile	Sat Sep 06 00:46:25 2014 +0900
@@ -22,6 +22,8 @@
 
 # Use debugger
 # gem 'debugger', group: [:development, :test]
+#
+gem "dalli"
 
 gem "twitter"
 gem "twitter-text"
--- a/Gemfile.lock	Sat Sep 06 00:08:18 2014 +0900
+++ b/Gemfile.lock	Sat Sep 06 00:46:25 2014 +0900
@@ -31,6 +31,7 @@
     arel (5.0.1.20140414130214)
     buftok (0.2.0)
     builder (3.2.2)
+    dalli (2.7.2)
     equalizer (0.0.9)
     erubis (2.7.0)
     faraday (0.9.0)
@@ -132,6 +133,7 @@
   x86-mingw32
 
 DEPENDENCIES
+  dalli
   puma
   rails (~> 4.1.4)
   sdoc (~> 0.4.0)
--- a/README.md	Sat Sep 06 00:08:18 2014 +0900
+++ b/README.md	Sat Sep 06 00:46:25 2014 +0900
@@ -26,12 +26,17 @@
 - `RT_CONSUMER_KEY`
 - `RT_CONSUMER_SECRET`
 
+Make sure to set them before using.
+
 Optionally, setting those two variables allows you access to private accounts which your account has access to:
 
 - `RT_ACCESS_TOKEN`
 - `RT_ACCESS_TOKEN_SECRET`
 
-Make sure to set them before using.
+It is also recommended to have memcached installed so tweet requests will be cached
+for a short while.
+
+- `RT_MEMCACHED_SERVERS` (separate multiple servers with comma)
 
 License
 -------
--- a/app/controllers/tweets_controller.rb	Sat Sep 06 00:08:18 2014 +0900
+++ b/app/controllers/tweets_controller.rb	Sat Sep 06 00:46:25 2014 +0900
@@ -1,6 +1,7 @@
 class TweetsController < ApplicationController
   def show
     @tweets = Tweet.new(params[:id]).timeline
+
     @user = @tweets.first.user
   rescue Twitter::Error::NotFound
     head :not_found
--- a/app/models/tweet.rb	Sat Sep 06 00:08:18 2014 +0900
+++ b/app/models/tweet.rb	Sat Sep 06 00:46:25 2014 +0900
@@ -7,7 +7,10 @@
   }
 
   def timeline
-    @timeline ||= @client.user_timeline(@twitter_id)
+    @timeline ||=
+      Rails.cache.fetch :timeline => @twitter_id, :expires_in => 5.minutes do
+        @client.user_timeline(@twitter_id)
+      end
   end
 
   def initialize(twitter_id)
--- a/config/application.rb	Sat Sep 06 00:08:18 2014 +0900
+++ b/config/application.rb	Sat Sep 06 00:46:25 2014 +0900
@@ -27,5 +27,9 @@
     # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
     # config.i18n.default_locale = :de
     config.middleware.delete "ActionDispatch::Cookies"
+
+    if ENV["RT_MEMCACHED_SERVERS"]
+      config.cache_store = :mem_cache_store, ENV["RT_MEMCACHED_SERVERS"].split(","), { :namespace => "rsstweet" }
+    end
   end
 end