activerecord_cache 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,5 +17,5 @@ Gem::Specification.new do |s|
17
17
 
18
18
  s.licenses = ['MIT']
19
19
 
20
- s.add_dependency 'rails', ['~> 3.1.3']
20
+ s.add_dependency 'rails', ['>= 3.1.3', '<= 3.3.0']
21
21
  end
@@ -1,31 +1,31 @@
1
1
  module ActiveRecordCache
2
2
  module Base
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  included do
6
6
  class_attribute :use_activerecord_cache, :instance_writer => false
7
7
  self.use_activerecord_cache = false
8
-
8
+
9
9
  after_save :write_to_cache, :if => lambda { self.class.use_activerecord_cache }
10
10
  after_destroy :delete_from_cache, :if => lambda { self.class.use_activerecord_cache }
11
11
  end
12
-
12
+
13
13
  module ClassMethods
14
14
  def cache_key(id_or_record)
15
15
  unless use_activerecord_cache
16
16
  message = "ActiveRecord cache is not enabled for #{self.name}"
17
17
  raise ActiveRecordCache::CachingNotEnabled, message
18
18
  end
19
-
19
+
20
20
  if id_or_record.is_a?(ActiveRecord::Base)
21
21
  id = id_or_record[id_or_record.class.primary_key]
22
22
  else
23
23
  id = id_or_record
24
24
  end
25
-
25
+
26
26
  "#{model_name}/#{id}"
27
27
  end
28
-
28
+
29
29
  def find_through_cache(id)
30
30
  unless use_activerecord_cache
31
31
  message = "ActiveRecord cache is not enabled for #{self.name}"
@@ -34,31 +34,31 @@ module ActiveRecordCache
34
34
 
35
35
  Rails.cache.read(cache_key(id)) || where(primary_key => id).first
36
36
  end
37
-
37
+
38
38
  def find_some_through_cache(ids)
39
39
  unless use_activerecord_cache
40
40
  message = "ActiveRecord cache is not enabled for #{self.name}"
41
41
  raise ActiveRecordCache::CacheNotEnabled, message
42
42
  end
43
-
43
+
44
44
  records = []
45
45
  cache_misses = []
46
-
46
+
47
47
  ids.each do |id|
48
48
  if record = find_in_cache(id)
49
49
  records << record
50
50
  else
51
- cache_misses << id
51
+ cache_misses << id
52
52
  end
53
53
  end
54
-
54
+
55
55
  if cache_misses.present?
56
56
  records += where(primary_key => cache_misses).all
57
57
  end
58
-
58
+
59
59
  records
60
60
  end
61
-
61
+
62
62
  def find_in_cache(id)
63
63
  unless use_activerecord_cache
64
64
  message = "ActiveRecord cache is not enabled for #{self.name}"
@@ -67,25 +67,20 @@ module ActiveRecordCache
67
67
 
68
68
  Rails.cache.read(cache_key(id))
69
69
  end
70
-
70
+
71
71
  end
72
-
73
- module InstanceMethods
74
72
 
75
- def write_to_cache
76
- unless self.class.use_activerecord_cache
77
- message = "ActiveRecord cache is not enabled for #{self.class.name}"
78
- raise ActiveRecordCache::CacheNotEnabled, message
79
- end
80
-
81
- Rails.cache.write(self.class.cache_key(self), self)
73
+ def write_to_cache
74
+ unless self.class.use_activerecord_cache
75
+ message = "ActiveRecord cache is not enabled for #{self.class.name}"
76
+ raise ActiveRecordCache::CacheNotEnabled, message
82
77
  end
83
78
 
84
- def delete_from_cache
85
- Rails.cache.delete(self.class.cache_key(self))
86
- end
87
-
79
+ Rails.cache.write(self.class.cache_key(self), self)
80
+ end
81
+
82
+ def delete_from_cache
83
+ Rails.cache.delete(self.class.cache_key(self))
88
84
  end
89
-
90
85
  end
91
86
  end
@@ -1,24 +1,19 @@
1
1
  module ActiveRecordCache
2
2
  module BelongsToAssociation
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  included do
6
6
  alias_method_chain :find_target, :caching
7
7
  end
8
-
9
-
10
- module InstanceMethods
11
-
8
+
12
9
  private
13
-
14
- def find_target_with_caching
15
- if klass.use_activerecord_cache && reflection.options[:primary_key].nil?
16
- klass.find_through_cache(owner[reflection.foreign_key]).tap { |record| set_inverse_instance(record) }
17
- else
18
- find_target_without_caching
19
- end
10
+
11
+ def find_target_with_caching
12
+ if klass.use_activerecord_cache && reflection.options[:primary_key].nil?
13
+ klass.find_through_cache(owner[reflection.foreign_key]).tap { |record| set_inverse_instance(record) }
14
+ else
15
+ find_target_without_caching
20
16
  end
21
17
  end
22
-
23
18
  end
24
- end
19
+ end
@@ -1,23 +1,17 @@
1
1
  module ActiveRecordCache
2
2
  module BelongsToPreloader
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  included do
6
6
  alias_method_chain :records_for, :caching
7
7
  end
8
-
9
-
10
- module InstanceMethods
11
-
12
- def records_for_with_caching(ids)
13
- if klass.use_activerecord_cache && association_key.name == klass.primary_key
14
- klass.find_some_through_cache(ids)
15
- else
16
- records_for_without_caching(ids)
17
- end
8
+
9
+ def records_for_with_caching(ids)
10
+ if klass.use_activerecord_cache && association_key.name == klass.primary_key
11
+ klass.find_some_through_cache(ids)
12
+ else
13
+ records_for_without_caching(ids)
18
14
  end
19
-
20
15
  end
21
-
22
16
  end
23
- end
17
+ end
@@ -1,68 +1,60 @@
1
1
  module ActiveRecordCache
2
2
  module FinderMethods
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  included do
6
6
  alias_method_chain :find_by_attributes, :caching
7
7
  alias_method_chain :find_some, :caching
8
8
  alias_method_chain :find_one, :caching
9
9
  end
10
10
 
11
- module InstanceMethods
12
-
13
11
  protected
14
-
15
- def find_by_attributes_with_caching(match, attributes, *args)
16
- unless cached_lookup_allowed? && attributes == Array(@klass.primary_key) && (match.finder == :all || !args.first.is_a?(Array))
17
- return find_by_attributes_without_caching(match, attributes, *args)
18
- end
19
-
20
- param = args.first
21
-
22
- results = case match.finder
23
- when :all then @klass.find_some_through_cache(Array(param))
24
- when :first then @klass.find_through_cache(param)
25
- when :last then @klass.find_through_cache(param)
26
- end
27
-
28
- results
29
- end
30
-
31
-
32
- def find_some_with_caching(ids)
33
- return find_some_without_caching(ids) unless cached_lookup_allowed?
34
-
35
- results = find_some_through_cache(ids)
36
12
 
37
- if results.size != ids.size
38
- error = "Couldn't find all #{@klass.name.pluralize} with IDs "
39
- error << "(#{ids.join(", ")}) (found #{results.size} results, but was looking for #{ids.size})"
40
- raise ActiveRecord::RecordNotFound, error
41
- end
42
-
43
- results
13
+ def find_by_attributes_with_caching(match, attributes, *args)
14
+ unless cached_lookup_allowed? && attributes == Array(@klass.primary_key) && (match.finder == :all || !args.first.is_a?(Array))
15
+ return find_by_attributes_without_caching(match, attributes, *args)
44
16
  end
45
-
46
-
47
- def find_one_with_caching(id)
48
- return find_one_without_caching(id) unless cached_lookup_allowed?
49
-
50
- record = @klass.find_through_cache(id)
51
-
52
- unless record
53
- raise ActiveRecord::RecordNotFound, "Couldn't find #{@klass.name} with #{primary_key}=#{id}"
54
- end
55
-
56
- record
17
+
18
+ param = args.first
19
+
20
+ results = case match.finder
21
+ when :all then @klass.find_some_through_cache(Array(param))
22
+ when :first then @klass.find_through_cache(param)
23
+ when :last then @klass.find_through_cache(param)
24
+ end
25
+
26
+ results
27
+ end
28
+
29
+ def find_some_with_caching(ids)
30
+ return find_some_without_caching(ids) unless cached_lookup_allowed?
31
+
32
+ results = find_some_through_cache(ids)
33
+
34
+ if results.size != ids.size
35
+ error = "Couldn't find all #{@klass.name.pluralize} with IDs "
36
+ error << "(#{ids.join(", ")}) (found #{results.size} results, but was looking for #{ids.size})"
37
+ raise ActiveRecord::RecordNotFound, error
57
38
  end
58
-
59
-
60
- # only support the most basic lookups through the cache
61
- def cached_lookup_allowed?
62
- @klass.use_activerecord_cache && arel.where_sql.nil? && arel.join_sql.nil? && @limit_value.nil? && @offset_value.nil?
39
+
40
+ results
41
+ end
42
+
43
+ def find_one_with_caching(id)
44
+ return find_one_without_caching(id) unless cached_lookup_allowed?
45
+
46
+ record = @klass.find_through_cache(id)
47
+
48
+ unless record
49
+ raise ActiveRecord::RecordNotFound, "Couldn't find #{@klass.name} with #{primary_key}=#{id}"
63
50
  end
64
-
51
+
52
+ record
53
+ end
54
+
55
+ # only support the most basic lookups through the cache
56
+ def cached_lookup_allowed?
57
+ @klass.use_activerecord_cache && arel.where_sql.nil? && arel.join_sql.nil? && @limit_value.nil? && @offset_value.nil?
65
58
  end
66
-
67
59
  end
68
- end
60
+ end
@@ -1,22 +1,18 @@
1
1
  module ActiveRecordCache
2
2
  module Relation
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  included do
6
6
  alias_method_chain :to_a, :caching
7
7
  end
8
-
9
- module InstanceMethods
10
- def to_a_with_caching
11
- previously_loaded = loaded?
12
8
 
13
- records = to_a_without_caching
14
- records.each(&:write_to_cache) if @klass.use_activerecord_cache && !previously_loaded && select_values.empty?
15
-
16
- records
17
- end
18
-
9
+ def to_a_with_caching
10
+ previously_loaded = loaded?
11
+
12
+ records = to_a_without_caching
13
+ records.each(&:write_to_cache) if @klass.use_activerecord_cache && !previously_loaded && select_values.empty?
14
+
15
+ records
19
16
  end
20
-
21
17
  end
22
18
  end
@@ -2,7 +2,7 @@ module ActiveRecordCache
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 5
5
+ TINY = 6
6
6
  PRE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-02 00:00:00.000000000Z
12
+ date: 2013-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &2170443680 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: 3.1.3
22
+ - - <=
23
+ - !ruby/object:Gem::Version
24
+ version: 3.3.0
22
25
  type: :runtime
23
26
  prerelease: false
24
- version_requirements: *2170443680
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.3
33
+ - - <=
34
+ - !ruby/object:Gem::Version
35
+ version: 3.3.0
25
36
  description: Caches ActiveRecord models for simple finds using the Rails low-level
26
37
  cache
27
38
  email:
@@ -56,15 +67,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
67
  - - ! '>='
57
68
  - !ruby/object:Gem::Version
58
69
  version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -1348494793212696198
59
73
  required_rubygems_version: !ruby/object:Gem::Requirement
60
74
  none: false
61
75
  requirements:
62
76
  - - ! '>='
63
77
  - !ruby/object:Gem::Version
64
78
  version: '0'
79
+ segments:
80
+ - 0
81
+ hash: -1348494793212696198
65
82
  requirements: []
66
83
  rubyforge_project:
67
- rubygems_version: 1.8.15
84
+ rubygems_version: 1.8.24
68
85
  signing_key:
69
86
  specification_version: 3
70
87
  summary: A basic caching plugin for ActiveRecord