Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding cacheing to pages. adding cacheing to partials (can set:cache …

…=> false" or set expiry with :cache_expiry => ###).
  • Loading branch information...
commit db671157aa26037e83cfd938ead923dea6fcea74 1 parent 16c515d
Greg Leuch authored

Showing 2 changed files with 33 additions and 15 deletions. Show diff stats Hide diff stats

  1. +32 15 retweet.rb
  2. +1 0  settings.yml.default
47 retweet.rb
@@ -3,6 +3,8 @@
3 3 require 'twitter_oauth'
4 4 require 'configatron'
5 5 require 'haml'
  6 +require 'sinatra/memcache'
  7 +
6 8
7 9 configure do
8 10 %w(dm-core dm-types dm-aggregates dm-timestamps dm-ar-finders user tweet).each{ |lib| require lib }
@@ -13,6 +15,8 @@
13 15 DataMapper.setup(:default, configatron.db_connection.gsub(/ROOT/, ROOT))
14 16 DataMapper.auto_upgrade!
15 17
  18 + set :cache_enable, (configatron.enable_memcache && Sinatra::Application.environment.to_s == 'production')
  19 + set :cache_logging, false # causes problems if using w/ partials! :/
16 20 set :sessions, true
17 21 set :views, File.dirname(__FILE__) + '/views/'+ configatron.template_name
18 22 set :public, File.dirname(__FILE__) + '/public/'+ configatron.template_name
@@ -103,12 +107,20 @@ def launch_retweet_hell(msg=false)
103 107
104 108 def partial(name, options = {})
105 109 item_name, counter_name = name.to_sym, "#{name}_counter".to_sym
  110 + options = {:cache => true, :cache_expiry => 300}.merge(options)
  111 +
106 112 if collection = options.delete(:collection)
107 113 collection.enum_for(:each_with_index).collect{|item, index| partial(name, options.merge(:locals => { item_name => item, counter_name => index + 1 }))}.join
108 114 elsif object = options.delete(:object)
109 115 partial(name, options.merge(:locals => {item_name => object, counter_name => nil}))
110 116 else
111   - haml "_#{name}".to_sym, options.merge(:layout => false)
  117 + unless options[:cache].blank?
  118 + cache "_#{name}", :expiry => (options[:cache_expiry].blank? ? 300 : options[:cache_expiry]), :compress => false do
  119 + haml "_#{name}".to_sym, options.merge(:layout => false)
  120 + end
  121 + else
  122 + haml "_#{name}".to_sym, options.merge(:layout => false)
  123 + end
112 124 end
113 125 end
114 126
@@ -146,14 +158,16 @@ def user_profile_url(screen_name, at=true)
146 158 # Homepage
147 159 get '/' do
148 160 get_user unless session[:user].blank?
149   - haml (@user.blank? ? :home : :thanks)
  161 +
  162 + cache "homepage/#{@user.blank? ? 'guest' : 'user/#{@user.id}'}", :expiry => 600, :compress => true do
  163 + haml (@user.blank? ? :home : :thanks)
  164 + end
150 165 end
151 166
152 167
153 168 # Initiate the conversation with Twitter
154 169 get '/connect' do
155 170 @title = 'Connect to Twitter'
156   -
157 171 twitter_connect
158 172
159 173 begin
@@ -162,20 +176,22 @@ def user_profile_url(screen_name, at=true)
162 176 session[:request_token_secret] = request_token.secret
163 177 redirect request_token.authorize_url.gsub('authorize', 'authenticate')
164 178 rescue
165   - twitter_fail('An error has occured while trying to authenticate with Twitter. Please try again.')
  179 + cache 'error/connect', :expiry => 600, :compress => false do
  180 + twitter_fail('An error has occured while trying to authenticate with Twitter. Please try again.')
  181 + end
166 182 end
167 183 end
168 184
169 185
170 186 # Callback URL to return to after talking with Twitter
171 187 get '/auth' do
172   - @title = 'Authenticate with Twitter'
173   -
174   -
  188 + @title = 'Authenticate with Twitter'
175 189
176 190 unless params[:denied].blank?
177   - @error = "We are sorry that you decided to not use #{configatron.site_name}. <a href=\"/\">Click</a> to return."
178   - haml :fail
  191 + cache 'error/auth/denied', :expiry => 600, :compress => false do
  192 + @error = "We are sorry that you decided to not use #{configatron.site_name}. <a href=\"/\">Click</a> to return."
  193 + haml :fail
  194 + end
179 195 else
180 196 twitter_connect
181 197 @access_token = @twitter_client.authorize(session[:request_token], session[:request_token_secret], :oauth_verifier => params[:oauth_verifier])
@@ -205,7 +221,9 @@ def user_profile_url(screen_name, at=true)
205 221 @twitter_client.friend(name) unless dev?
206 222 end
207 223 rescue
208   - twitter_fail('An error has occured while trying to post a tweet to Twitter. Please try again.')
  224 + cache 'error/auth/fail', :expiry => 600, :compress => false do
  225 + twitter_fail('An error has occured while trying to post a tweet to Twitter. Please try again.')
  226 + end
209 227 end
210 228 end
211 229
@@ -216,20 +234,19 @@ def user_profile_url(screen_name, at=true)
216 234 # Launch retweet hell...
217 235 get '/run/*' do
218 236 @title = 'Launch Retweet Hell!'
219   - launch = params[:splat].to_s == configatron.secret_launch_code.to_s
220   -
221   - @error = '<strong>WTF!?</strong> You ain\'t got access to this. Fuck off.' unless launch
  237 + allowed, launch = params[:splat].to_s == configatron.secret_launch_code.to_s, true
222 238
  239 + @error = '<strong>WTF!?</strong> You ain\'t got access to this. Fuck off.' unless allowed
223 240
224 241 # Randomized retweet hell if running a cron job (recommended to use '*/1 * * * * curl -s http://example.com/run/----')
225   - if launch && configatron.randomize_hell && configatron.randomize_hell_freq.is_a?(Integer)
  242 + if allowed && configatron.randomize_hell && configatron.randomize_hell_freq.is_a?(Integer)
226 243 unless rand(configatron.randomize_hell_freq).round == 1
227 244 @error = "Waiting patiently for a truely randomized hell."
228 245 launch = false
229 246 end
230 247 end
231 248
232   - if launch
  249 + if launch && allowed
233 250 launch_retweet_hell
234 251 else
235 252 haml :fail
1  settings.yml.default
@@ -4,6 +4,7 @@ development: &local
4 4 group_website: http://www.fffff.at
5 5
6 6 template_name: default
  7 + enable_memcache: true
7 8
8 9 enable_retweet_game: true
9 10

0 comments on commit db67115

Please sign in to comment.
Something went wrong with that request. Please try again.