Category: Software

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.

Gnome 3 autocomplete crash, Filezilla drag&drop crash and shutdown without admin password

Since I'm using those hints from time to time, I've decided to put them here, so I don't need to google everytime I need them.

Gnome 3 autocomplete crash fix

Well to me honest, this isn't a fix but just a workaround, but hey! Wokrs for me ;)

sudo chattr +i ~/.local/share/recently-used.xbel

Filezilla drag&drop crash

sudo apt-get install curl
curl http://apt.wxwidgets.org/key.asc | sudo apt-key add -

Now let's add to /etc/apt/sources.list file:

deb http://apt.wxwidgets.org/ natty-wx main
deb-src http://apt.wxwidgets.org/ natty-wx main

and then:

sudo apt-get update
sudo apt-get install python-wxgtk2.8 python-wxtools wx2.8-i18n

Ubuntu shutdown without admin password

To do this, we just need to change the policy for shutting down. Edit the /usr/share/polkit-1/actions/org.freedesktop.consolekit.policy file and replace:

<action id="org.freedesktop.consolekit.system.stop-multiple-users">
  <description>Stop the system when multiple users are logged in</description>
  <message>System policy prevents stopping the system when other users are logged in</message>
  <defaults>
    <allow_inactive>no</allow_inactive>
    <allow_active>auth_admin_keep</allow_active>
  </defaults>
</action>

to:

<action id="org.freedesktop.consolekit.system.stop-multiple-users">
  <description>Stop the system when multiple users are logged in</description>
  <message>System policy prevents stopping the system when other users are logged in</message>
  <defaults>
    <allow_inactive>no</allow_inactive>
    <allow_active>yes</allow_active>
  </defaults>
</action>

Same for rebooting:

<action id="org.freedesktop.consolekit.system.restart-multiple-users">
  <description>Restart the system when multiple users are logged in</description>
  <message>System policy prevents restarting the system when other users are logged in</message>
  <defaults>
    <allow_inactive>no</allow_inactive>
    <allow_active>auth_admin_keep</allow_active>
  </defaults>
</action>

to:

<action id="org.freedesktop.consolekit.system.restart-multiple-users">
  <description>Restart the system when multiple users are logged in</description>
  <message>System policy prevents restarting the system when other users are logged in</message>
  <defaults>
    <allow_inactive>no</allow_inactive>
    <allow_active>yes</allow_active>
  </defaults>
</action>

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑