# Caching and Application/Site Controllers

**URL:** <https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881>\
**Category:** Radiant CMS\
**Created:** [October 21, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881 "2010-10-21T22:54:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 21, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881/1 "2010-10-21T22:54:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stan\_Rawrysz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@Stan\_Rawrysz](https://www.ruby-forum.com/u/Stan_Rawrysz)\
**Post date:** [October 22, 2010, 1:26am UTC](https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881/2 "2010-10-22T01:26:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stan\_Rawrysz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@Stan\_Rawrysz](https://www.ruby-forum.com/u/Stan_Rawrysz)\
**Post date:** [October 22, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881/3 "2010-10-22T15:20:02Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![Stan\_Rawrysz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@Stan\_Rawrysz](https://www.ruby-forum.com/u/Stan_Rawrysz)\
**Post date:** [October 22, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881/4 "2010-10-22T15:57:57Z")

</div>

On Fri, Oct 22, 2010 at 9:18 AM, Josh F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![Stan\_Rawrysz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@Stan\_Rawrysz](https://www.ruby-forum.com/u/Stan_Rawrysz)\
**Post date:** [October 22, 2010, 10:59pm UTC](https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881/5 "2010-10-22T22:59:29Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![Stan\_Rawrysz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@Stan\_Rawrysz](https://www.ruby-forum.com/u/Stan_Rawrysz)\
**Post date:** [October 22, 2010, 4:39am UTC](https://www.ruby-forum.com/t/caching-and-application-site-controllers/197881/6 "2010-10-22T04:39:39Z")

</div>

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
