Tag: Ruby on Rails

Rails 3.2, Redis-store, views caching and expire_fragment with Regexp

In one of my projects, I have a VPS with low I/O, so I decided to move from disk cache to something else. Since I use some Regexps in expire_fragment method, I've decided to use Redis-store. It gives me exactly what I need:

  • Performance
  • Persistence
  • I already know Redis ;)
  • I use Redis in the same project for other purpose
  • "Almost" working Regexp support

expire_fragment with Regexp why aren't you working?

After view minutes with Redis-store source code, I've figured out why ;) Well, it uses native Redis KEYS method to get all the matching keys and it expires them. Unfortunately KEYS don't support Regexp matching :( Instead it works with wild-cards matching and the same goes for Redis-store.

Quick fix

Ok, this isn't so bad. My Regexps are relatively simple and there's not to much of them, so converting them should not be a problem. Most of the time I expire fragments outside controllers, so I've created an additional layer in the expire process (read more about this issue). We just need to map all the Regexps into an "Redis acceptable" form. As I mentioned above, my Regexps are simple, so mapping them was really easy (few examples):

/announcements-index/ => "*announcements-index*"
/weekly-topics-index/ => "*weekly-topics-index*"

Ruby code for such conversions looks like this:

fragment = "*#{fragment.to_s.split(':').last.gsub(')', '')}*"

this solution works for simple Regexps and it works for me. Unfortunately this isn't the only issue with Redis-store. I've overwritten the expire_fragment method for my layer:

  def expire_fragment(fragment, options = nil)
    if Rails.configuration.cache_store == :redis_store
      if fragment.is_a?(Regexp)
        fragment = "*#{fragment.to_s.split(':').last.gsub(')', '')}*"
      end
    end
    super
  end

But still only direct cache hits expire would work.

cache_store.delete_matched doesn't work?

Expire_fragment method under ActionController::Caching looks like this:

def expire_fragment(key, options = nil)
  return unless cache_configured?
  key = fragment_cache_key(key) unless key.is_a?(Regexp)

  instrument_fragment_cache :expire_fragment, key do
    if key.is_a?(Regexp)
      cache_store.delete_matched(key, options)
    else
      cache_store.delete(key, options)
    end
  end
end

So, as you can see, the delete_matched method is invoked only when we pass a Regexp. But hey! we never pass one :( we pass a string with a wild-card and it tries to expire it using the delete method. Luckily patching this is really simple:

module ActiveSupport
  module Cache
    class RedisStore < Store
      def delete(key, options)
        delete_matched(key, options)
      end
    end
  end
end

And that's all :) After applying both presented here solutions, Redis-store should work with simple Regexps without any problems.

RVM, Ruby 1.9.3-p194, ruby-debugger and “You need to install ruby-debug” on Ubuntu 11.10

Recently I've been upgrading my RVM and Ruby versions. After upgrade I've encountered a problem connected to ruby-debugger. When starting Rails server I always ended with such a message:

You need to install ruby-debug to run the server in debugging mode. 
With gems, use 'gem install ruby-debug'

# Edit: this solutions fixes also this problem:

cannot load such file -- zlib

Ruby-debbug doesn't work to well with Ruby 1.9, so I use following combination in my gemsets:

group :development, :test do
  gem 'linecache19', :git => 'git://github.com/mark-moseley/linecache'
  gem 'ruby-debug-base19x', '~> 0.11.30.pre4'
  gem 'ruby-debug19'
end

Until now, it worked really well, but after the upgrade, the "You need to install ruby-debug" message kept showing again and again (even when the gems from list above were installed). To fix this issue, you need to run following commands:

rm -rf $rvm_path/usr
rvm pkg install zlib

# Posted in 5 lines instead of 1 for better visibility
sudo apt-get install build-essential openssl libreadline6 libreadline6-dev 
sudo apt-get install curl git-core zlib1g zlib1g-dev libssl-dev
sudo apt-get install libyaml-dev  libsqlite3-0 libsqlite3-dev sqlite3
sudo apt-get install libxml2-dev  libxslt-dev autoconf libc6-dev 
sudo apt-get install ncurses-dev automake libtool bison subversion

rvm reinstall 1.9.3-p194

After successful Ruby version reinstall, you should be able to run ruby software with debugger enabled.

# Update
Looks like, you can (as guys suggested in comments) debugger instead of ruby-debugger, however the method above fixes also problem with:

cannot load such file -- zlib

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑