model_cache 1.0.2

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: db36a572097605d795bfdee2ae701124240e8837
4
+ data.tar.gz: 45db56d922634d8ce0fa56394bbb11b0300b3147
5
+ SHA512:
6
+ metadata.gz: 83f1bf9da33c9deb41b600a047834f6f8a2dc8990cade6a258fd54c9a02c43ff9a6323526b651a1ec4686d3cfba3fff47d0a7cb47a6cf8c1190f198a0b75ae99
7
+ data.tar.gz: b34e3c0182873a8772bfbf7f7c3c8218972f12f77bf559013e180a9a75b7886bed11ccfa78cb0a5b387591cf1dda1e64b6073020910693f69d24d3ee010695f2
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Patrick Hogan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ModelCache'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1 @@
1
+ require "model_cache/railtie" if defined?(Rails)
@@ -0,0 +1,87 @@
1
+ module Cacheable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ after_commit do
6
+ self.class.updated_at = Time.now
7
+ end
8
+ end
9
+
10
+
11
+ def cache(cache_key, options = {}, &block)
12
+ cache_key = ActiveSupport::Cache.expand_cache_key (Array(cache_key) + [self, updated_at])
13
+ Rails.cache.fetch cache_key, options, &block
14
+ end
15
+
16
+
17
+ def method_missing(method, *arguments, &block)
18
+ if method.to_s =~ /^cached_(.*)$/
19
+ cache $1 do
20
+ value = send $1.to_sym
21
+ value = value.to_a if value.is_a? ActiveRecord::Relation
22
+ value
23
+ end
24
+ else
25
+ super
26
+ end
27
+ end
28
+
29
+
30
+ def self.respond_to?(method_sym, include_private = false)
31
+ if method.to_s =~ /^cached_(.*)$/
32
+ true
33
+ else
34
+ super
35
+ end
36
+ end
37
+
38
+
39
+ module ClassMethods
40
+
41
+ def updated_at(options = {})
42
+ value = Rails.cache.fetch "#{name}_updated_at"
43
+ if value.blank? || options[:force]
44
+ value = maximum :updated_at
45
+ Rails.cache.write "#{name}_updated_at", value
46
+ end
47
+ value
48
+ end
49
+
50
+
51
+ def updated_at=(value)
52
+ Rails.cache.write "#{name}_updated_at", value
53
+ end
54
+
55
+
56
+ def cache(cache_key, options = {}, &block)
57
+ cache_key = ActiveSupport::Cache.expand_cache_key (Array(cache_key) + [self, updated_at])
58
+ Rails.cache.fetch cache_key, options, &block
59
+ end
60
+
61
+
62
+ def method_missing(method, *arguments, &block)
63
+ if method.to_s =~ /^cached_(.*)$/
64
+ cache $1 do
65
+ value = send $1.to_sym
66
+ value = value.to_a if value.is_a? ActiveRecord::Relation
67
+ value
68
+ end
69
+ else
70
+ super
71
+ end
72
+ end
73
+
74
+
75
+ def self.respond_to?(method_sym, include_private = false)
76
+ if method.to_s =~ /^cached_(.*)$/
77
+ true
78
+ else
79
+ super
80
+ end
81
+ end
82
+
83
+ end
84
+ end
85
+
86
+
87
+ ActiveRecord::Base.send :include, Cacheable
@@ -0,0 +1,7 @@
1
+ module ModelCache
2
+ class Railtie < Rails::Railtie
3
+ config.after_initialize do
4
+ require "model_cache/cacheable"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module ModelCache
2
+ VERSION = "1.0.2"
3
+ end
4
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :model_cache do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Hogan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
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.0
27
+ description: An ActiveRecord extension that allows for caching based on latest updated_at
28
+ time.
29
+ email:
30
+ - pbhogan@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - lib/model_cache.rb
38
+ - lib/model_cache/cacheable.rb
39
+ - lib/model_cache/railtie.rb
40
+ - lib/model_cache/version.rb
41
+ - lib/tasks/model_cache_tasks.rake
42
+ homepage: https://github.com/pbhogan/model_cache
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.6.12
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: An ActiveRecord extension that allows for caching based on latest updated_at
66
+ time.
67
+ test_files: []