cache_back 0.2.0 → 0.3.0
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.
- data/.gitignore +1 -0
- data/Rakefile +4 -2
- data/VERSION +1 -1
- data/lib/cache_back/configuration_mixin.rb +4 -1
- data/lib/cache_back/dirty_mixin.rb +16 -0
- data/lib/cache_back/read_mixin.rb +8 -2
- data/lib/cache_back/write_mixin.rb +4 -2
- data/lib/cache_back.rb +6 -1
- data/test/database.yml +7 -0
- data/test/dirty_test.rb +18 -0
- data/test/find_one_test.rb +30 -0
- data/test/fixtures/blogs.yml +2 -0
- data/test/fixtures/comments.yml +14 -0
- data/test/fixtures/posts.yml +11 -0
- data/test/helper.rb +66 -1
- data/test/schema.rb +35 -0
- metadata +22 -3
- data/test/test_cache_back.rb +0 -7
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -11,6 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/staugaard/cache_back"
|
12
12
|
gem.authors = ["Mick Staugaard"]
|
13
13
|
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
gem.add_development_dependency "mocha", ">= 0"
|
14
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
16
|
end
|
16
17
|
Jeweler::GemcutterTasks.new
|
@@ -21,7 +22,7 @@ end
|
|
21
22
|
require 'rake/testtask'
|
22
23
|
Rake::TestTask.new(:test) do |test|
|
23
24
|
test.libs << 'lib' << 'test'
|
24
|
-
test.pattern = 'test
|
25
|
+
test.pattern = 'test/**/*_test.rb'
|
25
26
|
test.verbose = true
|
26
27
|
end
|
27
28
|
|
@@ -29,7 +30,8 @@ begin
|
|
29
30
|
require 'rcov/rcovtask'
|
30
31
|
Rcov::RcovTask.new do |test|
|
31
32
|
test.libs << 'test'
|
32
|
-
test.pattern = 'test
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.rcov_opts << "--exclude \"test/*,gems/*,/Library/Ruby/*,config/*\" --rails"
|
33
35
|
test.verbose = true
|
34
36
|
end
|
35
37
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
@@ -3,6 +3,7 @@ require 'activesupport'
|
|
3
3
|
module CacheBack
|
4
4
|
autoload :ReadMixin, 'cache_back/read_mixin'
|
5
5
|
autoload :WriteMixin, 'cache_back/write_mixin'
|
6
|
+
autoload :DirtyMixin, 'cache_back/dirty_mixin'
|
6
7
|
|
7
8
|
module ConfigurationMixin
|
8
9
|
def cache_back_version(version)
|
@@ -34,7 +35,9 @@ module CacheBack
|
|
34
35
|
@cache_back_option = options
|
35
36
|
|
36
37
|
include WriteMixin unless instance_methods.include?('store_in_cache_back')
|
37
|
-
extend ReadMixin unless
|
38
|
+
extend ReadMixin unless methods.include?('find_one_with_cache_back')
|
39
|
+
extend DirtyMixin unless methods.include?('cache_back_dirty_methods')
|
38
40
|
end
|
41
|
+
|
39
42
|
end
|
40
43
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module CacheBack
|
2
|
+
module DirtyMixin
|
3
|
+
def cache_back_dirty_methods(*method_names)
|
4
|
+
method_names.each do |method|
|
5
|
+
unless method_defined?("without_cache_back_update_#{method}")
|
6
|
+
alias_method("without_cache_back_update_#{method}", method)
|
7
|
+
define_method(method) do |*args|
|
8
|
+
result = send("without_cache_back_update_#{method}", *args)
|
9
|
+
store_in_cache_back
|
10
|
+
result
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -8,9 +8,9 @@ module CacheBack
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def find_one_with_cache_back(id, options)
|
11
|
-
record = CacheBack.cache.read(cache_back_key_for(id))
|
11
|
+
record = cache_safe?(options) ? CacheBack.cache.read(cache_back_key_for(id)) : nil
|
12
12
|
|
13
|
-
|
13
|
+
if record.nil?
|
14
14
|
record = find_one_without_cache_back(id, options)
|
15
15
|
record.store_in_cache_back if record
|
16
16
|
end
|
@@ -18,5 +18,11 @@ module CacheBack
|
|
18
18
|
record
|
19
19
|
end
|
20
20
|
|
21
|
+
private
|
22
|
+
|
23
|
+
def cache_safe?(options)
|
24
|
+
!options.has_key?(:select)
|
25
|
+
end
|
26
|
+
|
21
27
|
end
|
22
28
|
end
|
@@ -12,7 +12,9 @@ module CacheBack
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def store_in_cache_back
|
15
|
-
|
15
|
+
if !readonly? && cache_back_key
|
16
|
+
CacheBack.cache.write(cache_back_key, shallow_clone, self.class.inherited_cache_back_options)
|
17
|
+
end
|
16
18
|
end
|
17
19
|
|
18
20
|
def remove_from_cache_back
|
@@ -20,7 +22,7 @@ module CacheBack
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def self.included(model_class)
|
23
|
-
model_class.after_save :store_in_cache_back
|
25
|
+
#model_class.after_save :store_in_cache_back
|
24
26
|
model_class.after_destroy :remove_from_cache_back
|
25
27
|
end
|
26
28
|
end
|
data/lib/cache_back.rb
CHANGED
@@ -13,4 +13,9 @@ module CacheBack
|
|
13
13
|
end
|
14
14
|
|
15
15
|
ActiveRecord::Base.extend(CacheBack::ConfigurationMixin)
|
16
|
-
|
16
|
+
begin
|
17
|
+
ActionController::Dispatcher.middleware.use(CacheBack::Middleware)
|
18
|
+
rescue NameError => e
|
19
|
+
|
20
|
+
end
|
21
|
+
|
data/test/database.yml
ADDED
data/test/dirty_test.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class DirtyTest < ActiveSupport::TestCase
|
4
|
+
fixtures :blogs, :posts
|
5
|
+
|
6
|
+
Post.has_cache_back
|
7
|
+
Post.cache_back_dirty_methods :make_dirty!
|
8
|
+
|
9
|
+
should "update the cache when a dirty method is called" do
|
10
|
+
post = Post.first
|
11
|
+
|
12
|
+
pots = Post.find(post.id)
|
13
|
+
assert(Rails.cache.read(post.cache_back_key))
|
14
|
+
|
15
|
+
CacheBack.cache.expects(:write)
|
16
|
+
post.make_dirty!
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class FindOneTest < ActiveSupport::TestCase
|
4
|
+
fixtures :blogs, :posts
|
5
|
+
|
6
|
+
Post.has_cache_back
|
7
|
+
|
8
|
+
should "cache find(id) calls" do
|
9
|
+
post = Post.first
|
10
|
+
assert_nil(Rails.cache.read(post.cache_back_key))
|
11
|
+
Post.find(post.id)
|
12
|
+
assert(Rails.cache.read(post.cache_back_key))
|
13
|
+
Post.connection.expects(:select_all).never
|
14
|
+
Post.find(post.id)
|
15
|
+
end
|
16
|
+
|
17
|
+
should "not use cache when using the :select option" do
|
18
|
+
post = Post.first
|
19
|
+
assert_nil(Rails.cache.read(post.cache_back_key))
|
20
|
+
|
21
|
+
Post.find(post.id, :select => 'title')
|
22
|
+
assert_nil(Rails.cache.read(post.cache_back_key))
|
23
|
+
|
24
|
+
Post.find(post.id)
|
25
|
+
assert(Rails.cache.read(post.cache_back_key))
|
26
|
+
|
27
|
+
CacheBack.cache.expects(:read).never
|
28
|
+
Post.find(post.id, :select => 'title')
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
few_comments_1:
|
2
|
+
post: has_two_comments
|
3
|
+
body: what ever body 1
|
4
|
+
|
5
|
+
few_comments_2:
|
6
|
+
post: has_two_comments
|
7
|
+
body: what ever body 2
|
8
|
+
|
9
|
+
<% (1..10000).each do |i| %>
|
10
|
+
many_comments_<%= i %>:
|
11
|
+
post: has_many_comments
|
12
|
+
body: what ever body <%= i %>
|
13
|
+
<% end %>
|
14
|
+
|
data/test/helper.rb
CHANGED
@@ -1,10 +1,75 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
|
+
require 'mocha'
|
3
4
|
require 'shoulda'
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_record'
|
7
|
+
require 'active_record/fixtures'
|
8
|
+
|
9
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
10
|
+
ActiveRecord::Base.establish_connection('test')
|
11
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
12
|
+
|
13
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
4
14
|
|
5
15
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
16
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
17
|
require 'cache_back'
|
8
18
|
|
9
|
-
class
|
19
|
+
class ActiveSupport::TestCase
|
20
|
+
include ActiveRecord::TestFixtures
|
21
|
+
|
22
|
+
fixtures :all
|
23
|
+
|
24
|
+
setup :clear_cache
|
25
|
+
|
26
|
+
def create_fixtures(*table_names)
|
27
|
+
if block_given?
|
28
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
|
29
|
+
else
|
30
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Turn off transactional fixtures if you're working with MyISAM tables in MySQL
|
35
|
+
self.use_transactional_fixtures = true
|
36
|
+
|
37
|
+
# Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
|
38
|
+
self.use_instantiated_fixtures = false
|
39
|
+
|
40
|
+
# Add more helper methods to be used by all tests here...
|
41
|
+
def clear_cache
|
42
|
+
CacheBack.cache.reset!
|
43
|
+
Rails.cache.clear
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
ActiveSupport::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
48
|
+
$LOAD_PATH.unshift(ActiveSupport::TestCase.fixture_path)
|
49
|
+
|
50
|
+
module Rails
|
51
|
+
module_function
|
52
|
+
CACHE = ActiveSupport::Cache::MemoryStore.new
|
53
|
+
|
54
|
+
def cache
|
55
|
+
CACHE
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Comment < ActiveRecord::Base
|
60
|
+
belongs_to :post
|
61
|
+
end
|
62
|
+
|
63
|
+
class Post < ActiveRecord::Base
|
64
|
+
belongs_to :blog
|
65
|
+
has_many :comments
|
66
|
+
|
67
|
+
def make_dirty!
|
68
|
+
self.updated_at = Time.now
|
69
|
+
self.connection.execute("UPDATE posts SET updated_at = '#{updated_at.utc.to_s(:db)}' WHERE id = #{id}")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Blog < ActiveRecord::Base
|
74
|
+
has_many :posts
|
10
75
|
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# This file is auto-generated from the current state of the database. Instead of editing this file,
|
2
|
+
# please use the migrations feature of Active Record to incrementally modify your database, and
|
3
|
+
# then regenerate this schema definition.
|
4
|
+
#
|
5
|
+
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
|
6
|
+
# to create the application database on another system, you should be using db:schema:load, not running
|
7
|
+
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
8
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
9
|
+
#
|
10
|
+
# It's strongly recommended to check this file into your version control system.
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define(:version => 1) do
|
13
|
+
|
14
|
+
create_table "comments", :force => true do |t|
|
15
|
+
t.text "body"
|
16
|
+
t.integer "post_id"
|
17
|
+
t.datetime "created_at"
|
18
|
+
t.datetime "updated_at"
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table "posts", :force => true do |t|
|
22
|
+
t.string "title"
|
23
|
+
t.integer "blog_id"
|
24
|
+
t.string "comments_cache_key"
|
25
|
+
t.datetime "created_at"
|
26
|
+
t.datetime "updated_at"
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table "blogs", :force => true do |t|
|
30
|
+
t.string "name"
|
31
|
+
t.datetime "created_at"
|
32
|
+
t.datetime "updated_at"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_back
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mick Staugaard
|
@@ -22,6 +22,16 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "0"
|
24
24
|
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
25
35
|
description: A rewrite of cache money
|
26
36
|
email: mick@staugaard.com
|
27
37
|
executables: []
|
@@ -41,11 +51,18 @@ files:
|
|
41
51
|
- lib/cache_back.rb
|
42
52
|
- lib/cache_back/cache.rb
|
43
53
|
- lib/cache_back/configuration_mixin.rb
|
54
|
+
- lib/cache_back/dirty_mixin.rb
|
44
55
|
- lib/cache_back/middleware.rb
|
45
56
|
- lib/cache_back/read_mixin.rb
|
46
57
|
- lib/cache_back/write_mixin.rb
|
58
|
+
- test/database.yml
|
59
|
+
- test/dirty_test.rb
|
60
|
+
- test/find_one_test.rb
|
61
|
+
- test/fixtures/blogs.yml
|
62
|
+
- test/fixtures/comments.yml
|
63
|
+
- test/fixtures/posts.yml
|
47
64
|
- test/helper.rb
|
48
|
-
- test/
|
65
|
+
- test/schema.rb
|
49
66
|
has_rdoc: true
|
50
67
|
homepage: http://github.com/staugaard/cache_back
|
51
68
|
licenses: []
|
@@ -75,5 +92,7 @@ signing_key:
|
|
75
92
|
specification_version: 3
|
76
93
|
summary: A write back caching layer on active record
|
77
94
|
test_files:
|
95
|
+
- test/dirty_test.rb
|
96
|
+
- test/find_one_test.rb
|
78
97
|
- test/helper.rb
|
79
|
-
- test/
|
98
|
+
- test/schema.rb
|