second_level_cache 2.1.16 → 2.2.1
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 +5 -5
- data/CHANGELOG.md +4 -8
- data/Gemfile +1 -1
- data/README.md +14 -13
- data/Rakefile +2 -2
- data/lib/second_level_cache/active_record/base.rb +9 -13
- data/lib/second_level_cache/active_record/belongs_to_association.rb +8 -12
- data/lib/second_level_cache/active_record/core.rb +6 -10
- data/lib/second_level_cache/active_record/fetch_by_uniq_key.rb +29 -17
- data/lib/second_level_cache/active_record/finder_methods.rb +23 -48
- data/lib/second_level_cache/active_record/has_one_association.rb +18 -19
- data/lib/second_level_cache/active_record/persistence.rb +6 -17
- data/lib/second_level_cache/active_record/preloader.rb +16 -17
- data/lib/second_level_cache/active_record/railtie.rb +13 -10
- data/lib/second_level_cache/active_record.rb +5 -8
- data/lib/second_level_cache/config.rb +14 -15
- data/lib/second_level_cache/record_marshal.rb +20 -35
- data/lib/second_level_cache/version.rb +2 -2
- data/lib/second_level_cache.rb +18 -24
- data/second_level_cache.gemspec +19 -18
- data/test/active_record_test_case_helper.rb +15 -12
- data/test/base_test.rb +2 -3
- data/test/belongs_to_association_test.rb +2 -3
- data/test/fetch_by_uniq_key_test.rb +14 -15
- data/test/finder_methods_test.rb +5 -44
- data/test/has_one_association_test.rb +5 -10
- data/test/model/account.rb +1 -2
- data/test/model/animal.rb +0 -1
- data/test/model/book.rb +4 -5
- data/test/model/image.rb +2 -4
- data/test/model/post.rb +3 -4
- data/test/model/topic.rb +2 -3
- data/test/model/user.rb +11 -14
- data/test/persistence_test.rb +3 -4
- data/test/polymorphic_association_test.rb +1 -2
- data/test/preloader_test.rb +2 -3
- data/test/record_marshal_test.rb +12 -15
- data/test/require_test.rb +2 -3
- data/test/second_level_cache_test.rb +2 -3
- data/test/single_table_inheritance_test.rb +0 -1
- data/test/test_helper.rb +13 -13
- metadata +49 -37
- data/lib/second_level_cache/active_record/multi_read_from_cache.rb +0 -21
- data/test/model/multi_read_from_cache_test.rb +0 -19
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
module RecordMarshal
|
3
2
|
class << self
|
4
3
|
# dump ActiveRecord instace with only attributes.
|
@@ -10,52 +9,38 @@ module RecordMarshal
|
|
10
9
|
# ]
|
11
10
|
|
12
11
|
def dump(record)
|
13
|
-
[
|
14
|
-
record.class.name,
|
15
|
-
record.attributes
|
16
|
-
]
|
12
|
+
[record.class.name, record.attributes]
|
17
13
|
end
|
18
14
|
|
19
15
|
# load a cached record
|
20
16
|
def load(serialized)
|
21
17
|
return unless serialized
|
22
|
-
#fix issues 19
|
23
|
-
#fix 2.1.2 object.changed? ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Hash, but was a String. -- "{:a=>\"t\", :b=>\"x\"}"
|
24
|
-
#fix 2.1.4 object.changed? is true
|
25
|
-
#fix Rails 4.2 is deprecating `serialized_attributes` without replacement to Rails 5 is deprecating `serialized_attributes` without replacement
|
26
|
-
klass
|
27
|
-
|
28
|
-
klass.serialized_attributes.each do |k, v|
|
29
|
-
next if attributes[k].nil? || attributes[k].is_a?(String)
|
30
|
-
if attributes[k].respond_to?(:unserialize)
|
31
|
-
if attributes[k].serialized_value.is_a?(String)
|
32
|
-
attributes[k] = attributes[k].serialized_value
|
33
|
-
next
|
34
|
-
end
|
18
|
+
# fix issues 19
|
19
|
+
# fix 2.1.2 object.changed? ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Hash, but was a String. -- "{:a=>\"t\", :b=>\"x\"}"
|
20
|
+
# fix 2.1.4 object.changed? is true
|
21
|
+
# fix Rails 4.2 is deprecating `serialized_attributes` without replacement to Rails 5 is deprecating `serialized_attributes` without replacement
|
22
|
+
klass = serialized[0].constantize
|
23
|
+
attributes = serialized[1]
|
35
24
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
if coder.is_a?(ActiveRecord::Coders::YAMLColumn)
|
48
|
-
attributes[name] = coder.dump(attributes[name]) if attributes[name].is_a?(coder.object_class)
|
49
|
-
elsif coder == ActiveRecord::Coders::JSON
|
50
|
-
attributes[name] = attributes[name].to_json
|
51
|
-
end
|
25
|
+
# for ActiveRecord 5.0.0
|
26
|
+
klass.columns.each do |c|
|
27
|
+
name = c.name
|
28
|
+
cast_type = klass.attribute_types[name]
|
29
|
+
next unless cast_type.is_a?(::ActiveRecord::Type::Serialized)
|
30
|
+
coder = cast_type.coder
|
31
|
+
next if attributes[name].nil? || attributes[name].is_a?(String)
|
32
|
+
if coder.is_a?(::ActiveRecord::Coders::YAMLColumn)
|
33
|
+
attributes[name] = coder.dump(attributes[name]) if attributes[name].is_a?(coder.object_class)
|
34
|
+
elsif coder == ::ActiveRecord::Coders::JSON
|
35
|
+
attributes[name] = attributes[name].to_json
|
52
36
|
end
|
53
37
|
end
|
38
|
+
|
54
39
|
klass.instantiate(attributes)
|
55
40
|
end
|
56
41
|
|
57
42
|
def load_multi(serializeds)
|
58
|
-
serializeds.map{|serialized| load(serialized)}
|
43
|
+
serializeds.map { |serialized| load(serialized) }
|
59
44
|
end
|
60
45
|
end
|
61
46
|
end
|
data/lib/second_level_cache.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require 'active_support/all'
|
3
2
|
require 'second_level_cache/config'
|
4
3
|
require 'second_level_cache/record_marshal'
|
@@ -9,7 +8,7 @@ module SecondLevelCache
|
|
9
8
|
end
|
10
9
|
|
11
10
|
class << self
|
12
|
-
delegate :logger, :cache_store, :cache_key_prefix, :
|
11
|
+
delegate :logger, :cache_store, :cache_key_prefix, to: Config
|
13
12
|
end
|
14
13
|
|
15
14
|
module Mixin
|
@@ -18,39 +17,30 @@ module SecondLevelCache
|
|
18
17
|
module ClassMethods
|
19
18
|
attr_reader :second_level_cache_options
|
20
19
|
|
20
|
+
delegate :logger, :cache_store, :cache_key_prefix, to: SecondLevelCache
|
21
|
+
|
21
22
|
def acts_as_cached(options = {})
|
22
23
|
@second_level_cache_enabled = true
|
23
24
|
@second_level_cache_options = options
|
24
25
|
@second_level_cache_options[:expires_in] ||= 1.week
|
25
26
|
@second_level_cache_options[:version] ||= 0
|
26
|
-
relation.class.send :
|
27
|
-
|
27
|
+
relation.class.send :prepend, SecondLevelCache::ActiveRecord::FinderMethods
|
28
|
+
prepend SecondLevelCache::ActiveRecord::Core
|
28
29
|
end
|
29
30
|
|
30
31
|
def second_level_cache_enabled?
|
31
|
-
|
32
|
+
@second_level_cache_enabled == true
|
32
33
|
end
|
33
34
|
|
34
35
|
def without_second_level_cache
|
35
|
-
old
|
36
|
+
old = @second_level_cache_enabled
|
37
|
+
@second_level_cache_enabled = false
|
36
38
|
|
37
39
|
yield if block_given?
|
38
40
|
ensure
|
39
41
|
@second_level_cache_enabled = old
|
40
42
|
end
|
41
43
|
|
42
|
-
def cache_store
|
43
|
-
Config.cache_store
|
44
|
-
end
|
45
|
-
|
46
|
-
def logger
|
47
|
-
Config.logger
|
48
|
-
end
|
49
|
-
|
50
|
-
def cache_key_prefix
|
51
|
-
Config.cache_key_prefix
|
52
|
-
end
|
53
|
-
|
54
44
|
def cache_version
|
55
45
|
second_level_cache_options[:version]
|
56
46
|
end
|
@@ -60,11 +50,13 @@ module SecondLevelCache
|
|
60
50
|
end
|
61
51
|
|
62
52
|
def read_second_level_cache(id)
|
63
|
-
|
53
|
+
return unless second_level_cache_enabled?
|
54
|
+
RecordMarshal.load(SecondLevelCache.cache_store.read(second_level_cache_key(id)))
|
64
55
|
end
|
65
56
|
|
66
57
|
def expire_second_level_cache(id)
|
67
|
-
|
58
|
+
return unless second_level_cache_enabled?
|
59
|
+
SecondLevelCache.cache_store.delete(second_level_cache_key(id))
|
68
60
|
end
|
69
61
|
end
|
70
62
|
|
@@ -73,13 +65,15 @@ module SecondLevelCache
|
|
73
65
|
end
|
74
66
|
|
75
67
|
def expire_second_level_cache
|
76
|
-
|
68
|
+
return unless self.class.second_level_cache_enabled?
|
69
|
+
SecondLevelCache.cache_store.delete(second_level_cache_key)
|
77
70
|
end
|
78
71
|
|
79
72
|
def write_second_level_cache
|
80
|
-
|
81
|
-
|
82
|
-
|
73
|
+
return unless self.class.second_level_cache_enabled?
|
74
|
+
marshal = RecordMarshal.dump(self)
|
75
|
+
expires_in = self.class.second_level_cache_options[:expires_in]
|
76
|
+
SecondLevelCache.cache_store.write(second_level_cache_key, marshal, expires_in: expires_in)
|
83
77
|
end
|
84
78
|
|
85
79
|
alias update_second_level_cache write_second_level_cache
|
data/second_level_cache.gemspec
CHANGED
@@ -4,9 +4,9 @@ lib = File.expand_path('../lib', __FILE__)
|
|
4
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
|
-
gem.authors = [
|
8
|
-
gem.email = [
|
9
|
-
gem.description =
|
7
|
+
gem.authors = ['Hooopo']
|
8
|
+
gem.email = ['hoooopo@gmail.com']
|
9
|
+
gem.description = 'Write Through and Read Through caching library inspired by CacheMoney and cache_fu, support ActiveRecord 4.'
|
10
10
|
gem.summary = <<-SUMMARY
|
11
11
|
SecondLevelCache is a write-through and read-through caching library inspired by Cache Money and cache_fu, support only Rails3 and ActiveRecord.
|
12
12
|
|
@@ -15,26 +15,27 @@ Gem::Specification.new do |gem|
|
|
15
15
|
Write-Through: As objects are created, updated, and deleted, all of the caches are automatically kept up-to-date and coherent.
|
16
16
|
SUMMARY
|
17
17
|
|
18
|
-
gem.homepage =
|
18
|
+
gem.homepage = 'https://github.com/csdn-dev/second_level_cache'
|
19
19
|
|
20
|
-
gem.files = Dir.glob(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
gem.files = Dir.glob('lib/**/*.rb') + [
|
21
|
+
'README.md',
|
22
|
+
'Rakefile',
|
23
|
+
'Gemfile',
|
24
|
+
'CHANGELOG.md',
|
25
|
+
'second_level_cache.gemspec'
|
26
26
|
]
|
27
|
-
gem.test_files = Dir.glob(
|
27
|
+
gem.test_files = Dir.glob('test/**/*.rb')
|
28
28
|
gem.executables = gem.files.grep(%r{^bin/})
|
29
|
-
gem.name =
|
30
|
-
gem.require_paths = [
|
29
|
+
gem.name = 'second_level_cache'
|
30
|
+
gem.require_paths = ['lib']
|
31
31
|
gem.version = SecondLevelCache::VERSION
|
32
32
|
|
33
|
-
gem.add_runtime_dependency
|
33
|
+
gem.add_runtime_dependency 'activesupport', ['>= 5.0.0.beta1', '< 5.1.0']
|
34
|
+
gem.add_runtime_dependency 'activerecord', ['>= 5.0.0.beta1', '< 5.1.0']
|
34
35
|
|
35
|
-
gem.
|
36
|
-
gem.add_development_dependency
|
37
|
-
gem.add_development_dependency "rake"
|
36
|
+
gem.add_development_dependency 'sqlite3'
|
37
|
+
gem.add_development_dependency 'rake'
|
38
38
|
gem.add_development_dependency 'pry'
|
39
|
-
gem.add_development_dependency
|
39
|
+
gem.add_development_dependency 'database_cleaner', '~> 1.3.0'
|
40
|
+
gem.add_development_dependency 'rubocop', '~> 0.36.0'
|
40
41
|
end
|
@@ -37,9 +37,9 @@ module ActiveRecordTestCaseHelper
|
|
37
37
|
ensure
|
38
38
|
failed_patterns = []
|
39
39
|
patterns_to_match.each do |pattern|
|
40
|
-
failed_patterns << pattern unless SQLCounter.log_all.any?{ |sql| pattern === sql }
|
40
|
+
failed_patterns << pattern unless SQLCounter.log_all.any? { |sql| pattern === sql }
|
41
41
|
end
|
42
|
-
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map
|
42
|
+
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found.#{SQLCounter.log.empty? ? '' : "\nQueries:\n#{SQLCounter.log.join("\n")}"}"
|
43
43
|
end
|
44
44
|
|
45
45
|
def assert_queries(num = 1, options = {})
|
@@ -48,9 +48,9 @@ module ActiveRecordTestCaseHelper
|
|
48
48
|
x = yield
|
49
49
|
the_log = ignore_none ? SQLCounter.log_all : SQLCounter.log
|
50
50
|
if num == :any
|
51
|
-
assert_operator the_log.size, :>=, 1,
|
51
|
+
assert_operator the_log.size, :>=, 1, '1 or more queries expected, but none were executed.'
|
52
52
|
else
|
53
|
-
mesg = "#{the_log.size} instead of #{num} queries were executed.#{the_log.
|
53
|
+
mesg = "#{the_log.size} instead of #{num} queries were executed.#{the_log.empty? ? '' : "\nQueries:\n#{the_log.join("\n")}"}"
|
54
54
|
assert_equal num, the_log.size, mesg
|
55
55
|
end
|
56
56
|
x
|
@@ -61,15 +61,15 @@ module ActiveRecordTestCaseHelper
|
|
61
61
|
assert_queries(0, options, &block)
|
62
62
|
end
|
63
63
|
|
64
|
-
def assert_column(model, column_name, msg=nil)
|
65
|
-
assert
|
64
|
+
def assert_column(model, column_name, msg = nil)
|
65
|
+
assert exists_column?(model, column_name), msg
|
66
66
|
end
|
67
67
|
|
68
|
-
def assert_no_column(model, column_name, msg=nil)
|
69
|
-
assert_not
|
68
|
+
def assert_no_column(model, column_name, msg = nil)
|
69
|
+
assert_not exists_column?(model, column_name), msg
|
70
70
|
end
|
71
71
|
|
72
|
-
def
|
72
|
+
def exists_column?(model, column_name)
|
73
73
|
model.reset_column_information
|
74
74
|
model.column_names.include?(column_name.to_s)
|
75
75
|
end
|
@@ -77,10 +77,13 @@ module ActiveRecordTestCaseHelper
|
|
77
77
|
class SQLCounter
|
78
78
|
class << self
|
79
79
|
attr_accessor :ignored_sql, :log, :log_all
|
80
|
-
def clear_log
|
80
|
+
def clear_log
|
81
|
+
self.log = []
|
82
|
+
self.log_all = []
|
83
|
+
end
|
81
84
|
end
|
82
85
|
|
83
|
-
|
86
|
+
clear_log
|
84
87
|
|
85
88
|
self.ignored_sql = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/, /^BEGIN/, /^COMMIT/]
|
86
89
|
|
@@ -102,7 +105,7 @@ module ActiveRecordTestCaseHelper
|
|
102
105
|
@ignore = ignore
|
103
106
|
end
|
104
107
|
|
105
|
-
def call(
|
108
|
+
def call(_name, _start, _finish, _message_id, values)
|
106
109
|
sql = values[:sql]
|
107
110
|
|
108
111
|
# FIXME: this seems bad. we should probably have a better way to indicate
|
data/test/base_test.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require 'test_helper'
|
3
2
|
|
4
3
|
class BaseTest < ActiveSupport::TestCase
|
5
4
|
def setup
|
6
|
-
@user = User.create :
|
5
|
+
@user = User.create name: 'csdn', email: 'test@csdn.com'
|
7
6
|
end
|
8
7
|
|
9
8
|
def test_should_update_cache_when_update_attributes
|
10
|
-
@user.update_attributes :
|
9
|
+
@user.update_attributes name: 'change'
|
11
10
|
assert_equal @user.name, User.read_second_level_cache(@user.id).name
|
12
11
|
end
|
13
12
|
|
@@ -1,16 +1,15 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require 'test_helper'
|
3
2
|
|
4
3
|
class BelongsToAssociationTest < ActiveSupport::TestCase
|
5
4
|
def setup
|
6
|
-
@user = User.create :
|
5
|
+
@user = User.create name: 'csdn', email: 'test@csdn.com'
|
7
6
|
end
|
8
7
|
|
9
8
|
def test_should_get_cache_when_use_belongs_to_association
|
10
9
|
book = @user.books.create
|
11
10
|
|
12
11
|
@user.write_second_level_cache
|
13
|
-
book.clear_association_cache
|
12
|
+
book.send(:clear_association_cache)
|
14
13
|
assert_no_queries do
|
15
14
|
assert_equal @user, book.user
|
16
15
|
end
|
@@ -1,43 +1,42 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require 'test_helper'
|
3
2
|
|
4
3
|
class FetchByUinqKeyTest < ActiveSupport::TestCase
|
5
4
|
def setup
|
6
|
-
@user = User.create :
|
7
|
-
@post = Post.create :
|
5
|
+
@user = User.create name: 'hooopo', email: 'hoooopo@gmail.com'
|
6
|
+
@post = Post.create slug: 'foobar', topic_id: 2
|
8
7
|
end
|
9
8
|
|
10
9
|
def test_cache_uniq_key
|
11
|
-
assert_equal User.send(:cache_uniq_key,
|
12
|
-
assert_equal User.send(:cache_uniq_key,
|
13
|
-
assert_equal User.send(:cache_uniq_key,
|
14
|
-
long_val =
|
15
|
-
assert_equal User.send(:cache_uniq_key,
|
10
|
+
assert_equal User.send(:cache_uniq_key, name: 'hooopo'), 'uniq_key_User_name_hooopo'
|
11
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: 2), 'uniq_key_User_foo_1,bar_2'
|
12
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: nil), 'uniq_key_User_foo_1,bar_'
|
13
|
+
long_val = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
14
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: long_val), "uniq_key_User_foo_1,bar_#{Digest::MD5.hexdigest(long_val)}"
|
16
15
|
end
|
17
16
|
|
18
17
|
def test_should_query_from_db_using_primary_key
|
19
|
-
Post.fetch_by_uniq_keys(:
|
18
|
+
Post.fetch_by_uniq_keys(topic_id: 2, slug: 'foobar')
|
20
19
|
@post.expire_second_level_cache
|
21
|
-
assert_sql(/SELECT\s+"posts".* FROM "posts"\s+WHERE "posts"."id" = \? LIMIT
|
22
|
-
Post.fetch_by_uniq_keys(:
|
20
|
+
assert_sql(/SELECT\s+"posts".* FROM "posts"\s+WHERE "posts"."id" = \? LIMIT ?/) do
|
21
|
+
Post.fetch_by_uniq_keys(topic_id: 2, slug: 'foobar')
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
25
|
def test_should_not_hit_db_using_fetch_by_uniq_key_twice
|
27
|
-
post = Post.fetch_by_uniq_keys(:
|
26
|
+
post = Post.fetch_by_uniq_keys(topic_id: 2, slug: 'foobar')
|
28
27
|
assert_equal post, @post
|
29
28
|
assert_no_queries do
|
30
|
-
Post.fetch_by_uniq_keys(:
|
29
|
+
Post.fetch_by_uniq_keys(topic_id: 2, slug: 'foobar')
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
33
|
def test_should_fail_when_fetch_by_uniq_key_with_bang_method
|
35
34
|
assert_raises(ActiveRecord::RecordNotFound) do
|
36
|
-
Post.fetch_by_uniq_keys!(:
|
35
|
+
Post.fetch_by_uniq_keys!(topic_id: 2, slug: 'foobar1')
|
37
36
|
end
|
38
37
|
|
39
38
|
assert_raises(ActiveRecord::RecordNotFound) do
|
40
|
-
User.fetch_by_uniq_key!(
|
39
|
+
User.fetch_by_uniq_key!('xxxxx', :name)
|
41
40
|
end
|
42
41
|
end
|
43
42
|
|
data/test/finder_methods_test.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require 'test_helper'
|
3
2
|
|
4
3
|
class FinderMethodsTest < ActiveSupport::TestCase
|
5
4
|
def setup
|
6
|
-
@user = User.create :
|
7
|
-
@other_user = User.create :name => 'shopper+', :email => 'test@shopperplus.com'
|
5
|
+
@user = User.create name: 'csdn', email: 'test@csdn.com'
|
8
6
|
end
|
9
7
|
|
10
8
|
def test_should_find_without_cache
|
@@ -22,61 +20,24 @@ class FinderMethodsTest < ActiveSupport::TestCase
|
|
22
20
|
def test_should_find_with_condition
|
23
21
|
@user.write_second_level_cache
|
24
22
|
assert_no_queries do
|
25
|
-
assert_equal @user, User.where(:
|
23
|
+
assert_equal @user, User.where(name: @user.name).find(@user.id)
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
|
-
def
|
27
|
+
def test_should_not_find_from_cache_when_select_speical_columns
|
30
28
|
@user.write_second_level_cache
|
31
|
-
only_id_user = User.select(
|
29
|
+
only_id_user = User.select('id').find(@user.id)
|
32
30
|
assert_raises(ActiveModel::MissingAttributeError) do
|
33
31
|
only_id_user.name
|
34
32
|
end
|
35
33
|
end
|
36
34
|
|
37
35
|
def test_without_second_level_cache
|
38
|
-
@user.name =
|
36
|
+
@user.name = 'NewName'
|
39
37
|
@user.write_second_level_cache
|
40
38
|
User.without_second_level_cache do
|
41
39
|
@from_db = User.find(@user.id)
|
42
40
|
end
|
43
41
|
refute_equal @user.name, @from_db.name
|
44
42
|
end
|
45
|
-
|
46
|
-
def test_find_some_record
|
47
|
-
@users = User.find(@user.id, @other_user.id)
|
48
|
-
assert_equal 2, @users.size
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_find_some_record_without_second_level_cache
|
52
|
-
User.without_second_level_cache do
|
53
|
-
@users = User.find(@user.id, @other_user.id)
|
54
|
-
end
|
55
|
-
assert_equal 2, @users.size
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_missing_id_will_raise_for_find_some
|
59
|
-
assert_raises(ActiveRecord::RecordNotFound) do
|
60
|
-
@users = User.find(@user.id, User.last.id + 10000)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_filter_works_fine_for_find_some
|
65
|
-
assert_raises(ActiveRecord::RecordNotFound) do
|
66
|
-
@users = User.where("name is null").find(@user.id, @other_user.id)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_half_in_cache_for_find_some
|
71
|
-
@user.expire_second_level_cache
|
72
|
-
@users = User.find(@user.id, @other_user.id)
|
73
|
-
assert_equal 2, @users.size
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_no_record_in_cache_for_find_some
|
77
|
-
@user.expire_second_level_cache
|
78
|
-
@other_user.expire_second_level_cache
|
79
|
-
@users = User.find(@user.id, @other_user.id)
|
80
|
-
assert_equal 2, @users.size
|
81
|
-
end
|
82
43
|
end
|
@@ -1,9 +1,8 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require 'test_helper'
|
3
2
|
|
4
3
|
class HasOneAssociationTest < ActiveSupport::TestCase
|
5
4
|
def setup
|
6
|
-
@user = User.create :
|
5
|
+
@user = User.create name: 'hooopo', email: 'hoooopo@gmail.com'
|
7
6
|
@account = @user.create_account
|
8
7
|
end
|
9
8
|
|
@@ -15,7 +14,7 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
15
14
|
end
|
16
15
|
|
17
16
|
def test_should_fetch_has_one_through
|
18
|
-
user = User.create :
|
17
|
+
user = User.create name: 'hooopo', email: 'hoooopo@gmail.com', forked_from_user: @user
|
19
18
|
clean_user = user.reload
|
20
19
|
assert_equal User, clean_user.forked_from_user.class
|
21
20
|
assert_equal @user.id, user.forked_from_user.id
|
@@ -27,15 +26,11 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
27
26
|
|
28
27
|
def test_has_one_with_conditions
|
29
28
|
user = User.create name: 'hooopo', email: 'hoooopo@gmail.com'
|
30
|
-
|
29
|
+
Namespace.create(user_id: user.id, name: 'ruby-china', kind: 'group')
|
31
30
|
user.create_namespace(name: 'hooopo')
|
32
|
-
|
31
|
+
Namespace.create(user_id: user.id, name: 'rails', kind: 'group')
|
33
32
|
assert_not_equal user.namespace, nil
|
34
|
-
|
35
|
-
clear_user = user.reload
|
36
|
-
assert_no_queries do
|
37
|
-
clear_user.namespace
|
38
|
-
end
|
33
|
+
clear_user = User.find(user.id)
|
39
34
|
assert_equal clear_user.namespace.name, 'hooopo'
|
40
35
|
end
|
41
36
|
end
|
data/test/model/account.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
ActiveRecord::Base.connection.create_table(:accounts, force: true) do |t|
|
3
2
|
t.integer :age
|
4
3
|
t.string :site
|
@@ -7,6 +6,6 @@ ActiveRecord::Base.connection.create_table(:accounts, force: true) do |t|
|
|
7
6
|
end
|
8
7
|
|
9
8
|
class Account < ActiveRecord::Base
|
10
|
-
acts_as_cached
|
9
|
+
acts_as_cached expires_in: 3.days
|
11
10
|
belongs_to :user
|
12
11
|
end
|
data/test/model/animal.rb
CHANGED
data/test/model/book.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
|
-
|
2
|
-
ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
|
1
|
+
ActiveRecord::Base.connection.create_table(:books, force: true) do |t|
|
3
2
|
t.string :title
|
4
3
|
t.string :body
|
5
4
|
t.integer :user_id
|
6
|
-
t.integer :images_count, :
|
5
|
+
t.integer :images_count, default: 0
|
7
6
|
end
|
8
7
|
|
9
8
|
class Book < ActiveRecord::Base
|
10
9
|
acts_as_cached
|
11
10
|
|
12
|
-
belongs_to :user, :
|
13
|
-
has_many :images, :
|
11
|
+
belongs_to :user, counter_cache: true
|
12
|
+
has_many :images, as: :imagable
|
14
13
|
end
|
data/test/model/image.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
ActiveRecord::Base.connection.create_table(:images, :force => true) do |t|
|
1
|
+
ActiveRecord::Base.connection.create_table(:images, force: true) do |t|
|
3
2
|
t.string :url
|
4
3
|
t.string :imagable_type
|
5
4
|
t.integer :imagable_id
|
@@ -8,6 +7,5 @@ end
|
|
8
7
|
class Image < ActiveRecord::Base
|
9
8
|
acts_as_cached
|
10
9
|
|
11
|
-
belongs_to :imagable, :
|
10
|
+
belongs_to :imagable, polymorphic: true, counter_cache: true
|
12
11
|
end
|
13
|
-
|
data/test/model/post.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
t.text :body
|
1
|
+
ActiveRecord::Base.connection.create_table(:posts, force: true) do |t|
|
2
|
+
t.text :body
|
4
3
|
t.string :slug
|
5
4
|
t.integer :topic_id
|
6
5
|
end
|
7
6
|
|
8
7
|
class Post < ActiveRecord::Base
|
9
8
|
acts_as_cached
|
10
|
-
belongs_to :topic, :
|
9
|
+
belongs_to :topic, touch: true
|
11
10
|
end
|
data/test/model/topic.rb
CHANGED