kasket 0.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/kasket.rb CHANGED
@@ -12,15 +12,17 @@ module Kasket
12
12
  CONFIGURATION = {:max_collection_size => 100}
13
13
 
14
14
  class Version
15
- MAJOR = 0
16
- MINOR = 9
17
- PATCH = 1
15
+ MAJOR = 1
16
+ MINOR = 0
17
+ PATCH = 0
18
18
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
19
19
  end
20
20
 
21
21
  module_function
22
22
 
23
23
  def setup(options = {})
24
+ return if ActiveRecord::Base.extended_by.member?(Kasket::ConfigurationMixin)
25
+
24
26
  CONFIGURATION[:max_collection_size] = options[:max_collection_size] if options[:max_collection_size]
25
27
 
26
28
  ActiveRecord::Base.extend(Kasket::ConfigurationMixin)
@@ -55,15 +55,19 @@ module Kasket
55
55
 
56
56
  def store_in_kasket(key, records)
57
57
  if records.size == 1
58
- Kasket.cache.write(key, records.first.instance_variable_get(:@attributes).dup)
59
- elsif records.size <= Kasket::CONFIGURATION[:max_collection_size]
60
- instance_keys = records.map do |record|
61
- instance_key = kasket_key_for_id(record.id)
62
- Kasket.cache.write(instance_key, record.instance_variable_get(:@attributes).dup)
63
- instance_key
58
+ if records.first.kasket_cacheable?
59
+ Kasket.cache.write(key, records.first.instance_variable_get(:@attributes).dup)
64
60
  end
61
+ elsif records.size <= Kasket::CONFIGURATION[:max_collection_size]
62
+ if records.all?(&:kasket_cacheable?)
63
+ instance_keys = records.map do |record|
64
+ instance_key = kasket_key_for_id(record.id)
65
+ Kasket.cache.write(instance_key, record.instance_variable_get(:@attributes).dup)
66
+ instance_key
67
+ end
65
68
 
66
- Kasket.cache.write(key, instance_keys) if key.is_a?(String)
69
+ Kasket.cache.write(key, instance_keys) if key.is_a?(String)
70
+ end
67
71
  end
68
72
  records
69
73
  end
@@ -26,8 +26,12 @@ module Kasket
26
26
  @kasket_key ||= new_record? ? nil : self.class.kasket_key_for_id(id)
27
27
  end
28
28
 
29
+ def kasket_cacheable?
30
+ true
31
+ end
32
+
29
33
  def store_in_kasket
30
- if !readonly? && kasket_key
34
+ if kasket_cacheable? && kasket_key
31
35
  Kasket.cache.write(kasket_key, @attributes.dup)
32
36
  end
33
37
  end
@@ -0,0 +1,58 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ class CacheableTest < ActiveSupport::TestCase
4
+ context "#store_in_kasket" do
5
+ should "only cache object that are kasket_cacheable?" do
6
+ post = Post.send(:instantiate, { 'id' => 1, 'title' => 'Hello' })
7
+
8
+ post.expects(:kasket_cacheable?).returns(true)
9
+ Kasket.cache.expects(:write).once
10
+ post.store_in_kasket
11
+
12
+ post.expects(:kasket_cacheable?).returns(false)
13
+ Kasket.cache.expects(:write).never
14
+ post.store_in_kasket
15
+ end
16
+ end
17
+
18
+ context "caching of results of find" do
19
+ setup do
20
+ @post_database_result = { 'id' => 1, 'title' => 'Hello' }
21
+ @post_records = [Post.send(:instantiate, @post_database_result)]
22
+ Post.stubs(:find_by_sql_without_kasket).returns(@post_records)
23
+
24
+ @comment_database_result = [{ 'id' => 1, 'body' => 'Hello' }, { 'id' => 2, 'body' => 'World' }]
25
+ @comment_records = @comment_database_result.map {|r| Comment.send(:instantiate, r)}
26
+ Comment.stubs(:find_by_sql_without_kasket).returns(@comment_records)
27
+ end
28
+
29
+ context "with just one result" do
30
+ should "write result in cache if it is kasket_cacheable?" do
31
+ Post.any_instance.expects(:kasket_cacheable?).returns(true)
32
+ Kasket.cache.expects(:write).once
33
+ Post.find(1)
34
+ end
35
+
36
+ should "not write result in cache if it is not kasket_cacheable?" do
37
+ Post.any_instance.expects(:kasket_cacheable?).returns(false)
38
+ Kasket.cache.expects(:write).never
39
+ Post.find(1)
40
+ end
41
+ end
42
+
43
+ context "with several results" do
44
+ should "write result in cache if all results are kasket_cacheable?" do
45
+ Comment.any_instance.stubs(:kasket_cacheable?).returns(true)
46
+ Kasket.cache.expects(:write).times(@comment_records.size + 1)
47
+ Comment.all(:conditions => {:post_id => 1})
48
+ end
49
+
50
+ should "not write result in cache if any of them are not kasket_cacheable?" do
51
+ @comment_records[0].expects(:kasket_cacheable?).returns(true)
52
+ @comment_records[1].expects(:kasket_cacheable?).returns(false)
53
+ Kasket.cache.expects(:write).never
54
+ Comment.all(:conditions => {:post_id => 1})
55
+ end
56
+ end
57
+ end
58
+ end
data/test/helper.rb CHANGED
@@ -4,6 +4,11 @@ require 'bundler'
4
4
  Bundler.setup
5
5
  Bundler.require(:default, :development)
6
6
 
7
+ if defined?(Debugger)
8
+ ::Debugger.start
9
+ ::Debugger.settings[:autoeval] = true if ::Debugger.respond_to?(:settings)
10
+ end
11
+
7
12
  require 'test/unit'
8
13
  require 'active_record/fixtures'
9
14
 
@@ -42,10 +47,15 @@ $LOAD_PATH.unshift(ActiveSupport::TestCase.fixture_path)
42
47
  module Rails
43
48
  module_function
44
49
  CACHE = ActiveSupport::Cache::MemoryStore.new
50
+ LOGGER = Logger.new(STDOUT)
45
51
 
46
52
  def cache
47
53
  CACHE
48
54
  end
55
+
56
+ def logger
57
+ LOGGER
58
+ end
49
59
  end
50
60
 
51
61
  require 'test_models'
@@ -37,7 +37,8 @@ class ReadMixinTest < ActiveSupport::TestCase
37
37
  assert_equal(@comment_database_result, stored_value.map {|key| Kasket.cache.read(key)})
38
38
 
39
39
  Comment.expects(:find_by_sql_without_kasket).never
40
- assert_equal(@comment_records, Comment.find_by_sql('SELECT * FROM `comments` WHERE (post_id = 1)'))
40
+ records = Comment.find_by_sql('SELECT * FROM `comments` WHERE (post_id = 1)')
41
+ assert_equal(@comment_records, records.sort {|c1, c2| c1.id <=> c2.id})
41
42
  end
42
43
 
43
44
  context "modifying results" do
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: 57
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
- - 0
8
- - 9
9
7
  - 1
10
- version: 0.9.1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
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: 2011-01-05 00:00:00 +01:00
19
+ date: 2011-04-26 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -108,7 +108,7 @@ dependencies:
108
108
  type: :development
109
109
  version_requirements: *id006
110
110
  - !ruby/object:Gem::Dependency
111
- name: sqlite3-ruby
111
+ name: sqlite3
112
112
  prerelease: false
113
113
  requirement: &id007 !ruby/object:Gem::Requirement
114
114
  none: false
@@ -142,6 +142,7 @@ files:
142
142
  - lib/kasket.rb
143
143
  - README.rdoc
144
144
  - test/cache_expiry_test.rb
145
+ - test/cacheable_test.rb
145
146
  - test/configuration_mixin_test.rb
146
147
  - test/database.yml
147
148
  - test/dirty_test.rb
@@ -185,12 +186,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
186
  requirements: []
186
187
 
187
188
  rubyforge_project:
188
- rubygems_version: 1.3.7
189
+ rubygems_version: 1.6.2
189
190
  signing_key:
190
191
  specification_version: 3
191
192
  summary: A write back caching layer on active record
192
193
  test_files:
193
194
  - test/cache_expiry_test.rb
195
+ - test/cacheable_test.rb
194
196
  - test/configuration_mixin_test.rb
195
197
  - test/database.yml
196
198
  - test/dirty_test.rb