Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iguchi1124/counter_cache-rails
ActiveRecord child records counter caching on your rails application cache store.
https://github.com/iguchi1124/counter_cache-rails
Last synced: 17 days ago
JSON representation
ActiveRecord child records counter caching on your rails application cache store.
- Host: GitHub
- URL: https://github.com/iguchi1124/counter_cache-rails
- Owner: iguchi1124
- License: mit
- Created: 2016-09-13T17:20:22.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-04T03:47:45.000Z (over 6 years ago)
- Last Synced: 2024-10-05T08:17:47.319Z (about 1 month ago)
- Language: Ruby
- Homepage:
- Size: 43 KB
- Stars: 9
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# CounterCacheRails
ActiveRecord child records counter caching on your rails application cache store.
## Installation
Add this line to your rails application's Gemfile:
```ruby
gem 'counter_cache-rails'
```## Usage
```rb
class Post
has_many :commentscounter_cache :comments,
if: ->(comment) { comment.visible? },
scope: ->(comments) { comments.visible }after_update_comments_count do
# you can use callbacks on update counter cache
endafter_increment_comments_count do
Redis.current.increment('service:total:comments')
endafter_decrement_comments_count do
Redis.current.decrement('service:total:comments')
endafter_cache_comments_count do
update(comments_count: comments_count)
end
endclass Comment
belong_to :post
endpost = Post.create(title: 'sample title', body: 'post body')
post.comments_count # => 0post.comments.create(body: 'comment body')
post.comments_count # => 1post.comments_count(force: true) # force reload counter cache
```## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).