active_record_multiple_query_cache 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00050eb28914caff20e2c4a00c6597a734effcf1
4
+ data.tar.gz: f7ccb81d7ab5034ee4d270c1eb1fca9162aa7271
5
+ SHA512:
6
+ metadata.gz: 85d1785a38917b157d5d94bda59446cab464750ca4f9fb818a47d0fe20bf6ed8ec3337c390aafc8582b459458a9b78aaf50948e1aa72b77f0e2990a7014c09f5
7
+ data.tar.gz: 4b64956e9d7995e7208625a257c645d1eb135d6cd333bc0b17cc9ca937b1faa2e6d21143975d3aa81d58753138a0ac8e3a4f720501a98262eedcd4abd8125634
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 alpaca-tc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # ActiveRecordMultipleQueryCache
2
+
3
+ Enable the query_cache for your abstract base class inherited from ActiveRecord::Base.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_record_multiple_query_cache'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ # your custom base class
17
+ class ApplicationSlaveRecord < ActiveRecord::Base
18
+ self.abstract_class = true
19
+ establish_connection(:"slave_#{Rails.env}")
20
+ end
21
+
22
+ # config/initializers/active_record_multiple_query_cache.rb
23
+ ActiveRecordMultipleQueryCache.install_query_cache('ApplicationSlaveRecord')
24
+ ```
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alpaca-tc/active_record_multiple_query_cache.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record_multiple_query_cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'active_record_multiple_query_cache'
8
+ spec.version = ActiveRecordMultipleQueryCache::VERSION
9
+ spec.authors = ['alpaca-tc']
10
+ spec.email = ['alpaca-tc@alpaca.tc']
11
+
12
+ spec.summary = %q{Enable the query_cache for your abstract base class inherited from ActiveRecord::Base}
13
+ spec.homepage = 'https://github.com/alpaca-tc/active_record_multiple_query_cache'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(spec)/})
18
+ end
19
+
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency('activerecord', '>= 4.2')
23
+
24
+ # for test
25
+ spec.add_development_dependency('sqlite3')
26
+ spec.add_development_dependency('rack')
27
+
28
+ spec.add_development_dependency('bundler')
29
+ spec.add_development_dependency('rake')
30
+ spec.add_development_dependency('rspec')
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord_multiple_query_cache"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ require 'active_record_multiple_query_cache/version'
2
+ require 'active_record_multiple_query_cache/rails4_query_cache'
3
+
4
+ module ActiveRecordMultipleQueryCache
5
+ autoload :Rails4QueryCache, 'active_record_multiple_query_cache/rails4_query_cache'
6
+ autoload :Rails5QueryCache, 'active_record_multiple_query_cache/rails5_query_cache'
7
+
8
+ def self.install_query_cache(activerecord_base_class_name, rails = Rails)
9
+ if rails.gem_version >= Gem::Version.new('5.0.0')
10
+ executor = ActiveSupport::Executor
11
+ query_cache = Rails5QueryCache.new(activerecord_base_class_name)
12
+
13
+ executor.register_hook(hook)
14
+ else
15
+ middleware = ActiveRecordMultipleQueryCache::Rails4QueryCache.new(activerecord_base_class_name)
16
+ rails.configuration.app_middleware.insert_after('::ActionDispatch::Callbacks', middleware)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ require 'rack'
2
+ require 'active_support/core_ext/string/inflections'
3
+
4
+ module ActiveRecordMultipleQueryCache
5
+ class Rails4QueryCache
6
+ def initialize(active_record_base_class)
7
+ active_record_base_class = active_record_base_class.constantize if active_record_base_class.is_a?(String)
8
+ @active_record_base_class = active_record_base_class
9
+ end
10
+
11
+ def new(app)
12
+ @app = app
13
+ self
14
+ end
15
+
16
+ def call(env)
17
+ connection = @active_record_base_class.connection
18
+ enabled = connection.query_cache_enabled
19
+ connection_id = @active_record_base_class.connection_id
20
+ connection.enable_query_cache!
21
+
22
+ response = @app.call(env)
23
+ response[2] = Rack::BodyProxy.new(response[2]) do
24
+ restore_query_cache_settings(connection_id, enabled)
25
+ end
26
+
27
+ response
28
+ rescue Exception
29
+ restore_query_cache_settings(connection_id, enabled)
30
+ raise
31
+ end
32
+
33
+ def name
34
+ self.class.name
35
+ end
36
+
37
+ def to_s
38
+ %{#<#{self.class} @active_record_class: "#{@active_record_base_class}">}
39
+ end
40
+
41
+ private
42
+
43
+ def restore_query_cache_settings(connection_id, enabled)
44
+ @active_record_base_class.connection_id = connection_id
45
+ @active_record_base_class.connection.clear_query_cache
46
+ @active_record_base_class.connection.disable_query_cache! unless enabled
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ module ActiveRecordMultipleQueryCache
2
+ class Rails5QueryCache
3
+ def initialize(active_record_base_class)
4
+ activerecord_base_class = active_record_base_class.constantize if active_record_base_class.is_a?(String)
5
+
6
+ @active_record_base_class = active_record_base_class
7
+ end
8
+
9
+ def run
10
+ caching_pool = @active_record_base_class.connection_pool
11
+ caching_was_enabled = caching_pool.query_cache_enabled
12
+
13
+ caching_pool.enable_query_cache!
14
+
15
+ [caching_pool, caching_was_enabled]
16
+ end
17
+
18
+ def complete((caching_pool, caching_was_enabled))
19
+ caching_pool.disable_query_cache! unless caching_was_enabled
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordMultipleQueryCache
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_multiple_query_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alpaca-tc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - alpaca-tc@alpaca.tc
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - active_record_multiple_query_cache.gemspec
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/active_record_multiple_query_cache.rb
115
+ - lib/active_record_multiple_query_cache/rails4_query_cache.rb
116
+ - lib/active_record_multiple_query_cache/rails5_query_cache.rb
117
+ - lib/active_record_multiple_query_cache/version.rb
118
+ homepage: https://github.com/alpaca-tc/active_record_multiple_query_cache
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.5.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Enable the query_cache for your abstract base class inherited from ActiveRecord::Base
142
+ test_files: []