aub-cache_advance 1.0.1 → 1.0.2
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/cache_advance.gemspec +2 -2
- data/lib/cache_advance/cache_set.rb +2 -2
- data/lib/cache_advance/named_cache.rb +4 -4
- data/rails/init.rb +10 -3
- data/test/cache_mock.rb +15 -0
- data/test/cache_set_test.rb +3 -3
- data/test/named_cache_test.rb +121 -2
- data/test/test_helper.rb +1 -0
- metadata +2 -1
data/cache_advance.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
|
-
s.version = '1.0.
|
2
|
+
s.version = '1.0.2'
|
3
3
|
s.date = %q{2009-01-08}
|
4
4
|
|
5
5
|
s.name = %q{cache_advance}
|
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.authors = ['Aubrey Holland']
|
8
8
|
s.description = %q{A system for spiffy declarative caching}
|
9
9
|
s.email = %q{aubrey@patch.com}
|
10
|
-
s.files = %w(cache_advance.gemspec lib/cache_advance/active_record_sweeper.rb lib/cache_advance/cache_set.rb lib/cache_advance/mapper.rb lib/cache_advance/named_cache.rb lib/cache_advance/rails_cache.rb lib/cache_advance.rb rails/init.rb Rakefile README.textile test/active_record_sweeper_test.rb test/cache_set_test.rb test/mapper_test.rb test/named_cache_test.rb test/rails_cache_test.rb test/test_helper.rb TODO.textile)
|
10
|
+
s.files = %w(cache_advance.gemspec lib/cache_advance/active_record_sweeper.rb lib/cache_advance/cache_set.rb lib/cache_advance/mapper.rb lib/cache_advance/named_cache.rb lib/cache_advance/rails_cache.rb lib/cache_advance.rb rails/init.rb Rakefile README.textile test/active_record_sweeper_test.rb test/cache_mock.rb test/cache_set_test.rb test/mapper_test.rb test/named_cache_test.rb test/rails_cache_test.rb test/test_helper.rb TODO.textile)
|
11
11
|
s.homepage = %q{http://github.com/aub/cache_advance}
|
12
12
|
s.require_paths = ['lib']
|
13
13
|
s.rubygems_version = %q{1.2.0}
|
@@ -3,7 +3,7 @@ module CacheAdvance
|
|
3
3
|
attr_reader :named_caches
|
4
4
|
attr_reader :qualifiers
|
5
5
|
attr_reader :plugins
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :sweeper_type
|
7
7
|
attr_accessor :cache
|
8
8
|
|
9
9
|
def initialize
|
@@ -35,7 +35,7 @@ module CacheAdvance
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def create_sweepers
|
38
|
-
|
38
|
+
sweeper_type.initialize_observed(@named_caches.values.map { |c| c.expiration_types }.flatten.compact.uniq)
|
39
39
|
end
|
40
40
|
|
41
41
|
def expire_for_class(class_name)
|
@@ -2,10 +2,10 @@ module CacheAdvance
|
|
2
2
|
class NamedCache
|
3
3
|
STORED_KEY = 'STORED_CACHES'
|
4
4
|
|
5
|
-
def initialize(name, params,
|
5
|
+
def initialize(name, params, cache_set, cache)
|
6
6
|
@name = name.to_s
|
7
7
|
@params = params
|
8
|
-
@
|
8
|
+
@cache_set = cache_set
|
9
9
|
@cache = cache
|
10
10
|
end
|
11
11
|
|
@@ -14,7 +14,7 @@ module CacheAdvance
|
|
14
14
|
key << suffix.to_s
|
15
15
|
|
16
16
|
qualifiers.each do |q|
|
17
|
-
if (qualifier = @
|
17
|
+
if (qualifier = @cache_set.qualifiers[q])
|
18
18
|
this_one = qualifier.call(request)
|
19
19
|
key << this_one.to_s unless this_one.nil?
|
20
20
|
end
|
@@ -76,7 +76,7 @@ module CacheAdvance
|
|
76
76
|
protected
|
77
77
|
|
78
78
|
def call_plugins(method, key, request)
|
79
|
-
@
|
79
|
+
@cache_set.plugins.each { |p| p.send(method, @name, key, request) if p.respond_to?(method) }
|
80
80
|
end
|
81
81
|
|
82
82
|
def add_to_cached_keys_list(key)
|
data/rails/init.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'cache_advance'
|
2
|
-
require 'cache_advance/
|
2
|
+
require 'cache_advance/active_record_sweeper'
|
3
3
|
require 'cache_advance/rails_cache'
|
4
4
|
require 'config/caches'
|
5
5
|
require 'dispatcher'
|
6
6
|
|
7
|
+
# This is the helper method that can be used in rails views/controllers/helpers.
|
7
8
|
ActionController::Base.helper do
|
8
9
|
def cache_it(cache, options={}, &block)
|
9
10
|
CacheAdvance::Caches.apply(cache, request, options) do
|
@@ -12,13 +13,19 @@ ActionController::Base.helper do
|
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
16
|
+
# This will get called after the standard rails environment is initialized.
|
15
17
|
config.after_initialize do
|
16
|
-
|
18
|
+
|
19
|
+
# Setup the sweeper_type and cache as appropriate for Rails.
|
20
|
+
CacheAdvance::Caches.sweeper_type = CacheAdvance::ActiveRecordSweeper
|
17
21
|
CacheAdvance::Caches.cache = CacheAdvance::RailsCache.new
|
18
22
|
|
23
|
+
# This hooks the sweepers into the observer system and adds it to the list.
|
19
24
|
CacheAdvance::Caches.create_sweepers
|
20
|
-
ActiveRecord::Base.observers << CacheAdvance::
|
25
|
+
ActiveRecord::Base.observers << CacheAdvance::ActiveRecordSweeper
|
21
26
|
|
27
|
+
# In development mode, the models we observe get reloaded with each request. Using
|
28
|
+
# this hook allows us to reload the observer relationships each time as well.
|
22
29
|
ActionController::Dispatcher.to_prepare(:cache_advance_reload) do
|
23
30
|
CacheAdvance::ActiveRecordSweeper.instance.reload_sweeper
|
24
31
|
end
|
data/test/cache_mock.rb
ADDED
data/test/cache_set_test.rb
CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class Plugin; end
|
4
4
|
|
5
|
-
require 'cache_advance/
|
5
|
+
require 'cache_advance/active_record_sweeper'
|
6
6
|
|
7
7
|
class CacheSetTest < Test::Unit::TestCase
|
8
8
|
def setup
|
@@ -30,10 +30,10 @@ class CacheSetTest < Test::Unit::TestCase
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_should_pass_expiration_types_to_the_sweeper
|
33
|
-
@cache_set.
|
33
|
+
@cache_set.sweeper_type = CacheAdvance::ActiveRecordSweeper
|
34
34
|
@cache_set.add_named_cache(:kewl, { :expiration_types => [:publication, :article] })
|
35
35
|
@cache_set.add_named_cache(:howza, { :expiration_types => [:publication] })
|
36
|
-
CacheAdvance::
|
36
|
+
CacheAdvance::ActiveRecordSweeper.expects(:initialize_observed).with([:publication, :article])
|
37
37
|
@cache_set.create_sweepers
|
38
38
|
end
|
39
39
|
end
|
data/test/named_cache_test.rb
CHANGED
@@ -1,7 +1,126 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
3
|
class NamedCacheTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@request = mock
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_key_for_in_basic_case
|
10
|
+
create_named_cache
|
11
|
+
assert_equal 'test_cache', @named_cache.key_for(@request)
|
12
|
+
assert_equal 'test_cachesuf', @named_cache.key_for(@request, 'suf')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_key_with_qualifiers
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def create_named_cache(options={})
|
22
|
+
@name = options[:name] || 'test_cache'
|
23
|
+
@params = {}
|
24
|
+
@cache_set = CacheAdvance::CacheSet.new
|
25
|
+
@cache = CacheAdvance::CacheMock.new
|
26
|
+
@named_cache = CacheAdvance::NamedCache.new(@name, @params, @cache_set, @cache)
|
6
27
|
end
|
7
28
|
end
|
29
|
+
|
30
|
+
|
31
|
+
# module CacheAdvance
|
32
|
+
# class NamedCache
|
33
|
+
# STORED_KEY = 'STORED_CACHES'
|
34
|
+
#
|
35
|
+
# def initialize(name, params, configuration, cache)
|
36
|
+
# @name = name.to_s
|
37
|
+
# @params = params
|
38
|
+
# @configuration = configuration
|
39
|
+
# @cache = cache
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# def key_for(request, suffix='')
|
43
|
+
# key = @name.dup
|
44
|
+
# key << suffix.to_s
|
45
|
+
#
|
46
|
+
# qualifiers.each do |q|
|
47
|
+
# if (qualifier = @configuration.qualifiers[q])
|
48
|
+
# this_one = qualifier.call(request)
|
49
|
+
# key << this_one.to_s unless this_one.nil?
|
50
|
+
# end
|
51
|
+
# end if qualifiers
|
52
|
+
# key
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# def value_for(request, options, &block)
|
56
|
+
# key = key_for(request, options[:key])
|
57
|
+
#
|
58
|
+
# if (cache = @cache.read(key))
|
59
|
+
# call_plugins('after_read', key, request)
|
60
|
+
# return cache
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# call_plugins('before_write', key, request)
|
64
|
+
# result = block.call
|
65
|
+
# @cache.write(key, result, rails_options)
|
66
|
+
# call_plugins('after_write', key, request)
|
67
|
+
#
|
68
|
+
# add_to_cached_keys_list(key)
|
69
|
+
#
|
70
|
+
# result
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# def rails_options
|
74
|
+
# options = {}
|
75
|
+
# options[:expires_in] = expiration_time if expiration_time
|
76
|
+
# options
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# def expire_for(type)
|
80
|
+
# if expiration_types.include?(type)
|
81
|
+
# expire_all
|
82
|
+
# end
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# def expire_all
|
86
|
+
# if (data = @cache.read(@name + STORED_KEY))
|
87
|
+
# data = Array(Marshal.load(data))
|
88
|
+
# data.each { |key| @cache.delete(key) }
|
89
|
+
# else
|
90
|
+
# @cache.delete(@name)
|
91
|
+
# end
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# def expiration_types
|
95
|
+
# Array(@params[:expiration_types])
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# def expiration_time
|
99
|
+
# @params[:expiration_time]
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# def qualifiers
|
103
|
+
# Array(@params[:qualifiers])
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# protected
|
107
|
+
#
|
108
|
+
# def call_plugins(method, key, request)
|
109
|
+
# @configuration.plugins.each { |p| p.send(method, @name, key, request) if p.respond_to?(method) }
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# def add_to_cached_keys_list(key)
|
113
|
+
# unless expiration_types.blank? || key == @name
|
114
|
+
# if (data = @cache.read(@name + STORED_KEY))
|
115
|
+
# data = Array(Marshal.load(data))
|
116
|
+
# else
|
117
|
+
# data = []
|
118
|
+
# end
|
119
|
+
# unless data.include?(key)
|
120
|
+
# @cache.write(@name + STORED_KEY, Marshal.dump(data << key))
|
121
|
+
# end
|
122
|
+
# end
|
123
|
+
# end
|
124
|
+
# end
|
125
|
+
# end
|
126
|
+
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aub-cache_advance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aubrey Holland
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- Rakefile
|
34
34
|
- README.textile
|
35
35
|
- test/active_record_sweeper_test.rb
|
36
|
+
- test/cache_mock.rb
|
36
37
|
- test/cache_set_test.rb
|
37
38
|
- test/mapper_test.rb
|
38
39
|
- test/named_cache_test.rb
|