will_cache 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +30 -0
- data/Rakefile +9 -0
- data/lib/will_cache/cacheable.rb +39 -0
- data/lib/will_cache.rb +20 -0
- data/test/fixtures/database.yml +5 -0
- data/test/fixtures/schema.rb +14 -0
- data/test/functional/will_cache_test.rb +44 -0
- data/test/test_helper.rb +18 -0
- metadata +74 -0
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
WillCache
|
2
|
+
=========
|
3
|
+
|
4
|
+
WillCache provides simple API for caching ActiveRecord objects that uses ActiveSupport Cache Store internally so that it's trivial to switch cache storage. WillCache API is heavily inspired by excellent but no longer maintained [cache_fu plugin](https://github.com/defunkt/cache_fu) and one of the goals of the gem is to provide drop-in replacement for it.
|
5
|
+
|
6
|
+
|
7
|
+
## Example usage
|
8
|
+
|
9
|
+
>> User.last.cached(:profile)
|
10
|
+
User Load (0.000581) (1 Row) SELECT * FROM `users` ORDER BY users.id DESC LIMIT 1
|
11
|
+
Cache miss: users:65:profile
|
12
|
+
Profile Load (0.000454) (1 Row) SELECT * FROM `profiles` WHERE (`profiles`.user_id = 65) LIMIT 1
|
13
|
+
Cache write (will save 1.64ms): users:65:profile
|
14
|
+
=> #<Profile id: 65, first_name: nil, last_name: nil, ...>
|
15
|
+
|
16
|
+
Example shows [inline log in Rails console](http://rors.org/2011/07/17/inline-logging-in-rails-console.html).
|
17
|
+
|
18
|
+
|
19
|
+
## Install
|
20
|
+
|
21
|
+
Specify the gem in Gemfile of the project
|
22
|
+
|
23
|
+
gem "will_cache"
|
24
|
+
|
25
|
+
|
26
|
+
## Credits
|
27
|
+
|
28
|
+
Author: [Dejan Simic](http://github.com/dejan)<br/>
|
29
|
+
|
30
|
+
Initial development of the gem was sponsored by [LessEverything](http://lesseverything.com)
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module WillCache
|
2
|
+
module Cacheable
|
3
|
+
def expire_cache(method_name, args = {})
|
4
|
+
with = args[:with]
|
5
|
+
Rails.cache.delete(method_cache_key(method_name, with))
|
6
|
+
end
|
7
|
+
|
8
|
+
def cached(method_name, args = {})
|
9
|
+
with = args[:with]
|
10
|
+
Rails.cache.fetch(method_cache_key(method_name, with)) {
|
11
|
+
do_send(method_name, with)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def do_send(method_name, with)
|
16
|
+
if with.blank?
|
17
|
+
send(method_name)
|
18
|
+
else
|
19
|
+
send(method_name, with)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_cache_key(method_name, with)
|
24
|
+
if self.is_a?(ActiveRecord::Base)
|
25
|
+
base = "#{self.class}:#{id}:#{method_name}"
|
26
|
+
else
|
27
|
+
base = "#{self}:#{method_name}"
|
28
|
+
end
|
29
|
+
if with.blank?
|
30
|
+
base
|
31
|
+
else
|
32
|
+
"#{base}(#{with.inspect})"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
data/lib/will_cache.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path("../will_cache/cacheable", __FILE__)
|
2
|
+
|
3
|
+
module WillCache
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
include Cacheable
|
10
|
+
def acts_as_cached
|
11
|
+
send :include, InstanceMethods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
include Cacheable
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveRecord::Base.send :include, WillCache
|
@@ -0,0 +1,14 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
create_table "articles", :force => true do |t|
|
3
|
+
t.column "title", :string
|
4
|
+
t.column "body", :text
|
5
|
+
t.column "user_id", :integer
|
6
|
+
t.column "created_at", :datetime
|
7
|
+
t.column "updated_at", :datetime
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table "users", :force => true do |t|
|
11
|
+
t.column "name", :string
|
12
|
+
t.column "bio", :text
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Rails
|
4
|
+
def self.cache
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class User < ActiveRecord::Base
|
9
|
+
has_many :articles
|
10
|
+
acts_as_cached
|
11
|
+
end
|
12
|
+
|
13
|
+
class Article < ActiveRecord::Base
|
14
|
+
belongs_to :user
|
15
|
+
acts_as_cached
|
16
|
+
end
|
17
|
+
|
18
|
+
class WillCacheTest < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
fixtures_dir = File.dirname(__FILE__) + '/../fixtures'
|
21
|
+
connections = YAML.load_file("#{fixtures_dir}/database.yml")
|
22
|
+
ActiveRecord::Base.establish_connection(connections['sqlite3'])
|
23
|
+
ActiveRecord::Migration.verbose = false
|
24
|
+
load "#{fixtures_dir}/schema.rb"
|
25
|
+
|
26
|
+
@user = User.create!
|
27
|
+
@user.articles << Article.new(:body => 'hey')
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_cached_on_class_method
|
31
|
+
mock(Rails.cache).fetch("User:count") { 1 }
|
32
|
+
assert_equal 1, User.cached(:count)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_cached_on_instance_method
|
36
|
+
mock(Rails.cache).fetch("User:1:articles") { @user.articles }
|
37
|
+
assert_equal @user.articles, @user.cached(:articles)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_expire_cache
|
41
|
+
mock(Rails.cache).delete("User:1:articles")
|
42
|
+
@user.expire_cache(:articles)
|
43
|
+
end
|
44
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rr'
|
3
|
+
|
4
|
+
rails_version = ENV['RAILS_VERSION']
|
5
|
+
if rails_version
|
6
|
+
gem "activerecord", rails_version
|
7
|
+
gem "actionpack", rails_version
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
require 'active_record'
|
12
|
+
require 'active_support/core_ext/class'
|
13
|
+
|
14
|
+
require File.dirname(__FILE__) + '/../init'
|
15
|
+
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
include RR::Adapters::TestUnit
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: will_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dejan Simic
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-18 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: WillCache provides simple API for caching ActiveRecord objects that uses ActiveSupport Cache Store internally so that it's trivial to switch cache storage. WillCache API is heavily inspired by excellent but no longer maintained cache_fu plugin and one of the goals of the gem is to provide drop-in replacement for it.
|
23
|
+
email: desimic@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- Rakefile
|
32
|
+
- lib/will_cache/cacheable.rb
|
33
|
+
- lib/will_cache.rb
|
34
|
+
- test/fixtures/database.yml
|
35
|
+
- test/fixtures/schema.rb
|
36
|
+
- test/functional/will_cache_test.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
- README.md
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/dejan/will_cache
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Drop-in replacement for cache_fu that uses Rails.cache store
|
73
|
+
test_files: []
|
74
|
+
|