cache-machine 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +1 -0
- data/README.rdoc +48 -2
- data/VERSION +1 -1
- data/cache-machine.gemspec +4 -3
- data/lib/cache_machine/cache.rb +13 -5
- data/lib/cache_machine/cache/map.rb +8 -5
- data/spec/spec_helper.rb +1 -0
- metadata +23 -9
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -17,13 +17,13 @@ Cache Machine depends only on the Rails.cache API and is thereby library agnosti
|
|
17
17
|
= Usage
|
18
18
|
|
19
19
|
# Fetch cache of venues collection on model_instance.
|
20
|
-
@neighborhood.fetch_cache_of(:venues) { @neighborhood.venues
|
20
|
+
@neighborhood.fetch_cache_of(:venues) { @neighborhood.venues }
|
21
21
|
|
22
22
|
# Specify format.
|
23
23
|
@neighborhood.fetch_cache_of(:venues, :format => :json) { @neighborhood.venues.to_json }
|
24
24
|
|
25
25
|
# Paginated content.
|
26
|
-
@neighborhood.fetch_cache_of(:venues, :
|
26
|
+
@neighborhood.fetch_cache_of(:venues, :page => 2) { @neighborhood.venues.paginate(:page => 2) }
|
27
27
|
|
28
28
|
In you target model define <b>cache map</b>:
|
29
29
|
|
@@ -81,6 +81,18 @@ Suppose you need custom timestamp value.
|
|
81
81
|
TwitterApi.fetch_tweets_for @lady_gaga
|
82
82
|
end
|
83
83
|
|
84
|
+
Note what timestamp declarations work in object scope. Lets take an example:
|
85
|
+
|
86
|
+
class LadyGagaPerformer < ActiveRecord::Base
|
87
|
+
define_timestamp (:tweets_timestamp) { tweets.last.updated_at.to_i }
|
88
|
+
|
89
|
+
has_many :tweets
|
90
|
+
end
|
91
|
+
|
92
|
+
class Tweet < ActiveRecord::Base
|
93
|
+
belongs_to :lady_gaga_performer
|
94
|
+
end
|
95
|
+
|
84
96
|
=== Using methods as timestamps
|
85
97
|
Suppose you have your own really custom cache key.
|
86
98
|
|
@@ -112,6 +124,40 @@ Want to see collection timestamp?
|
|
112
124
|
# For timestamps.
|
113
125
|
@lady_gaga.reset_timestamp_of :events
|
114
126
|
|
127
|
+
== Cache formats
|
128
|
+
Cache Machine invalidates cache using a couple of keys with the different formats.
|
129
|
+
Default formats are:
|
130
|
+
- EHTML
|
131
|
+
- HTML
|
132
|
+
- JSON
|
133
|
+
- XML
|
134
|
+
|
135
|
+
This means you call 5 times for cache invalidation (1 time without specifying format) with different keys. Sometimes it is too much. Cache machine allows you to set your own formats. Just place in your environment config or in initializer the following:
|
136
|
+
CacheMachine::Cache.formats = [:doc, :pdf]
|
137
|
+
|
138
|
+
Or if you do not want to use formats at all:
|
139
|
+
CacheMachine::Cache.formats = nil
|
140
|
+
|
141
|
+
Then use:
|
142
|
+
|
143
|
+
@lady_gaga.fetch_cache_of(:new_songs, :format => :doc) { "LaLaLa".to_doc }
|
144
|
+
@lady_gaga.fetch_cache_of(:new_songs, :format => :pdf) { "GaGaGa".to_pdf }
|
145
|
+
|
146
|
+
Cache Machine will invalidate cache for each format you specified in config.
|
147
|
+
|
148
|
+
== Working with paginated content
|
149
|
+
Suppose you installed WillPaginate gem and want to cache each page with fetched results separately.
|
150
|
+
class TweetsController < ApplicationController
|
151
|
+
|
152
|
+
def index
|
153
|
+
@tweets = @lady_gaga.fetch_cache_of(:tweets, :page => params[:page]) do
|
154
|
+
Tweet.all.paginate(:page => params[:page])
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
Cache Machine will use <tt>:page</tt> as a part of cache key and will invalidate each page on any change in associated collection.
|
160
|
+
|
115
161
|
== ActionView helper
|
116
162
|
From examples above:
|
117
163
|
<%= cache_for @lady_gaga, :updoming_events do %>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.8
|
data/cache-machine.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{cache-machine}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["
|
12
|
-
s.date = %q{2011-
|
11
|
+
s.authors = ["Sergei Zinin", "Kevin Goslar"]
|
12
|
+
s.date = %q{2011-09-11}
|
13
13
|
s.description = %q{A Ruby on Rails framework to support cache management based on explicitely modeled caching dependencies.}
|
14
14
|
s.email = %q{kgoslar@partyearth.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
|
29
29
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
30
30
|
s.add_runtime_dependency(%q<rails>, [">= 0"])
|
31
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 0"])
|
31
32
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
32
33
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
|
33
34
|
else
|
data/lib/cache_machine/cache.rb
CHANGED
@@ -4,8 +4,16 @@ module CacheMachine
|
|
4
4
|
module Cache
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
# Supported cache formats.
|
8
|
-
|
7
|
+
# Supported by default cache formats.
|
8
|
+
@formats = [nil, :ehtml, :html, :json, :xml]
|
9
|
+
|
10
|
+
def self.formats
|
11
|
+
@formats
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.formats= formats
|
15
|
+
@formats = [nil] | [*formats]
|
16
|
+
end
|
9
17
|
|
10
18
|
included do
|
11
19
|
after_save { self.class.reset_timestamps }
|
@@ -24,7 +32,7 @@ module CacheMachine
|
|
24
32
|
# acts_as_cache_machine_for :cats => :cat_ids
|
25
33
|
#
|
26
34
|
# # Cache and expire dependent collections (_mouse_ change invalidates all other collection caches by chain)
|
27
|
-
# acts_as_cache_machine_for :mouses => :cats, :cats => [:
|
35
|
+
# acts_as_cache_machine_for :mouses => :cats, :cats => [:dogs, :bears], :dogs, :bears
|
28
36
|
def acts_as_cache_machine_for *associations
|
29
37
|
include CacheMachine::Cache::Map
|
30
38
|
cache_associated(associations)
|
@@ -48,12 +56,12 @@ module CacheMachine
|
|
48
56
|
# Resets timestamp of class collection.
|
49
57
|
def reset_timestamp format = nil
|
50
58
|
cache_key = timestamp_key format
|
51
|
-
CacheMachine::Logger.info "CACHE_MACHINE
|
59
|
+
CacheMachine::Logger.info "CACHE_MACHINE (reset_timestamp): deleting '#{cache_key}'."
|
52
60
|
Rails.cache.delete(cache_key)
|
53
61
|
end
|
54
62
|
|
55
63
|
def reset_timestamps
|
56
|
-
|
64
|
+
CacheMachine::Cache.formats.each { |format| reset_timestamp format }
|
57
65
|
end
|
58
66
|
end
|
59
67
|
end
|
@@ -27,7 +27,7 @@ module CacheMachine
|
|
27
27
|
|
28
28
|
define_method timestamp_name do
|
29
29
|
fetch_cache_of(timestamp_key_of(timestamp_name), options) do
|
30
|
-
CacheMachine::Logger.info "CACHE_MACHINE
|
30
|
+
CacheMachine::Logger.info "CACHE_MACHINE (define_timestamp): deleting old timestamp '#{timestamp_name}'."
|
31
31
|
delete_cache_of timestamp_name # Case when cache expired by time.
|
32
32
|
Time.now.to_i.to_s
|
33
33
|
end
|
@@ -127,6 +127,7 @@ module CacheMachine
|
|
127
127
|
options[:expires_in]
|
128
128
|
end
|
129
129
|
|
130
|
+
CacheMachine::Logger.info "CACHE_MACHINE (fetch_cache_of): reading '#{cache_key}'."
|
130
131
|
Rails.cache.fetch(cache_key, :expires_in => expires_in, &block)
|
131
132
|
end
|
132
133
|
|
@@ -140,10 +141,10 @@ module CacheMachine
|
|
140
141
|
|
141
142
|
# Deletes cache of only +_member+ ignoring cache map.
|
142
143
|
def delete_cache_of_only _member
|
143
|
-
CacheMachine::Cache
|
144
|
+
CacheMachine::Cache.formats.each do |cache_format|
|
144
145
|
page_nr = 0; begin
|
145
146
|
cache_key = cache_key_of(_member, {:format => cache_format, :page => page_nr += 1})
|
146
|
-
CacheMachine::Logger.info "CACHE_MACHINE
|
147
|
+
CacheMachine::Logger.info "CACHE_MACHINE (delete_cache_of_only): deleting '#{cache_key}'"
|
147
148
|
end while Rails.cache.delete(cache_key)
|
148
149
|
end
|
149
150
|
reset_timestamp_of _member
|
@@ -156,7 +157,9 @@ module CacheMachine
|
|
156
157
|
|
157
158
|
# Returns timestamp of +anything+ from memcached.
|
158
159
|
def timestamp_of anything
|
159
|
-
|
160
|
+
key = timestamp_key_of anything
|
161
|
+
CacheMachine::Logger.info "CACHE_MACHINE (timestamp_of): reading timestamp '#{key}'."
|
162
|
+
Rails.cache.fetch(key) { Time.now.to_i.to_s }
|
160
163
|
end
|
161
164
|
|
162
165
|
# Returns cache key of +anything+ with timestamp attached.
|
@@ -167,7 +170,7 @@ module CacheMachine
|
|
167
170
|
# Deletes cache of +anything+ from memory.
|
168
171
|
def reset_timestamp_of anything
|
169
172
|
cache_key = timestamp_key_of anything
|
170
|
-
CacheMachine::Logger.info "CACHE_MACHINE
|
173
|
+
CacheMachine::Logger.info "CACHE_MACHINE (reset_timestamp_of): deleting '#{cache_key}'."
|
171
174
|
Rails.cache.delete(cache_key)
|
172
175
|
end
|
173
176
|
|
data/spec/spec_helper.rb
CHANGED
@@ -9,6 +9,7 @@ RSpec.configure { |config| config.mock_with :rspec }
|
|
9
9
|
|
10
10
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "db/sqlite3.test.db")
|
11
11
|
CacheMachine::Logger.level = :info
|
12
|
+
CacheMachine::Cache.formats = [:ehtml, :json]
|
12
13
|
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
13
14
|
|
14
15
|
Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(:memory_store) # You can set memcached
|
metadata
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache-machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 8
|
10
|
+
version: 0.1.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
-
- Kevin Goslar
|
14
13
|
- Sergei Zinin
|
14
|
+
- Kevin Goslar
|
15
15
|
autorequire:
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-09-11 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: rails
|
@@ -33,9 +33,23 @@ dependencies:
|
|
33
33
|
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
name:
|
36
|
+
name: activemodel
|
37
37
|
prerelease: false
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: bundler
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
53
|
none: false
|
40
54
|
requirements:
|
41
55
|
- - ~>
|
@@ -47,11 +61,11 @@ dependencies:
|
|
47
61
|
- 0
|
48
62
|
version: 1.0.0
|
49
63
|
type: :development
|
50
|
-
version_requirements: *
|
64
|
+
version_requirements: *id003
|
51
65
|
- !ruby/object:Gem::Dependency
|
52
66
|
name: jeweler
|
53
67
|
prerelease: false
|
54
|
-
requirement: &
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
55
69
|
none: false
|
56
70
|
requirements:
|
57
71
|
- - ~>
|
@@ -63,7 +77,7 @@ dependencies:
|
|
63
77
|
- 2
|
64
78
|
version: 1.6.2
|
65
79
|
type: :development
|
66
|
-
version_requirements: *
|
80
|
+
version_requirements: *id004
|
67
81
|
description: A Ruby on Rails framework to support cache management based on explicitely modeled caching dependencies.
|
68
82
|
email: kgoslar@partyearth.com
|
69
83
|
executables: []
|