Cache JSON instead of ActiveRecord objects in `Appearance` and `ApplicationSetting`
As suggested in https://gitlab.com/gitlab-org/gitlab-ee/issues/5571#note_66907691, there are a few gotchas with caching ActiveRecord objects in Redis:
- Attributes are not defined when loading an ActiveRecord object from cache:
https://github.com/rails/rails/issues/27348
- Serialization isn't compatible from one Rails version to another: (see
Avoid storing serialized data in database or cache
in https://shopifyengineering.myshopify.com/blogs/engineering/upgrading-shopify-to-rails-5-0) - When the schema is updated, the cache needs to be cleared (this can be solved by adding a version/timestamp in the cache key, as done here: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18211)
- We're caching information that we don't care about
Proposal
Cache attributes we care about as JSON, and instantiate a new ActiveRecord object and set its attributes from the ones retrieved from cache (and merge it into the defaults).
I've made an attempt in https://gitlab.com/gitlab-org/gitlab-ee/issues/5571#note_66985230 and this seemed to work well.
/cc @stanhu