# Can Rails cache a Controller "as long as code not changed"?

**URL:** <https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645>\
**Category:** Rails\
**Created:** [June 4, 2010, 12:31pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645 "2010-06-04T12:31:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 4, 2010, 12:31pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645/1 "2010-06-04T12:31:19Z")

</div>

At work, we have a situation where when

script/server

is run, then all the controller code is cached. This is to speed up the  
development server. But that will mean that whenever we change the  
controller code, we need to restart the server.

So we can turn off the caching of controller code all together. But  
can’t there be mechanism that is similar to the inclusion of javascript

foo.js?1273424325

which is to use the cached version as long as there is no code change,  
but recompile it when there is code change?

Maybe because we use HAML and SASS a lot, loading some page (such as the  
homepage of the site) can take 40 seconds on the dev environment and it  
is quite long.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 4, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645/2 "2010-06-04T16:23:41Z")

</div>

Jian L. wrote:

> At work, we have a situation where when
> 
> script/server
> 
> is run, then all the controller code is cached. This is to speed up the  
> development server. But that will mean that whenever we change the  
> controller code, we need to restart the server.

Yes, that’s normal behavior in development mode.

> So we can turn off the caching of controller code all together. But  
> can’t there be mechanism that is similar to the inclusion of javascript
> 
> foo.js?1273424325
> 
> which is to use the cached version as long as there is no code change,  
> but recompile it when there is code change?

Because it’s a different kind of caching. JavaScript caching simply  
involves using the browser cache for included files, whereas controller  
caching involves Ruby objects in memory on the server.

> Maybe because we use HAML and SASS a lot, loading some page (such as the  
> homepage of the site) can take 40 seconds on the dev environment and it  
> is quite long.

Haml and Sass shouldn’t be having that effect. Look elsewhere for your  
problems.

The fact that you’re asking this makes me think that you want _page_  
caching, not controller caching.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 4, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645/3 "2010-06-04T17:26:54Z")

</div>

On 4 June 2010 15:23, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jian L. wrote:
> 
> > At work, we have a situation where when
> > 
> > script/server
> > 
> > is run, then all the controller code is cached. This is to speed up the  
> > development server. But that will mean that whenever we change the  
> > controller code, we need to restart the server.
> 
> Yes, that’s normal behavior in development mode.

I thought that was the normal behaviour in production mode, not  
development.

Colin

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 5, 2010, 1:54pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645/4 "2010-06-05T13:54:07Z")

</div>

Marnen Laibow-Koser wrote:

> > foo.js?1273424325
> > 
> > which is to use the cached version as long as there is no code change,  
> > but recompile it when there is code change?
> 
> Because it’s a different kind of caching. JavaScript caching simply  
> involves using the browser cache for included files, whereas controller  
> caching involves Ruby objects in memory on the server.

I know that they are different kind of caching, – now can’t the same  
principle be used? When newer, reload / recompile it – when older, no  
need to reload or recompile.

so when the controller code in cache is newer, then no need to  
re-compile. when it finds that the controller code is newer, then  
recompile it.

by the way, what is this caching? I thought it is re-interpreted each  
time? So what is the caching for – it is not byte code like in Python?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 4, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645/5 "2010-06-04T17:47:30Z")

</div>

Colin L. wrote:

> On 4 June 2010 15:23, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Jian L. wrote:
> > 
> > > At work, we have a situation where when
> > > 
> > > ï¿½ script/server
> > > 
> > > is run, then all the controller code is cached. ï¿½This is to speed up the  
> > > development server. ï¿½But that will mean that whenever we change the  
> > > controller code, we need to restart the server.
> > 
> > Yes, that’s normal behavior in development mode.
> 
> I thought that was the normal behaviour in production mode, not  
> development.

Right you are. I didn’t write the word I meant to write. 😛

> Colin

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 5, 2010, 4:36pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-controller-as-long-as-code-not-changed/189645/6 "2010-06-05T16:36:35Z")

</div>

On Jun 5, 12:54 pm, Jian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I know that they are different kind of caching, – now can’t the same  
> principle be used? When newer, reload / recompile it – when older, no  
> need to reload or recompile.
> 
> so when the controller code in cache is newer, then no need to  
> re-compile. when it finds that the controller code is newer, then  
> recompile it.
> 
> by the way, what is this caching? I thought it is re-interpreted each  
> time? So what is the caching for – it is not byte code like in Python?

Caching is sort of the wrong word, because it implies that rails does  
something in order to achieve caching, whereas the opposite is true:  
rails’ code reloading is the extra behaviour.  
There have been attempts to make the code reloading stuff only reload  
what needs to be reloaded, see for example

> **[GitHub - thedarkone/rails-dev-boost: Speed up slow Rails development mode](https://github.com/thedarkone/rails-dev-boost)**
>
> Speed up slow Rails development mode. Contribute to thedarkone/rails-dev-boost development by creating an account on GitHub.

Fred
