kasket 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/kasket.gemspec +2 -2
- data/lib/kasket.rb +12 -3
- data/lib/kasket/read_mixin.rb +9 -8
- data/lib/kasket/write_mixin.rb +7 -6
- data/test/cache_expiry_test.rb +15 -15
- data/test/dirty_test.rb +2 -2
- data/test/find_one_test.rb +10 -10
- data/test/find_some_test.rb +6 -6
- data/test/helper.rb +1 -1
- data/test/read_mixin_test.rb +6 -6
- metadata +4 -4
data/kasket.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{kasket}
|
8
|
-
s.version = "0.8.
|
8
|
+
s.version = "0.8.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mick Staugaard", "Eric Chapweske"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-31}
|
13
13
|
s.description = %q{puts a cap on your queries}
|
14
14
|
s.email = %q{mick@staugaard.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/kasket.rb
CHANGED
@@ -14,7 +14,7 @@ module Kasket
|
|
14
14
|
class Version
|
15
15
|
MAJOR = 0
|
16
16
|
MINOR = 8
|
17
|
-
PATCH =
|
17
|
+
PATCH = 2
|
18
18
|
STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
|
19
19
|
end
|
20
20
|
|
@@ -29,9 +29,18 @@ module Kasket
|
|
29
29
|
ActiveRecord::Associations::HasOneThroughAssociation.send(:include, Kasket::ReloadAssociationMixin)
|
30
30
|
end
|
31
31
|
|
32
|
+
def self.cache_store=(options)
|
33
|
+
@cache_store = ActiveSupport::Cache.lookup_store(options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.cache
|
37
|
+
@cache_store ||= Rails.cache
|
38
|
+
end
|
39
|
+
|
32
40
|
def clear_local
|
33
|
-
if
|
34
|
-
|
41
|
+
if Kasket.cache.respond_to?(:with_local_cache)
|
42
|
+
Kasket.cache.send(:local_cache).try(:clear)
|
35
43
|
end
|
36
44
|
end
|
37
45
|
end
|
46
|
+
|
data/lib/kasket/read_mixin.rb
CHANGED
@@ -13,7 +13,7 @@ module Kasket
|
|
13
13
|
if query[:key].is_a?(Array)
|
14
14
|
find_by_sql_with_kasket_on_id_array(sql, query)
|
15
15
|
else
|
16
|
-
if value =
|
16
|
+
if value = Kasket.cache.read(query[:key])
|
17
17
|
Array.wrap(value).collect { |record| instantiate(record.dup) }
|
18
18
|
else
|
19
19
|
store_in_kasket(query[:key], find_by_sql_without_kasket(sql))
|
@@ -25,7 +25,7 @@ module Kasket
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def find_by_sql_with_kasket_on_id_array(sql, query)
|
28
|
-
key_value_map =
|
28
|
+
key_value_map = Kasket.cache.read_multi(*query[:key])
|
29
29
|
missing_ids = []
|
30
30
|
|
31
31
|
query[:key].each do |key|
|
@@ -50,18 +50,19 @@ module Kasket
|
|
50
50
|
|
51
51
|
def store_in_kasket(key, records)
|
52
52
|
if records.size == 1
|
53
|
-
|
53
|
+
Kasket.cache.write(key, records.first.instance_variable_get(:@attributes).dup)
|
54
54
|
elsif records.size <= Kasket::CONFIGURATION[:max_collection_size]
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
instance_keys = records.map do |record|
|
56
|
+
instance_key = kasket_key_for_id(record.id)
|
57
|
+
Kasket.cache.write(instance_key, record.instance_variable_get(:@attributes).dup)
|
58
|
+
instance_key
|
59
59
|
end
|
60
60
|
|
61
|
-
|
61
|
+
Kasket.cache.write(key, instance_keys) if key.is_a?(String)
|
62
62
|
end
|
63
63
|
records
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
data/lib/kasket/write_mixin.rb
CHANGED
@@ -4,7 +4,7 @@ module Kasket
|
|
4
4
|
module ClassMethods
|
5
5
|
def remove_from_kasket(ids)
|
6
6
|
Array(ids).each do |id|
|
7
|
-
|
7
|
+
Kasket.cache.delete(kasket_key_for_id(id))
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -14,8 +14,8 @@ module Kasket
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def transaction_with_kasket_disabled(*args)
|
17
|
-
without_kasket do
|
18
|
-
transaction_without_kasket_disabled(*args) { yield }
|
17
|
+
without_kasket do
|
18
|
+
transaction_without_kasket_disabled(*args) { yield }
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -27,7 +27,7 @@ module Kasket
|
|
27
27
|
|
28
28
|
def store_in_kasket
|
29
29
|
if !readonly? && kasket_key
|
30
|
-
|
30
|
+
Kasket.cache.write(kasket_key, @attributes.dup)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -54,7 +54,7 @@ module Kasket
|
|
54
54
|
|
55
55
|
def clear_kasket_indices
|
56
56
|
kasket_keys.each do |key|
|
57
|
-
|
57
|
+
Kasket.cache.delete(key)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -72,7 +72,7 @@ module Kasket
|
|
72
72
|
model_class.after_destroy :clear_kasket_indices
|
73
73
|
|
74
74
|
model_class.alias_method_chain :reload, :kasket_clearing
|
75
|
-
|
75
|
+
|
76
76
|
|
77
77
|
class << model_class
|
78
78
|
alias_method_chain :transaction, :kasket_disabled
|
@@ -81,3 +81,4 @@ module Kasket
|
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
84
|
+
|
data/test/cache_expiry_test.rb
CHANGED
@@ -8,20 +8,20 @@ class CacheExpiryTest < ActiveSupport::TestCase
|
|
8
8
|
post = Post.first
|
9
9
|
@post = Post.find(post.id)
|
10
10
|
|
11
|
-
assert(
|
11
|
+
assert(Kasket.cache.read(@post.kasket_key))
|
12
12
|
end
|
13
13
|
|
14
14
|
should "be removed from cache when deleted" do
|
15
15
|
@post.destroy
|
16
|
-
assert_nil(
|
16
|
+
assert_nil(Kasket.cache.read(@post.kasket_key))
|
17
17
|
end
|
18
18
|
|
19
19
|
should "clear all indices for instance when deleted" do
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "id=#{@post.id}")
|
21
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "title='#{@post.title}'")
|
22
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "title='#{@post.title}'/first")
|
23
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "blog_id=#{@post.blog_id}/id=#{@post.id}")
|
24
|
+
Kasket.cache.expects(:delete).never
|
25
25
|
|
26
26
|
@post.destroy
|
27
27
|
end
|
@@ -29,17 +29,17 @@ class CacheExpiryTest < ActiveSupport::TestCase
|
|
29
29
|
should "be removed from cache when updated" do
|
30
30
|
@post.title = "new_title"
|
31
31
|
@post.save
|
32
|
-
assert_nil(
|
32
|
+
assert_nil(Kasket.cache.read(@post.kasket_key))
|
33
33
|
end
|
34
34
|
|
35
35
|
should "clear all indices for instance when updated" do
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "id=#{@post.id}")
|
37
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "title='#{@post.title}'")
|
38
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "title='#{@post.title}'/first")
|
39
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "title='new_title'")
|
40
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "title='new_title'/first")
|
41
|
+
Kasket.cache.expects(:delete).with(Post.kasket_key_prefix + "blog_id=#{@post.blog_id}/id=#{@post.id}")
|
42
|
+
Kasket.cache.expects(:delete).never
|
43
43
|
|
44
44
|
@post.title = "new_title"
|
45
45
|
@post.save
|
data/test/dirty_test.rb
CHANGED
@@ -7,10 +7,10 @@ class DirtyTest < ActiveSupport::TestCase
|
|
7
7
|
post = Post.first
|
8
8
|
|
9
9
|
Post.cache { pots = Post.find(post.id) }
|
10
|
-
assert(
|
10
|
+
assert(Kasket.cache.read(post.kasket_key))
|
11
11
|
|
12
12
|
post.make_dirty!
|
13
13
|
|
14
|
-
assert_nil(
|
14
|
+
assert_nil(Kasket.cache.read(post.kasket_key))
|
15
15
|
end
|
16
16
|
end
|
data/test/find_one_test.rb
CHANGED
@@ -5,36 +5,36 @@ class FindOneTest < ActiveSupport::TestCase
|
|
5
5
|
|
6
6
|
should "cache find(id) calls" do
|
7
7
|
post = Post.first
|
8
|
-
|
8
|
+
Kasket.cache.write(post.kasket_key, nil)
|
9
9
|
assert_equal(post, Post.find(post.id))
|
10
|
-
assert(
|
10
|
+
assert(Kasket.cache.read(post.kasket_key))
|
11
11
|
Post.connection.expects(:select_all).never
|
12
12
|
assert_equal(post, Post.find(post.id))
|
13
13
|
end
|
14
14
|
|
15
15
|
should "only cache on indexed attributes" do
|
16
|
-
|
16
|
+
Kasket.cache.expects(:read).twice
|
17
17
|
Post.find_by_id(1)
|
18
18
|
Post.find_by_id(1, :conditions => {:blog_id => 2})
|
19
19
|
|
20
|
-
|
20
|
+
Kasket.cache.expects(:read).never
|
21
21
|
Post.first :conditions => {:blog_id => 2}
|
22
22
|
end
|
23
23
|
|
24
24
|
should "not use cache when using the :select option" do
|
25
25
|
post = Post.first
|
26
|
-
assert_nil(
|
26
|
+
assert_nil(Kasket.cache.read(post.kasket_key))
|
27
27
|
|
28
28
|
Post.find(post.id, :select => 'title')
|
29
|
-
assert_nil(
|
29
|
+
assert_nil(Kasket.cache.read(post.kasket_key))
|
30
30
|
|
31
31
|
Post.find(post.id)
|
32
|
-
assert(
|
32
|
+
assert(Kasket.cache.read(post.kasket_key))
|
33
33
|
|
34
|
-
|
34
|
+
Kasket.cache.expects(:read)
|
35
35
|
Post.find(post.id, :select => nil)
|
36
36
|
|
37
|
-
|
37
|
+
Kasket.cache.expects(:read).never
|
38
38
|
Post.find(post.id, :select => 'title')
|
39
39
|
end
|
40
40
|
|
@@ -42,7 +42,7 @@ class FindOneTest < ActiveSupport::TestCase
|
|
42
42
|
post = Post.find(Post.first.id)
|
43
43
|
other_blog = Blog.first(:conditions => "id != #{post.blog_id}")
|
44
44
|
|
45
|
-
assert(
|
45
|
+
assert(Kasket.cache.read(post.kasket_key))
|
46
46
|
|
47
47
|
assert_raise(ActiveRecord::RecordNotFound) do
|
48
48
|
other_blog.posts.find(post.id)
|
data/test/find_some_test.rb
CHANGED
@@ -7,13 +7,13 @@ class FindSomeTest < ActiveSupport::TestCase
|
|
7
7
|
post1 = Post.first
|
8
8
|
post2 = Post.last
|
9
9
|
|
10
|
-
assert_nil(
|
11
|
-
assert_nil(
|
10
|
+
assert_nil(Kasket.cache.read(post1.kasket_key))
|
11
|
+
assert_nil(Kasket.cache.read(post2.kasket_key))
|
12
12
|
|
13
13
|
Post.find(post1.id, post2.id)
|
14
14
|
|
15
|
-
assert(
|
16
|
-
assert(
|
15
|
+
assert(Kasket.cache.read(post1.kasket_key))
|
16
|
+
assert(Kasket.cache.read(post2.kasket_key))
|
17
17
|
Post.connection.expects(:select_all).never
|
18
18
|
Post.find(post1.id, post2.id)
|
19
19
|
end
|
@@ -22,8 +22,8 @@ class FindSomeTest < ActiveSupport::TestCase
|
|
22
22
|
post1 = Post.first
|
23
23
|
post2 = Post.last
|
24
24
|
assert_equal(post1, Post.find(post1.id))
|
25
|
-
assert(
|
26
|
-
assert_nil(
|
25
|
+
assert(Kasket.cache.read(post1.kasket_key))
|
26
|
+
assert_nil(Kasket.cache.read(post2.kasket_key))
|
27
27
|
|
28
28
|
Post.expects(:find_by_sql_without_kasket).with("SELECT * FROM \"posts\" WHERE (\"posts\".\"id\" = #{post2.id}) ").returns([post2])
|
29
29
|
found_posts = Post.find(post1.id, post2.id)
|
data/test/helper.rb
CHANGED
data/test/read_mixin_test.rb
CHANGED
@@ -4,31 +4,31 @@ class ReadMixinTest < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
context "find by sql with kasket" do
|
6
6
|
setup do
|
7
|
-
@database_results = [ { 'id' => 1, 'title' => 'Hello' }
|
7
|
+
@database_results = [ { 'id' => 1, 'title' => 'Hello' } ]
|
8
8
|
@records = @database_results.map { |r| Post.send(:instantiate, r) }
|
9
9
|
Post.stubs(:find_by_sql_without_kasket).returns(@records)
|
10
10
|
end
|
11
11
|
|
12
12
|
should "handle unsupported sql" do
|
13
|
-
|
14
|
-
|
13
|
+
Kasket.cache.expects(:read).never
|
14
|
+
Kasket.cache.expects(:write).never
|
15
15
|
assert_equal @records, Post.find_by_sql_with_kasket('select unsupported sql statement')
|
16
16
|
end
|
17
17
|
|
18
18
|
should "read results" do
|
19
|
-
|
19
|
+
Kasket.cache.write("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1", @database_results.first)
|
20
20
|
assert_equal [ @records.first ], Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)')
|
21
21
|
end
|
22
22
|
|
23
23
|
should "store results in kasket" do
|
24
24
|
Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)')
|
25
25
|
|
26
|
-
assert_equal @database_results.first,
|
26
|
+
assert_equal @database_results.first, Kasket.cache.read("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1")
|
27
27
|
end
|
28
28
|
|
29
29
|
context "modifying results" do
|
30
30
|
setup do
|
31
|
-
|
31
|
+
Kasket.cache.write("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1", @database_results.first)
|
32
32
|
@record = Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)').first
|
33
33
|
@record.instance_variable_get(:@attributes)['id'] = 3
|
34
34
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kasket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 59
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 8
|
9
|
-
-
|
10
|
-
version: 0.8.
|
9
|
+
- 2
|
10
|
+
version: 0.8.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mick Staugaard
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-08-
|
19
|
+
date: 2010-08-31 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|