active_record_caching 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d77e9b56e028154a8c8b133b730984b879e01500
4
+ data.tar.gz: 186f290de41407edc2ae363a4b50f1d601143981
5
+ SHA512:
6
+ metadata.gz: e38a1816a20a6e99d280fe83189ec7f4f642c8c536d9f0a4043afb5c4c677d7ecb4aaf7eb28b34d522dd8abf21e9069b610436c2cea42ea53d9b149e87d0703b
7
+ data.tar.gz: 42bf3e15c8e02db3f3229ba691d613a998ad7d3b678bf6535078e770d3d34c1ce79d6fbfa00cf3f25476b58ed8cbed585be6fea3b9e519da94496c9f37f0b02c
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Derek Lindahl
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.
@@ -0,0 +1,45 @@
1
+ # ActiveRecord Caching
2
+
3
+ Adds "Advanced Caching" to ActiveRecord::Base as described in Adam Hawkins'
4
+ blog, [Fast JSON APIs](http://broadcastingadam.com/2012/07/advanced_caching_part_6-fast_json_apis/).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'active_record_caching'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install active_record_caching
19
+
20
+ ## Usage
21
+
22
+ Index controller actions will support `#stale?`
23
+
24
+ ```ruby
25
+ class ResourceController < ApplicationController
26
+ responds_to :json
27
+
28
+ def index
29
+ # uses the new #cache_key method defined on ActiveRecord::Base to
30
+ # set the etag
31
+ if stale? collection do
32
+ # Use cached JSON from individual hashes to render a collection
33
+ respond_with collection
34
+ end
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/./fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
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_caching/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'active_record_caching'
8
+ spec.version = ActiveRecordCaching::VERSION
9
+ spec.authors = ['Derek Lindahl']
10
+ spec.email = ['dlindahl@customink.com']
11
+ spec.summary = %q{Additonal caching logic for ActiveRecord for fast JSON APIs}
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/dlindahl/active_record_caching'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record_caching/version'
2
+ require 'active_record_caching/errors'
3
+ require 'active_record_caching/cache_key'
4
+ require 'active_record_caching/cache_serializing'
5
+ require 'active_record_caching/railtie'
6
+
7
+ module ActiveRecordCaching
8
+ end
@@ -0,0 +1,35 @@
1
+ # Adds a #cache_key method to ActiveRecord::Base so that cache_digest has
2
+ # something meaningful to use when caching collections (ActiveRecord::Relation)
3
+ #
4
+ # From http://broadcastingadam.com/2012/07/advanced_caching_part_6-fast_json_apis/
5
+ module ActiveRecordCaching
6
+ module CacheKey
7
+ module Rails3
8
+ def cache_key
9
+ Digest::MD5.hexdigest(raw_cache_key)
10
+ end
11
+
12
+ def updated_at
13
+ scoped.maximum(:updated_at)
14
+ end
15
+
16
+ def raw_cache_key
17
+ "#{scoped.to_sql}-#{updated_at.try(:to_i)}-#{scoped.count}"
18
+ end
19
+ end
20
+
21
+ module Rails4
22
+ def cache_key
23
+ Digest::MD5.hexdigest(raw_cache_key)
24
+ end
25
+
26
+ def updated_at
27
+ all.maximum(:updated_at)
28
+ end
29
+
30
+ def raw_cache_key
31
+ "#{all.to_sql}-#{updated_at.try(:to_i)}-#{all.count}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ module ActiveRecordCaching
2
+ module CacheSerializing
3
+ def perform_caching?
4
+ valid_representation? && super
5
+ end
6
+
7
+ def cache_key
8
+ if defined?(super)
9
+ super
10
+ elsif object.respond_to?(:cache_key)
11
+ object.cache_key
12
+ else
13
+ raise NoMethodError, "undefined method `cache_key' for #{object}"
14
+ end
15
+ end
16
+
17
+ def respond_to?(method_name, *args)
18
+ return super if method_name != :cache_key
19
+
20
+ # If the serializer explicitly defines :cache_key or the object itself
21
+ # defines it, then we can assume that :cache_key is supported
22
+ method(:cache_key).owner != CacheSerializing || object.respond_to?(:cache_key)
23
+ end
24
+
25
+ private
26
+
27
+ # Allows easy determination about whether or not to read from the cache
28
+ # based on the serialized object's state of validations.
29
+ def valid_representation?
30
+ return true unless object.respond_to? :errors
31
+
32
+ object.errors.blank?
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveRecordCaching
2
+ class CacheKeyExists < StandardError
3
+ def message
4
+ "ActiveRecord::Base.cache_key exists" \
5
+ "\n\n" \
6
+ "The ActiveRecord::Base.cache_key monkey patch is no longer required." \
7
+ "\n" \
8
+ "ActiveRecord::Base already responds to .cache_key" \
9
+ "\n\n"
10
+ end
11
+ end
12
+
13
+ class VersionCheckRequired < StandardError
14
+ def message
15
+ "ActiveModel::Serializer implementation check required."\
16
+ "\n\n" \
17
+ "There were some discrepancies found in regards to serialization\n" \
18
+ "caching with ActiveModel::Serializer > 0.8.1.\n\n" \
19
+ "Since it appears that ASM has been upgraded, you must double\n" \
20
+ "check that ASM does indeed support caching and ensure that the\n" \
21
+ "patches in CacheSerializing are still needed and compatible.\n\n" \
22
+ "For more information:\n\n" \
23
+ " * https://github.com/rails-api/active_model_serializers/issues/467#issuecomment-30388385\n" \
24
+ " * https://github.com/rails-api/active_model_serializers/issues/478#issuecomment-31367143\n" \
25
+ "\n\n"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveRecordCaching
2
+ class Railtie < Rails::Railtie
3
+ config.before_initialize do |app|
4
+ ActiveSupport.on_load(:active_record) do
5
+ if ActiveRecord::Base.respond_to? :cache_key
6
+ raise CacheKeyExists
7
+ else
8
+ case Rails.version[0]
9
+ when '3' then ActiveRecord::Base.extend(ActiveRecordCaching::CacheKey::Rails3)
10
+ when '4' then ActiveRecord::Base.extend(ActiveRecordCaching::CacheKey::Rails4)
11
+ else
12
+ raise 'unsupported'
13
+ end
14
+ end
15
+ end
16
+
17
+ ActiveSupport.on_load(:active_model_serializers) do
18
+ require 'active_model/serializers/version'
19
+ if ActiveModel::Serializer::VERSION != '0.8.1'
20
+ raise VersionCheckRequired
21
+ end
22
+
23
+ ActiveModel::ArraySerializer.send :prepend, CacheSerializing
24
+ ActiveModel::Serializer.send :prepend, CacheSerializing
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordCaching
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_caching
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Derek Lindahl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-13 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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
+ description: Additonal caching logic for ActiveRecord for fast JSON APIs
42
+ email:
43
+ - dlindahl@customink.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - active_record_caching.gemspec
54
+ - lib/active_record_caching.rb
55
+ - lib/active_record_caching/cache_key.rb
56
+ - lib/active_record_caching/cache_serializing.rb
57
+ - lib/active_record_caching/errors.rb
58
+ - lib/active_record_caching/railtie.rb
59
+ - lib/active_record_caching/version.rb
60
+ homepage: https://github.com/dlindahl/active_record_caching
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.0
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Additonal caching logic for ActiveRecord for fast JSON APIs
84
+ test_files: []