# Reload is using a cache and i dont want it to be

**URL:** <https://www.ruby-forum.com/t/reload-is-using-a-cache-and-i-dont-want-it-to-be/193083>\
**Category:** Rails\
**Created:** [August 10, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/reload-is-using-a-cache-and-i-dont-want-it-to-be/193083 "2010-08-10T22:01:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [August 10, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/reload-is-using-a-cache-and-i-dont-want-it-to-be/193083/1 "2010-08-10T22:01:36Z")

</div>

class PendingOrder \< ActiveRecord::Base  
def sleep\_until\_processed  
sleep 0.5  
while !processed?  
reload  
sleep 0.25  
end  
end  
end

Aug 10 19:43:38 prod-app1 foo[31608]: PendingOrder Load (0.6ms)  
SELECT \* FROM `pending_orders` LIMIT 1  
Aug 10 19:43:39 prod-app1 foo[31496]: CACHE (0.0ms) SELECT \* FROM  
`pending_orders` WHERE (`pending_orders`.`id` = 221)

So the first query is legit – but second query is cached, and so  
forth. Every subsequent query ends up being a cached value, forever.

How can I make the above method _not_ look at a cache?

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [August 11, 2010, 5:07pm UTC](https://www.ruby-forum.com/t/reload-is-using-a-cache-and-i-dont-want-it-to-be/193083/2 "2010-08-11T17:07:33Z")

</div>

Josh Sharpe wrote:

> class PendingOrder \< ActiveRecord::Base  
> def sleep\_until\_processed  
> sleep 0.5  
> while !processed?  
> reload  
> sleep 0.25  
> end  
> end  
> end
> 
> Aug 10 19:43:38 prod-app1 foo[31608]: PendingOrder Load (0.6ms)  
> SELECT \* FROM `pending_orders` LIMIT 1  
> Aug 10 19:43:39 prod-app1 foo[31496]: CACHE (0.0ms) SELECT \* FROM  
> `pending_orders` WHERE (`pending_orders`.`id` = 221)
> 
> So the first query is legit – but second query is cached, and so  
> forth. Every subsequent query ends up being a cached value, forever.
> 
> How can I make the above method _not_ look at a cache?

Not forever! Subsequent queries are cached until an update changes the  
underlying data. Isn’t it good to read from the cache if the underlying  
database hasn’t changed?

I am assuming that “processed?” reads from a column in your  
pending\_orders table. If something in your application changes that  
value then the query cache should be cleared automatically. However, if  
that database change were to happen outside of your Rails application I  
could see that your code could end up blocking forever.

In any case I would recommend adding some “circuit breaker” code to your  
method to prevent blocking forever. Maybe some sort of timeout/watchdog  
to limit the amount of time this method could block.

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [August 11, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/reload-is-using-a-cache-and-i-dont-want-it-to-be/193083/3 "2010-08-11T17:14:51Z")

</div>

Josh Sharpe wrote:

> class PendingOrder \< ActiveRecord::Base  
> def sleep\_until\_processed  
> sleep 0.5  
> while !processed?  
> reload  
> sleep 0.25  
> end  
> end  
> end

> Robert W. wrote:
> 
> > In any case I would recommend adding some “circuit breaker” code to your  
> > method to prevent blocking forever. Maybe some sort of timeout/watchdog  
> > to limit the amount of time this method could block.

And, if your Rails application is single treaded then your  
sleep\_until\_processed method could block your entire Rails application  
never allowing other parts of the application to perform the work  
necessary to update the pending\_orders table.

Maybe you should consider offloading this work to a background process  
using something like delayed\_job or similar.

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [August 11, 2010, 5:36pm UTC](https://www.ruby-forum.com/t/reload-is-using-a-cache-and-i-dont-want-it-to-be/193083/4 "2010-08-11T17:36:25Z")

</div>

Frederick C. wrote:

> On Aug 11, 11:07ï¿½am, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am assuming that “processed?” reads from a column in your  
> > pending\_orders table. If something in your application changes that  
> > value then the query cache should be cleared automatically. However, if  
> > that database change were to happen outside of your Rails application I  
> > could see that your code could end up blocking forever.
> 
> It’s more than that - the cache is in memory, so the cache won’t be  
> cleared if a different rails instances touches the data

I actually misstated that, sorry. I should have said anything outside  
the current Rails instance (including other Rails instances).

Thanks Frederick for the clarification. And also for the uncached  
solution. I wasn’t actually aware of that, but it’s definitely good to  
know.

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [August 11, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/reload-is-using-a-cache-and-i-dont-want-it-to-be/193083/5 "2010-08-11T17:20:39Z")

</div>

On Aug 11, 11:07 am, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am assuming that “processed?” reads from a column in your  
> pending\_orders table. If something in your application changes that  
> value then the query cache should be cleared automatically. However, if  
> that database change were to happen outside of your Rails application I  
> could see that your code could end up blocking forever.

It’s more than that - the cache is in memory, so the cache won’t be  
cleared if a different rails instances touches the data

Something like

PendingOrder.uncached do  
…  
end

should do the trick

Fred
