cache_cow 0.0.1
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 +6 -0
- data/.gitignore.bak +4 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +176 -0
- data/MIT-LICENSE +20 -0
- data/README.md +48 -0
- data/Rakefile +54 -0
- data/cache_cow.gemspec +32 -0
- data/lib/cache_cow.rb +8 -0
- data/lib/cache_cow/acts_as_cached.rb +172 -0
- data/lib/cache_cow/cacheable.rb +76 -0
- data/lib/cache_cow/cached_id_list.rb +46 -0
- data/lib/cache_cow/version.rb +3 -0
- data/lib/tasks/cache_cow_tasks.rake +4 -0
- data/spec/cache_cow/acts_as_cached_spec.rb +8 -0
- data/spec/cache_cow/cacheable_spec.rb +161 -0
- data/spec/cache_cow/cached_id_list_spec.rb +54 -0
- data/spec/cache_cow_spec.rb +8 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +9 -0
- data/spec/dummy/app/assets/stylesheets/application.css +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/blog_comment.rb +2 -0
- data/spec/dummy/app/models/blog_post.rb +2 -0
- data/spec/dummy/app/models/comment.rb +2 -0
- data/spec/dummy/app/models/post.rb +6 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +49 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +30 -0
- data/spec/dummy/config/environments/production.rb +60 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/db/migrate/20111026035600_create_posts.rb +11 -0
- data/spec/dummy/db/migrate/20111026035754_create_comments.rb +10 -0
- data/spec/dummy/db/migrate/20111026222959_add_sti_to_posts_and_comments.rb +6 -0
- data/spec/dummy/db/schema.rb +32 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/factories.rb +11 -0
- data/spec/spec_helper.rb +26 -0
- metadata +310 -0
data/.gitignore
ADDED
data/.gitignore.bak
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in cache_cow.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
|
9
|
+
# Declare any dependencies that are still in development here instead of in
|
10
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
11
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
12
|
+
# your gem to rubygems.org.
|
13
|
+
|
14
|
+
# To use debugger
|
15
|
+
gem 'ruby-debug19', :platform => :ruby_19, :require => 'ruby-debug'
|
16
|
+
gem 'ruby-debug', :platform => :ruby_18
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cache_cow (0.0.1)
|
5
|
+
rails (~> 3.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (3.1.1)
|
11
|
+
actionpack (= 3.1.1)
|
12
|
+
mail (~> 2.3.0)
|
13
|
+
actionpack (3.1.1)
|
14
|
+
activemodel (= 3.1.1)
|
15
|
+
activesupport (= 3.1.1)
|
16
|
+
builder (~> 3.0.0)
|
17
|
+
erubis (~> 2.7.0)
|
18
|
+
i18n (~> 0.6)
|
19
|
+
rack (~> 1.3.2)
|
20
|
+
rack-cache (~> 1.1)
|
21
|
+
rack-mount (~> 0.8.2)
|
22
|
+
rack-test (~> 0.6.1)
|
23
|
+
sprockets (~> 2.0.2)
|
24
|
+
activemodel (3.1.1)
|
25
|
+
activesupport (= 3.1.1)
|
26
|
+
builder (~> 3.0.0)
|
27
|
+
i18n (~> 0.6)
|
28
|
+
activerecord (3.1.1)
|
29
|
+
activemodel (= 3.1.1)
|
30
|
+
activesupport (= 3.1.1)
|
31
|
+
arel (~> 2.2.1)
|
32
|
+
tzinfo (~> 0.3.29)
|
33
|
+
activeresource (3.1.1)
|
34
|
+
activemodel (= 3.1.1)
|
35
|
+
activesupport (= 3.1.1)
|
36
|
+
activesupport (3.1.1)
|
37
|
+
multi_json (~> 1.0)
|
38
|
+
ammeter (0.2.1)
|
39
|
+
activesupport (~> 3.0)
|
40
|
+
railties (~> 3.0)
|
41
|
+
rspec (~> 2.2)
|
42
|
+
rspec-rails (~> 2.2)
|
43
|
+
archive-tar-minitar (0.5.2)
|
44
|
+
arel (2.2.1)
|
45
|
+
builder (3.0.0)
|
46
|
+
capybara (1.1.1)
|
47
|
+
mime-types (>= 1.16)
|
48
|
+
nokogiri (>= 1.3.3)
|
49
|
+
rack (>= 1.0.0)
|
50
|
+
rack-test (>= 0.5.4)
|
51
|
+
selenium-webdriver (~> 2.0)
|
52
|
+
xpath (~> 0.1.4)
|
53
|
+
childprocess (0.2.2)
|
54
|
+
ffi (~> 1.0.6)
|
55
|
+
columnize (0.3.4)
|
56
|
+
dalli (1.1.3)
|
57
|
+
database_cleaner (0.6.7)
|
58
|
+
diff-lcs (1.1.3)
|
59
|
+
erubis (2.7.0)
|
60
|
+
factory_girl (2.2.0)
|
61
|
+
activesupport
|
62
|
+
factory_girl_rails (1.3.0)
|
63
|
+
factory_girl (~> 2.2.0)
|
64
|
+
railties (>= 3.0.0)
|
65
|
+
ffi (1.0.9)
|
66
|
+
hike (1.2.1)
|
67
|
+
i18n (0.6.0)
|
68
|
+
json (1.6.1)
|
69
|
+
json_pure (1.6.1)
|
70
|
+
linecache (0.46)
|
71
|
+
rbx-require-relative (> 0.0.4)
|
72
|
+
linecache19 (0.5.12)
|
73
|
+
ruby_core_source (>= 0.1.4)
|
74
|
+
mail (2.3.0)
|
75
|
+
i18n (>= 0.4.0)
|
76
|
+
mime-types (~> 1.16)
|
77
|
+
treetop (~> 1.4.8)
|
78
|
+
mime-types (1.17.1)
|
79
|
+
multi_json (1.0.3)
|
80
|
+
nokogiri (1.5.0)
|
81
|
+
polyglot (0.3.2)
|
82
|
+
rack (1.3.5)
|
83
|
+
rack-cache (1.1)
|
84
|
+
rack (>= 0.4)
|
85
|
+
rack-mount (0.8.3)
|
86
|
+
rack (>= 1.0.0)
|
87
|
+
rack-ssl (1.3.2)
|
88
|
+
rack
|
89
|
+
rack-test (0.6.1)
|
90
|
+
rack (>= 1.0)
|
91
|
+
rails (3.1.1)
|
92
|
+
actionmailer (= 3.1.1)
|
93
|
+
actionpack (= 3.1.1)
|
94
|
+
activerecord (= 3.1.1)
|
95
|
+
activeresource (= 3.1.1)
|
96
|
+
activesupport (= 3.1.1)
|
97
|
+
bundler (~> 1.0)
|
98
|
+
railties (= 3.1.1)
|
99
|
+
railties (3.1.1)
|
100
|
+
actionpack (= 3.1.1)
|
101
|
+
activesupport (= 3.1.1)
|
102
|
+
rack-ssl (~> 1.3.2)
|
103
|
+
rake (>= 0.8.7)
|
104
|
+
rdoc (~> 3.4)
|
105
|
+
thor (~> 0.14.6)
|
106
|
+
rake (0.9.2.2)
|
107
|
+
rbx-require-relative (0.0.5)
|
108
|
+
rdoc (3.11)
|
109
|
+
json (~> 1.4)
|
110
|
+
rspec (2.7.0)
|
111
|
+
rspec-core (~> 2.7.0)
|
112
|
+
rspec-expectations (~> 2.7.0)
|
113
|
+
rspec-mocks (~> 2.7.0)
|
114
|
+
rspec-core (2.7.1)
|
115
|
+
rspec-expectations (2.7.0)
|
116
|
+
diff-lcs (~> 1.1.2)
|
117
|
+
rspec-mocks (2.7.0)
|
118
|
+
rspec-rails (2.7.0)
|
119
|
+
actionpack (~> 3.0)
|
120
|
+
activesupport (~> 3.0)
|
121
|
+
railties (~> 3.0)
|
122
|
+
rspec (~> 2.7.0)
|
123
|
+
ruby-debug (0.10.4)
|
124
|
+
columnize (>= 0.1)
|
125
|
+
ruby-debug-base (~> 0.10.4.0)
|
126
|
+
ruby-debug-base (0.10.4)
|
127
|
+
linecache (>= 0.3)
|
128
|
+
ruby-debug-base19 (0.11.25)
|
129
|
+
columnize (>= 0.3.1)
|
130
|
+
linecache19 (>= 0.5.11)
|
131
|
+
ruby_core_source (>= 0.1.4)
|
132
|
+
ruby-debug19 (0.11.6)
|
133
|
+
columnize (>= 0.3.1)
|
134
|
+
linecache19 (>= 0.5.11)
|
135
|
+
ruby-debug-base19 (>= 0.11.19)
|
136
|
+
ruby_core_source (0.1.5)
|
137
|
+
archive-tar-minitar (>= 0.5.2)
|
138
|
+
rubyzip (0.9.4)
|
139
|
+
selenium-webdriver (2.9.1)
|
140
|
+
childprocess (>= 0.2.1)
|
141
|
+
ffi (= 1.0.9)
|
142
|
+
json_pure
|
143
|
+
rubyzip
|
144
|
+
sprockets (2.0.3)
|
145
|
+
hike (~> 1.2)
|
146
|
+
rack (~> 1.0)
|
147
|
+
tilt (~> 1.1, != 1.3.0)
|
148
|
+
sqlite3 (1.3.4)
|
149
|
+
steak (2.0.0)
|
150
|
+
capybara (>= 1.0.0)
|
151
|
+
rspec-rails (>= 2.5.0)
|
152
|
+
thor (0.14.6)
|
153
|
+
tilt (1.3.3)
|
154
|
+
treetop (1.4.10)
|
155
|
+
polyglot
|
156
|
+
polyglot (>= 0.3.1)
|
157
|
+
tzinfo (0.3.30)
|
158
|
+
xpath (0.1.4)
|
159
|
+
nokogiri (~> 1.3)
|
160
|
+
|
161
|
+
PLATFORMS
|
162
|
+
ruby
|
163
|
+
|
164
|
+
DEPENDENCIES
|
165
|
+
ammeter
|
166
|
+
cache_cow!
|
167
|
+
capybara
|
168
|
+
dalli
|
169
|
+
database_cleaner
|
170
|
+
factory_girl_rails
|
171
|
+
rspec
|
172
|
+
rspec-rails
|
173
|
+
ruby-debug
|
174
|
+
ruby-debug19
|
175
|
+
sqlite3
|
176
|
+
steak
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# CacheCow
|
2
|
+
|
3
|
+
CacheCow allows ActiveRecord models to `acts_as_cached``
|
4
|
+
|
5
|
+
|
6
|
+
## Install
|
7
|
+
|
8
|
+
In your Gemfile
|
9
|
+
|
10
|
+
gem "cache_cow"
|
11
|
+
|
12
|
+
Or via command line
|
13
|
+
|
14
|
+
gem install cache_cow
|
15
|
+
|
16
|
+
|
17
|
+
## Overview
|
18
|
+
|
19
|
+
CacheCow provides an api for caching results of methods and associations on ActiveRecord models.
|
20
|
+
|
21
|
+
To use in a model, declare `acts_as_cached`
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
|
25
|
+
class Post
|
26
|
+
acts_as_cached
|
27
|
+
end
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
Instances of a cache_cow model can read, write, fetch, and expire their own cache keys. Cache keys
|
32
|
+
are namespaced and versioned by convention as <class_name>:<version>:<id>:<cache_suffix>. An instances
|
33
|
+
id is the cache suffix be default.
|
34
|
+
|
35
|
+
``` ruby
|
36
|
+
|
37
|
+
# Post.cache_cow_version = 1
|
38
|
+
|
39
|
+
post = Post.find(123)
|
40
|
+
|
41
|
+
post.fetch_cache { "great post" } # fetches from cache key 'Post:1:123', store 'great post' on cache miss
|
42
|
+
|
43
|
+
post.write_cache("foo", "bar") # writes 'bar to cache key 'Post:1:123:foo'
|
44
|
+
|
45
|
+
post.read_cache("foo") # returns 'bar' from cache key 'Post:1:123:foo'
|
46
|
+
|
47
|
+
````
|
48
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Seymour'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path(".././spec/dummy/Rakefile", __FILE__)
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rspec/core/rake_task'
|
28
|
+
|
29
|
+
desc "Run specs"
|
30
|
+
RSpec::Core::RakeTask.new do |t|
|
31
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
32
|
+
# Put spec opts in a file named .rspec in root
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Generate code coverage"
|
36
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
37
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
38
|
+
t.rcov = true
|
39
|
+
t.rcov_opts = ['--exclude', 'spec']
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "Run the specs"
|
43
|
+
task :default => ["spec"]
|
44
|
+
require 'rake/testtask'
|
45
|
+
|
46
|
+
Rake::TestTask.new(:test) do |t|
|
47
|
+
t.libs << 'lib'
|
48
|
+
t.libs << 'test'
|
49
|
+
t.pattern = 'test/**/*_test.rb'
|
50
|
+
t.verbose = false
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
task :default => :test
|
data/cache_cow.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "cache_cow/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "cache_cow"
|
7
|
+
s.version = CacheCow::VERSION
|
8
|
+
s.authors = ["Ross Kaffenberger"]
|
9
|
+
s.email = ["rosskaff@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Allows ActiveRecord models to "acts_as_cached"}
|
12
|
+
s.description = %q{CacheCow provides an api for caching results of methods and associations on ActiveRecord models.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "cache_cow"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "rails", "~> 3.0"
|
22
|
+
|
23
|
+
s.add_development_dependency "dalli"
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "rspec-rails"
|
26
|
+
s.add_development_dependency 'steak'
|
27
|
+
s.add_development_dependency 'capybara'
|
28
|
+
s.add_development_dependency 'ammeter'
|
29
|
+
s.add_development_dependency 'database_cleaner'
|
30
|
+
s.add_development_dependency 'factory_girl_rails'
|
31
|
+
s.add_development_dependency "sqlite3"
|
32
|
+
end
|
data/lib/cache_cow.rb
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
module CacheCow
|
2
|
+
module ActsAsCached
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def acts_as_cached
|
8
|
+
include Cacheable
|
9
|
+
include CachedIdList
|
10
|
+
end
|
11
|
+
|
12
|
+
def acts_as_cached_options
|
13
|
+
@acts_as_cached_options ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def acts_as_cached_version
|
17
|
+
acts_as_cached_options[:version] || 1
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
class ActiveRecord::Base
|
26
|
+
include CacheCow::ActsAsCached
|
27
|
+
end
|
28
|
+
|
29
|
+
# ActiveRecord::Base.send :include, CacheCow::ActsAsCached
|
30
|
+
|
31
|
+
# def self.included(base)
|
32
|
+
# base.extend ClassMethods
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# module ClassMethods
|
36
|
+
# @@nil_sentinel = :_nil
|
37
|
+
#
|
38
|
+
# def self.extended(a_class)
|
39
|
+
# class << a_class
|
40
|
+
# alias_method_chain :inherited, :acts_as_cached
|
41
|
+
# end
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# def inherited_with_acts_as_cached(subclass)
|
45
|
+
# inherited_without_acts_as_cached(subclass)
|
46
|
+
# subclass.acts_as_cached acts_as_cached_options
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# def acts_as_cached(options = {})
|
50
|
+
# @acts_as_cached_options = options
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# def acts_as_cached_options
|
54
|
+
# @acts_as_cached_options ||= {}
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# def acts_as_cached_version
|
58
|
+
# acts_as_cached_options[:version] || 1
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# def cached(method, options = {})
|
62
|
+
# fetch_cache(method, options) { send(method) }
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# def fetch_cache(*args, &block)
|
66
|
+
# options = args.last.is_a?(Hash) ? args.pop : {}
|
67
|
+
# keys = args.flatten
|
68
|
+
#
|
69
|
+
# raise "Doesn't support multiget" unless keys.size == 1
|
70
|
+
#
|
71
|
+
# if (value = Rails.cache.read(cache_key(keys.first), options))
|
72
|
+
# value == @@nil_sentinel ? nil : value
|
73
|
+
# else
|
74
|
+
# value = fetch_cachable_data(keys.first, &block)
|
75
|
+
# value_to_cache = value.nil? ? @@nil_sentinel : value
|
76
|
+
# Rails.cache.write(cache_key(keys.first), value_to_cache, options)
|
77
|
+
# return value
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# def get_multi(cache_ids, &block)
|
82
|
+
# cache_keys = cache_ids.map { |cache_id| cache_key(cache_id) }
|
83
|
+
# cache_hits = Rails.cache.read_multi(*cache_keys)
|
84
|
+
# yield MultigetContext.new(self, cache_hits)
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# def fetch_cachable_data(cache_id = nil, &block)
|
88
|
+
# if block_given?
|
89
|
+
# yield(cache_id)
|
90
|
+
# else
|
91
|
+
# find(cache_id)
|
92
|
+
# end
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# def cached?(cache_id = nil)
|
96
|
+
# read_cache(cache_id).nil? ? false : true
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# def read_cache(cache_id = nil)
|
100
|
+
# Rails.cache.read(cache_key(cache_id))
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# def set_cache(cache_id, value, ttl = nil)
|
104
|
+
# returning(value) do |v|
|
105
|
+
# v = @@nil_sentinel if v.nil?
|
106
|
+
# Rails.cache.write(cache_key(cache_id), v, :expires_in => (ttl || 1500))
|
107
|
+
# end
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# def expire_cache(cache_id = nil)
|
111
|
+
# Rails.cache.delete cache_key(cache_id)
|
112
|
+
# true
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# def cache_name
|
116
|
+
# @cache_name ||= respond_to?(:base_class) ? base_class.name : name
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# def max_key_length
|
120
|
+
# 200
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# def cache_key(cache_id)
|
124
|
+
# [cache_name, acts_as_cached_version, cache_id].compact.join(':').gsub(' ', '_')[0..(max_key_length - 1)]
|
125
|
+
# end
|
126
|
+
#
|
127
|
+
# private
|
128
|
+
#
|
129
|
+
# NIL_SENTINEL = @@nil_sentinel
|
130
|
+
#
|
131
|
+
# class MultigetContext
|
132
|
+
# def initialize(klass, cache_hits)
|
133
|
+
# @klass = klass
|
134
|
+
# @cache_hits = cache_hits
|
135
|
+
# end
|
136
|
+
#
|
137
|
+
# def fetch_cache(*args, &block)
|
138
|
+
# args_temp = args.dup
|
139
|
+
# options = args_temp.last.is_a?(Hash) ? args.pop : {}
|
140
|
+
# keys = args_temp.flatten
|
141
|
+
# if value = @cache_hits[@klass.cache_key(keys.first)]
|
142
|
+
# value == NIL_SENTINEL ? nil : value
|
143
|
+
# else
|
144
|
+
# @klass.fetch_cache(args, options, &block)
|
145
|
+
# end
|
146
|
+
# end
|
147
|
+
# end
|
148
|
+
# end
|
149
|
+
#
|
150
|
+
# def fetch_cache(key = nil, options = {}, &block)
|
151
|
+
# self.class.fetch_cache(cache_id(key), options, &block)
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# def cached(method, options = {})
|
155
|
+
# self.class.fetch_cache(cache_id(method), options) { send(method) }
|
156
|
+
# end
|
157
|
+
#
|
158
|
+
# def cached?(key = nil)
|
159
|
+
# self.class.cached? cache_id(key)
|
160
|
+
# end
|
161
|
+
#
|
162
|
+
# def read_cache(key = nil)
|
163
|
+
# self.class.read_cache cache_id(key)
|
164
|
+
# end
|
165
|
+
#
|
166
|
+
# def expire_cache(key = nil)
|
167
|
+
# self.class.expire_cache(cache_id(key))
|
168
|
+
# end
|
169
|
+
#
|
170
|
+
# def cache_id(key = nil)
|
171
|
+
# key.nil? ? id.to_s : "#{id}:#{key}"
|
172
|
+
# end
|