Today, I ran across another form of this issue with Authlogic. In short, every request in a Rails application using Authlogic with a User model that includes a last_request_at column will cause the updated_at column to change. This can break caching that is based on the last time a user was updated.
last_request_at is one of Authlogic’s magic columns and is useful if you want to track users that might be logged in. However, by including this column, every controller request will touch the user record and change updated_at.
If you add last_request_at to your User model, you’ll start seeing this in your application logs:
To fix caching based on the User’s updated_at timestamp with Authlogic, you can add your own timestamp and maintain it whenever the User record changes. Here is what I came up with:
- Add a new column, user_updated_at, to the user model
- Whenever a relevant column changes on user, set user_updated_at to Time.now
- Change the cache control to be based on user_updated_at instead of updated_at
This made my controller request caching work as expected. How would you handle this?