Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hook for ActiveRecord CRUD to automatically trigger a priming of the cache #29

Open
prpetten opened this issue Sep 25, 2020 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@prpetten
Copy link

prpetten commented Sep 25, 2020

class FruitLoops < ApplicationRecord

  after_create :prime_cache, type: :create, serializer: ToucanSam
  after_update :prime_cache, type: :update, serializer: ToucanSam
  after_destroy :prime_cache, type: :destroy, serializer: ToucanSam

  # or possibly

  after_commit :prime_cache, serializer: ToucanSam
   ...
end

This way you would be invalidating your cache and creating a new one on every CRUD action so that you'd nearly always be getting a cached result from the serializer.

In concert with this functionality, it would be nice to have a rake task to prime all of the caches initially.

@adamcrown
Copy link
Member

That's a good thought, but this could also be implemented as a simple method like this right?

def prime_cache
  ToucanSam.new(self).as_json
end

That being said, it also seems like it would be just as easy to add to the gem. If there is enough demand for it, I'd be happy to add it in or accept a PR.

@adamcrown adamcrown added enhancement New feature or request good first issue Good for newcomers labels Jun 12, 2021
@Nerian
Copy link

Nerian commented Apr 29, 2022

I was thinking on adding this as well. @prpetten Did you get to implement this? If not, I can take care of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants