Rails 3 2 counter cache not updating

24-Apr-2019 14:32 by 6 Comments

Rails 3 2 counter cache not updating

Counting how many people saved a given product is therefore an important operation that we have to perform very often.

rails 3 2 counter cache not updating-81

A well known solution to this problem is provided by Rails in the form of counter cache feature in Active Record. Our traffic keeps growing and we start to notice occasional deadlocks in our database looking like this: What now?Redis has two operations that fit the requirement of counting perfectly; .These can be leveraged to buffer counts into Redis before saving them to the database, bypassing asking the database for counts.You are supposed to add the counter cache column, tell Rails what it is, and the rest is taken care of. We're now in a situation when the same rows are being updated at the same time or close to the same time.So we drop the configuration into the Save model and deploy with a migration that pre-fills all counter_cache values on users. We need to solve the locking or else the number of errors being thrown by update or delete endpoints will grow exponentially.This takes a significant load off our database especially when this is applied to all counters everywhere.

This process buys us another month until traffic reaches another threshold and we're runnning too many counts. We realize that we need to take a look at another datastore to hold these counts, even if for a temporary amount of time.

Please feel free to send pull-requests and report issues!

One of the possible future features could be providing read-time consistency by returning the sum of the buffered value and the database value, whenever the value needs to be displayed.

First up, create a new column in your parent table using migrations (You are using migrations right? Name the column something appropriate, I’ll be using in this case) to update our custom counter cache on each save or destroy.

Using after_save and after_destroy methods, we can do this every time and object is manipulated in any way that might affect our counter cache. Now everytime a comment is added, removed or modified, my custom counter cache will be updated.

The story is mostly perfect at this point except for the part where Redis goes away temporarily for any number of reasons (the cloud is not actually always there no matter how much you want it to be, turns out).

  1. No credit card required free webcam porn 15-Feb-2019 18:21

    But then, I'm just a 34 B, and I'm a sports hater. I grabbed her tight butt while I pulled a nipple with my teeth. " I didn't answer and instead worked her some more with the plug, then pulled it out and pushed it back in, over and over, until I could fuck her tight "latina ass" nicely with it. "That's how you like it," I hissed in her ear.

  2. cd singles dating service 05-Mar-2019 09:09

    I am not an earth nor an adjunct of an earth, I am the mate and companion of people, all just as immortal and fathomless as myself, (They do not know how immortal, but I know.) Every kind for itself and its own, for me mine male and female, For me those that have been boys and that love women, For me the man that is proud and feels how it stings to be slighted, For me the sweet-heart and the old maid, for me mothers and the mothers of mothers, For me lips that have smiled, eyes that have shed tears, For me children and the begetters of children. you are not guilty to me, nor stale nor discarded, I see through the broadcloth and gingham whether or no, And am around, tenacious, acquisitive, tireless, and cannot be shaken away.