Rails caching with expires_in lambda

When expires_in gets complicated you can simply use
:expires_in => lambda{ Time.now.to_i % 100 }

Code

class ActionController::Base
  def write_fragment_with_lambda_expire(key, content, options = nil)
    if options.is_a? Hash and options[:expires_in].is_a? Proc
      options = options.merge(:expires_inย  => options[:expires_in].call)
    end
    write_fragment_without_lambda_expire(key, content, options)
  end

  alias_method_chain :write_fragment, :lambda_expire
end

Finding the oldest element in memcached

We always wanted to know how full memcached is, and therefore know at which age an element is dropped. This hacky script will find it out, by inserting 30 values each day and taking out 30 untouched values from 30 previous days <-> if one is missing, thats how old your oldest element is.
(if you know a better way, let me know ๐Ÿ˜‰ )

Usage
Run one time each day (via cron) and store output into a logfile after 26 days:

rake check_memcached_age
Stats for 2009-12-01:
0: still there...
1: still there...
2: still there...
3: still there...
...
23: still there...
24: still there...
25: 
26: 
...

Your cache is 24 days old!

Script

task :check_memcached_age => :environment do
  cache = ActionController::Base.cache_store
  # insert probes for today
  (0..30).each do |i|
    cache.write "memcached-probe-#{Date.today.to_s(:db)}--#{i}", 'still there...'
  end

  # extract old probes
  results = (0..30).to_a.map do |i|
    [i, cache.read("memcached-probe-#{(Date.today-i.days).to_s(:db)}--#{i}")]
  end

  puts "stats for #{Date.today.to_s(:db)}"
  puts results.map{|day, present| "#{day}: #{present}"} * "\n" ย 
end

ActiveRecord.touch_without_callbacks

When updating to 2.3.4 I noticed that touch is
no longer a simple ‘update :updated_at’, but a save!,
which caused some code to break
(e.g. touch`ing in after_save == loop)

Usage

User.first.touch_without_callbacks
User.touch_without_callbacks([User.first.id, User.last.id])

Install
Paste somewhere…

# Provide .touch as it was in 2.3.2, simply update the :updated_at field.
class ActiveRecord::Base
  def touch_without_callbacks
    now = Time.now.utc
    self.class.touch_without_callbacks(id, now)
    self.updated_at = now
  end

  def self.touch_without_callbacks(ids, time=Time.now.utc)
    update_all({:updated_at=>time}, :id=>ids)
  end
end

What are your git-stats?

Also available as Gist

Ever wondered how much who adds/removes, its time to find out ๐Ÿ˜€
(those are real stats, I just obfuscated the names ๐Ÿ˜‰ )

Results

Git scores (in LOC):
mr-add              :  +482273       -9466       
justu               :  +286250       -159905     
grosser             :  +152384       -323344     
another             :  +121257       -82116      
naames              :  +104577       -13591      
justfor             :  +68716        -72446      
example             :  +7795         -4987       
andeven             :  +5100         -1730       
morenow             :  +4225         -2764       
finish              :  +17           -19       

Update: After working 2.5 years on this project my final stats where: +861345 -1115685 = -254340 LOC ๐Ÿ˜€

Install
Copy init git_stats.rb and ruby git_stats.rb
(you can add the names of people who commit with different users into the ‘same’ array)

#!/usr/bin/env ruby
# please add enhancements to http://gist.github.com/234560
require 'optparse'
start = Time.now.to_i
sort_by = '+'
STDOUT.sync = true

same = [
  ['my name','my-name']
]

# parse options
options = {}
OptionParser.new do |opts|
  opts.banner = <<BANNER
Show git stats, options are:
BANNER
  opts.on("--limit [LIMIT]", Integer, "Limit"){|x| options[:limit] = x }
  opts.on("--max-lines [SIZE]", Integer, "Max lines per commit  ignore large commits"){|x| options[:max_lines] = x }
  opts.on("--help", "Show help"){ puts self; exit }
end.parse!

# parse commits
pipe = open('|git log --pretty=format:"A:%an" --shortstat --no-merges')
authors = ["unknown"]
stats = {}

count = 0
loop do
  count += 1
  break if options[:limit] and count/2 > options[:limit] # break if limit was reached (2 lines per commit)
  line = pipe.readline rescue break

  if line =~ /^A\:(.*?)$/
    authors = $1.strip
    found = same.detect{|a| a.include?(authors)}
    authors = found.first if found
    authors = authors.split('&').map(&:strip) # split up multiple people
    next
  end

  if line =~ /files changed, (\d+) insertions\(\+\), (\d+) deletions/
    add = $1.to_i
    remove = $2.to_i
    if options[:max_lines] and (add + remove) > options[:max_lines]
      print 'x'
    else
      authors.each do |author|
        stats[author] ||= Hash.new(0)
        stats[author]['+'] += add
        stats[author]['-'] += remove
      end
      print '.'
    end
  end
end

longest = stats.map{|a,d|a.length}.max
stats = stats.sort_by{|a,d| -d[sort_by] }
stats = stats.map do |author, data|
  "#{author.ljust(longest)}:  +#{data['+'].to_s.ljust(10)}   -#{data['-'].to_s.ljust(10)} "
end

puts "\nGit scores (in LOC):"
puts stats.join("\n")
puts "Took #{Time.now.to_i - start} seconds"