blog.sowatchasayin

Rails etc.

Rails cache by memory_store with 1-day expiration

Rails cache has no expiration by default.
So I save the time cached in the cache itself.

class ApplicationController < ActionController::Base
helper_method :count_something
def count_something
cache_of_today "count-of-something" do
Something.count
end
end
def cache_of_today(key, &block)
cached = Rails.cache.read key
if !cached || cached[:created_at] < 1.day.ago
Rails.cache.write key, :created_at => Time.now, :value => block.call
cached = Rails.cache.read key
end
cached[:value]
end
end

Sync crontab and Rails with Capistrano

1. Put `crontab -l` into a file and add it into your repository
2. Add lines below to your config/deploy.rb

after 'deploy' do
run "crontab -r && crontab #{File.join(current_path, 'cron')}"
end


that's all. crontab on production server will be refreshed after every deployment.

新しいの5個だけ残してあとは消す

Rubyで書こうとしましたが、間違いでした。
Unixすばらしい。

ls -t1|awk '{if(/f/ && nlines++ >= 5) print $1;}'|xargs rm

Author

Fujimura Daisuke
http://fujimuradaisuke.com

Labels