Forum: Radiant CMS Caching and Application/Site Controllers

Posted by Stan Rawrysz (Guest)
on 2010-10-21 22:54
(Received via mailing list)
I've written an extension that needs to do some processing on every
call to a page in the cms. Basically, it checks for a cookie and
redirects the user if a certain value is present. I'm running into a
problem where that piece of code is not being called after the first
call to the app. I believe it's because of caching...

When i set cache_timeout to small, it works:
   SiteController.cache_timeout = 1.second

The extension is a SiteController extension:

  def activate
    Page.class_eval {
      SiteController.send :include,
IpRedirect::SiteControllerExt
    }
  end


The controller:

  module SiteControllerExt

    def self.included(base)
      base.class_eval do
        base.send(:include, InstanceMethods)
        before_filter :lookup_preferred_language_and_redirect
    end

    module InstanceMethods
      def lookup_preferred_language_and_redirect
          ....
      end


Is it possible that the before_filter is not called when caching is
turned on? Is there a way to make sure
that :lookup_preferred_language_and_redirect is called regardless of
caching?

I'm on 0.8.2 for this one.

Thanks in advance!
Stan
Posted by swartz (Guest)
on 2010-10-22 01:26
(Received via mailing list)
As far as I understand if there is a cached page already available the
radian sitecontroller doesn't even get executed. The server spits out
the generated page.

If you wish to have code executed everytime for every page access, you
pretty much have to disable caching.
If it's only afew specific pages that require this, you want to
declare your own page type and define the following:

def cache?
  false
end
Posted by Stan Rawrysz (Guest)
on 2010-10-22 04:39
(Received via mailing list)
So do you think it's possible to add the logic to a middleware object 
and
add the middleware object to the stack?

Put something like the following in the extension?

  extension_config do |config|
    config.middleware.use GeoIpRedirect
  end

I'm pretty new to middleware, so I'm not certain that this will get 
called
every time...

Stan
Posted by Josh French (jfrench)
on 2010-10-22 15:20
(Received via mailing list)
> So do you think it's possible to add the logic to a middleware object and add 
the middleware object to the stack?


Actually... wouldn't that work as long as it was inserted before 
Rack::Cache? Can someone more familiar with the caching layer 
confirm/deny?
Posted by Jim Gay (Guest)
on 2010-10-22 15:57
(Received via mailing list)
On Fri, Oct 22, 2010 at 9:18 AM, Josh French <josh@digitalpulp.com> 
wrote:
> So do you think it's possible to add the logic to a middleware object and
> add the middleware object to the stack?
>
> Actually... wouldn't that work as long as it was inserted before
> Rack::Cache? Can someone more familiar with the caching layer confirm/deny?

Yes. As long as it's injected before the cache, you're golden.
Posted by Stan Rawrysz (Guest)
on 2010-10-22 22:59
(Received via mailing list)
Awesome. These seems to have fixed it:

  extension_config do |config|
    if config.middleware.include? Radiant::Cache
      config.middleware.insert_before(Radiant::Cache, GeoIpRedirect)
    else
      config.middleware.use GeoIpRedirect
    end
  end

I'll reply back if my Qa team finds out it doesn't. :)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.