second_level_cache 2.0.0.beta → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ module SecondLevelCache
5
5
  def fetch_by_uniq_key(value, uniq_key_name)
6
6
  return self.where(uniq_key_name => value).first unless self.second_level_cache_enabled?
7
7
  if _id = SecondLevelCache.cache_store.read(cache_uniq_key(value, uniq_key_name))
8
- self.find_by_id(_id)
8
+ self.find(_id) rescue nil
9
9
  else
10
10
  record = self.where(uniq_key_name => value).first
11
11
  record.tap{|record| SecondLevelCache.cache_store.write(cache_uniq_key(value, uniq_key_name), record.id)} if record
@@ -11,8 +11,8 @@ module SecondLevelCache
11
11
  end
12
12
 
13
13
  def find_target_with_second_level_cache
14
- return find_target_without_second_level_cache unless association_class.second_level_cache_enabled?
15
- cache_record = association_class.fetch_by_uniq_key(owner[reflection.active_record_primary_key], reflection.foreign_key)
14
+ return find_target_without_second_level_cache unless klass.second_level_cache_enabled?
15
+ cache_record = klass.fetch_by_uniq_key(owner[reflection.active_record_primary_key], reflection.foreign_key)
16
16
  return cache_record.tap{|record| set_inverse_instance(record)} if cache_record
17
17
 
18
18
  record = find_target_without_second_level_cache
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module SecondLevelCache
3
- VERSION = "2.0.0.beta"
3
+ VERSION = "2.0.0.rc1"
4
4
  end
@@ -0,0 +1,30 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'active_record/test_helper'
3
+
4
+ class ActiveRecord::FetchByUinqKeyTest < Test::Unit::TestCase
5
+ def setup
6
+ DatabaseCleaner[:active_record].start
7
+ @user = User.create :name => 'hooopo', :email => 'hoooopo@gmail.com'
8
+ end
9
+
10
+ def test_should_query_from_db_using_primary_key
11
+ User.fetch_by_uniq_key(@user.name, :name)
12
+ $sql_logger = nil
13
+ User.fetch_by_uniq_key(@user.name, :name)
14
+ assert_equal $sql_logger.strip, 'SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1'
15
+ end
16
+
17
+ def test_should_not_hit_db_using_fetch_by_uniq_key_twice
18
+ user = User.fetch_by_uniq_key(@user.name, :name)
19
+ assert_equal user, @user
20
+ no_connection do
21
+ User.fetch_by_uniq_key(@user.name, :name)
22
+ end
23
+ end
24
+
25
+ def test_should_fail_when_fetch_by_uniq_key_with_bang_method
26
+ assert_raise(ActiveRecord::RecordNotFound) do
27
+ User.fetch_by_uniq_key!(@user.name + "not_exist", :name)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'active_record/test_helper'
3
+
4
+ class ActiveRecord::HasOneAssociationTest < Test::Unit::TestCase
5
+ def setup
6
+ @user = User.create :name => 'hooopo', :email => 'hoooopo@gmail.com'
7
+ @account = @user.create_account
8
+ end
9
+
10
+ def test_should_fetch_account_from_cache
11
+ clean_user = @user.reload
12
+ no_connection do
13
+ clean_user.account
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding : utf-8 -*-
2
+ ActiveRecord::Base.connection.create_table(:accounts, :force => true) do |t|
3
+ t.integer :age
4
+ t.string :site
5
+ t.integer :user_id
6
+ t.timestamps
7
+ end
8
+
9
+ class Account < ActiveRecord::Base
10
+ acts_as_cached(:expires_in => 3.day)
11
+ belongs_to :user
12
+ end
@@ -1,7 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
3
3
  t.text :options
4
- t.string :name
4
+ t.string :name, :unique => true
5
5
  t.string :email
6
6
  t.integer :books_count, :default => 0
7
7
  t.integer :images_count, :default => 0
@@ -12,7 +12,7 @@ class User < ActiveRecord::Base
12
12
  CacheVersion = 3
13
13
  serialize :options, Array
14
14
  acts_as_cached(:version => CacheVersion, :expires_in => 3.day)
15
-
15
+ has_one :account
16
16
  has_many :books
17
17
  has_many :images, :as => :imagable
18
18
  end
@@ -25,6 +25,7 @@ class Test::Unit::TestCase
25
25
 
26
26
  def teardown
27
27
  $sql_logger = nil
28
+ SecondLevelCache.cache_store.clear
28
29
  DatabaseCleaner[:active_record].clean
29
30
  end
30
31
  end
@@ -46,3 +47,4 @@ require 'active_record/model/book'
46
47
  require 'active_record/model/image'
47
48
  require 'active_record/model/topic'
48
49
  require 'active_record/model/post'
50
+ require 'active_record/model/account'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: second_level_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta
4
+ version: 2.0.0.rc1
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -119,7 +119,10 @@ files:
119
119
  - second_level_cache.gemspec
120
120
  - test/active_record/base_test.rb
121
121
  - test/active_record/belongs_to_association_test.rb
122
+ - test/active_record/fetch_by_uniq_key_test.rb
122
123
  - test/active_record/finder_methods_test.rb
124
+ - test/active_record/has_one_association_test.rb
125
+ - test/active_record/model/account.rb
123
126
  - test/active_record/model/book.rb
124
127
  - test/active_record/model/image.rb
125
128
  - test/active_record/model/post.rb
@@ -165,7 +168,10 @@ summary: ! 'SecondLevelCache is a write-through and read-through caching library
165
168
  test_files:
166
169
  - test/active_record/base_test.rb
167
170
  - test/active_record/belongs_to_association_test.rb
171
+ - test/active_record/fetch_by_uniq_key_test.rb
168
172
  - test/active_record/finder_methods_test.rb
173
+ - test/active_record/has_one_association_test.rb
174
+ - test/active_record/model/account.rb
169
175
  - test/active_record/model/book.rb
170
176
  - test/active_record/model/image.rb
171
177
  - test/active_record/model/post.rb