volatile_counter_cache 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +27 -0
- data/Rakefile +2 -0
- data/lib/volatile_counter_cache/version.rb +3 -0
- data/lib/volatile_counter_cache.rb +46 -0
- data/volatile_counter_cache.gemspec +21 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8269703b195a013b43674ac19dd4a7bd7e659a87
|
4
|
+
data.tar.gz: 65704e49e6dad06b31f4cc4ce704938fb570e407
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e6dcf87c79f3f26817f6377154cb620c6754fa5d42e6aa7dc5da99a995828e3b8b044a935ced5c86e21f4e79058bdefe17d4236221d8c9cff1c124dc208961b
|
7
|
+
data.tar.gz: 0109f3d4c1b22b1d9e519769e733299773c04d01d415c0b90a04f5638f9393c34826ef0e34d78dba2c635830b8dcc4802e6d610e764e2357762ca168fb93fa3c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ryo Nakamura
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# VolatileCounterCache
|
2
|
+
Provides volatile counter cache logic to ActiveRecord::Base.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
1. `favorites_count` tries to read counter cache from Rails.cache.
|
6
|
+
2. If there is no cache yet, it calls `favorites.count` and store the result.
|
7
|
+
3. When `favorite` is created or destroyed, the cache is purged.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class Tweet < ActiveRecord::Base
|
11
|
+
include VolatileCounterCache
|
12
|
+
|
13
|
+
has_many :favorites
|
14
|
+
|
15
|
+
volatile_counter_cache :favorites, cache: Rails.cache
|
16
|
+
end
|
17
|
+
|
18
|
+
Tweet.first.favorites_count #=> 42
|
19
|
+
Tweet.first.favorites.first.destroy
|
20
|
+
Tweet.first.favorites_count #=> 41
|
21
|
+
```
|
22
|
+
|
23
|
+
### options
|
24
|
+
- cache [ActiveSupport::Cache::Store] A store object to store count for each key.
|
25
|
+
- cache_options [Hash] Options for cache store.
|
26
|
+
- counter_method_name [String] An optional String to change counter method name.
|
27
|
+
- foreign_key [Symbol] An optional Symbol to change cache key at callback.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "volatile_counter_cache/version"
|
2
|
+
|
3
|
+
# A model concern module to provide volatile counter cache logic to model class.
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# class Tweet < ActiveRecord::Base
|
7
|
+
# include VolatileCounterCache
|
8
|
+
# has_many :favorites
|
9
|
+
# volatile_counter_cache :favorites, cache: Rails.cache
|
10
|
+
# end
|
11
|
+
# Tweet.first.favorites_count #=> 42
|
12
|
+
# Tweet.first.favorites.first.destroy
|
13
|
+
# Tweet.first.favorites_count #=> 41
|
14
|
+
#
|
15
|
+
module VolatileCounterCache
|
16
|
+
CACHE_KEY_PREFIX = 'volatile-counter-cache'
|
17
|
+
|
18
|
+
extend ActiveSupport::Concern
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
# Define counter method and callback using given cache store for 1:N association.
|
22
|
+
# @param association_name [Symbol] Association name used to counter method, cache key, and counting.
|
23
|
+
# @param cache [ActiveSupport::Cache::Store] A store object to store count for each key.
|
24
|
+
# @param cache_options [Hash] Options for cache store.
|
25
|
+
# @param counter_method_name [String] An optional String to change counter method name.
|
26
|
+
# @param foreign_key [Symbol] An optional Symbol to change cache key at callback.
|
27
|
+
def volatile_counter_cache(association_name, cache:, cache_options: {}, counter_method_name: nil, foreign_key: nil)
|
28
|
+
counter_method_name = counter_method_name || "#{association_name}_count"
|
29
|
+
cache_key_prefix = "#{CACHE_KEY_PREFIX}/#{model_name}/#{association_name}"
|
30
|
+
|
31
|
+
define_method(counter_method_name) do
|
32
|
+
cache.fetch("#{cache_key_prefix}/#{id}", cache_options) do
|
33
|
+
send(association_name).count
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
foreign_key = foreign_key || "#{table_name.singularize }_id"
|
38
|
+
child_class = association_name.to_s.singularize.camelize.constantize
|
39
|
+
callback_proc = -> do
|
40
|
+
cache.delete("#{cache_key_prefix}/#{send(foreign_key)}", cache_options)
|
41
|
+
end
|
42
|
+
child_class.after_create(&callback_proc)
|
43
|
+
child_class.after_destroy(&callback_proc)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "volatile_counter_cache/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "volatile_counter_cache"
|
7
|
+
spec.version = VolatileCounterCache::VERSION
|
8
|
+
spec.authors = ["Ryo Nakamura"]
|
9
|
+
spec.email = ["r7kamura@gmail.com"]
|
10
|
+
spec.summary = "Provides volatile counter cache logic to ActiveRecord::Base."
|
11
|
+
spec.homepage = "https://github.com/r7kamura/volatile_counter_cache"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: volatile_counter_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryo Nakamura
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- r7kamura@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/volatile_counter_cache.rb
|
54
|
+
- lib/volatile_counter_cache/version.rb
|
55
|
+
- volatile_counter_cache.gemspec
|
56
|
+
homepage: https://github.com/r7kamura/volatile_counter_cache
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.2.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Provides volatile counter cache logic to ActiveRecord::Base.
|
80
|
+
test_files: []
|
81
|
+
has_rdoc:
|