second_level_cache 2.1.0.rc1 → 2.1.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -134,10 +134,11 @@ end
134
134
 
135
135
  ```ruby
136
136
  # this will fetch from cache
137
- user = User.fetch_by_uniq_key("hooopo", :nick_name)
137
+ user = User.fetch_by_uniq_keys(nick_name: "hooopo")
138
+ post = Post.fetch_by_uniq_keys(user_id: 2, slug: "foo")
138
139
 
139
140
  # this also fetch from cache
140
- user = User.fetch_by_uniq_key!("hooopo", :nick_name) # this will raise `ActiveRecord::RecordNotFound` Exception when nick name not exists.
141
+ user = User.fetch_by_uniq_keys!(nick_name: "hooopo") # this will raise `ActiveRecord::RecordNotFound` Exception when nick name not exists.
141
142
  ```
142
143
 
143
144
  * You can use Rails's [Eager Loading](http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations) feature as normal. Even better, second_level_cache will transform the `IN` query into a Rails.cache.multi_read operation. For example:
@@ -2,24 +2,38 @@
2
2
  module SecondLevelCache
3
3
  module ActiveRecord
4
4
  module FetchByUniqKey
5
- def fetch_by_uniq_key(value, uniq_key_name)
6
- return self.where(uniq_key_name => value).first unless self.second_level_cache_enabled?
7
- if _id = SecondLevelCache.cache_store.read(cache_uniq_key(value, uniq_key_name))
5
+ def fetch_by_uniq_keys(where_values)
6
+ cache_key = cache_uniq_key(where_values)
7
+ if _id = SecondLevelCache.cache_store.read(cache_key)
8
8
  self.find(_id) rescue nil
9
9
  else
10
- record = self.where(uniq_key_name => value).first
11
- record.tap{|record| SecondLevelCache.cache_store.write(cache_uniq_key(value, uniq_key_name), record.id)} if record
10
+ record = self.where(where_values).first
11
+ record.tap{|record| SecondLevelCache.cache_store.write(cache_key, record.id)} if record
12
12
  end
13
13
  end
14
+
15
+ def fetch_by_uniq_keys!(where_values)
16
+ fetch_by_uniq_keys(where_values) || raise(::ActiveRecord::RecordNotFound)
17
+ end
18
+
19
+ def fetch_by_uniq_key(value, uniq_key_name)
20
+ # puts "[Deprecated] will remove in the future, use fetch_by_uniq_keys method instead."
21
+ fetch_by_uniq_keys(uniq_key_name => value)
22
+ end
14
23
 
15
24
  def fetch_by_uniq_key!(value, uniq_key_name)
25
+ # puts "[Deprecated] will remove in the future, use fetch_by_uniq_keys! method instead."
16
26
  fetch_by_uniq_key(value, uniq_key_name) || raise(::ActiveRecord::RecordNotFound)
17
27
  end
18
28
 
19
29
  private
20
-
21
- def cache_uniq_key(value, uniq_key_name)
22
- "uniq_key_#{self.name}_#{uniq_key_name}_#{value}"
30
+
31
+ def cache_uniq_key(where_values)
32
+ ext_key = where_values.collect { |k,v|
33
+ v = Digest::MD5.hexdigest(v) if v.size >= 32
34
+ [k,v].join("_")
35
+ }.join(",")
36
+ "uniq_key_#{self.name}_#{ext_key}"
23
37
  end
24
38
  end
25
39
  end
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module SecondLevelCache
3
- VERSION = "2.1.0.rc1"
3
+ VERSION = "2.1.0.rc2"
4
4
  end
@@ -32,7 +32,7 @@ Gem::Specification.new do |gem|
32
32
 
33
33
  gem.add_runtime_dependency "activesupport", ["> 4.0.0", "< 5.0"]
34
34
 
35
- gem.add_development_dependency "activerecord", ["> 4.0.0", "< 5.0"]
35
+ gem.add_runtime_dependency "activerecord", ["> 4.0.0", "< 5.0"]
36
36
  gem.add_development_dependency "sqlite3"
37
37
  gem.add_development_dependency "rake"
38
38
  gem.add_development_dependency "database_cleaner", "~> 1.2.0"
@@ -5,26 +5,43 @@ class ActiveRecord::FetchByUinqKeyTest < Minitest::Test
5
5
  def setup
6
6
  DatabaseCleaner[:active_record].start
7
7
  @user = User.create :name => 'hooopo', :email => 'hoooopo@gmail.com'
8
+ @post = Post.create :slug => "foobar", :topic_id => 2
9
+ end
10
+
11
+ def test_cache_uniq_key
12
+ assert_equal User.send(:cache_uniq_key, { :name => "hooopo" } ), "uniq_key_User_name_hooopo"
13
+ assert_equal User.send(:cache_uniq_key, { :foo => 1, :bar => 2 } ), "uniq_key_User_foo_1,bar_2"
14
+ long_val = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15
+ assert_equal User.send(:cache_uniq_key, { :foo => 1, :bar => long_val } ), "uniq_key_User_foo_1,bar_#{Digest::MD5.hexdigest(long_val)}"
8
16
  end
9
17
 
10
18
  def test_should_query_from_db_using_primary_key
11
- User.fetch_by_uniq_key(@user.name, :name)
19
+ Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
12
20
  $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'
21
+ Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
22
+ assert_equal $sql_logger.strip, 'SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1'
15
23
  end
16
24
 
17
25
  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
26
+ post = Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
27
+ assert_equal post, @post
20
28
  no_connection do
21
- User.fetch_by_uniq_key(@user.name, :name)
29
+ Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
22
30
  end
23
31
  end
24
32
 
25
33
  def test_should_fail_when_fetch_by_uniq_key_with_bang_method
26
34
  assert_raises(ActiveRecord::RecordNotFound) do
27
- User.fetch_by_uniq_key!(@user.name + "not_exist", :name)
35
+ Post.fetch_by_uniq_keys!(:topic_id => 2, :slug => "foobar1")
28
36
  end
37
+
38
+ assert_raises(ActiveRecord::RecordNotFound) do
39
+ User.fetch_by_uniq_key!("xxxxx", :name)
40
+ end
41
+ end
42
+
43
+ def test_should_work_with_fetch_by_uniq_key
44
+ user = User.fetch_by_uniq_key(@user.name, :name)
45
+ assert_equal user, @user
29
46
  end
30
47
  end
@@ -1,6 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  ActiveRecord::Base.connection.create_table(:posts, :force => true) do |t|
3
3
  t.text :body
4
+ t.string :slug
4
5
  t.integer :topic_id
5
6
  end
6
7
 
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.1.0.rc1
4
+ version: 2.1.0.rc2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-19 00:00:00.000000000 Z
12
+ date: 2014-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -44,7 +44,7 @@ dependencies:
44
44
  - - <
45
45
  - !ruby/object:Gem::Version
46
46
  version: '5.0'
47
- type: :development
47
+ type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  none: false