cache_proxy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Lucas Húngaro
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.textile ADDED
@@ -0,0 +1,69 @@
1
+ h1. cache_proxy
2
+
3
+ A mixin that provides better encapsulation and control when using cache in Rails
4
+
5
+ Does cache-proxy helps your daily work with Rails? So, "please recommend me in Work With Rails":http://workingwithrails.com/recommendation/new/person/9370-lucas-h-ngaro and thanks for your kindness! :)
6
+
7
+ h2. Why?
8
+
9
+ Using the default Rails abstration for caching will scatter calls to Rails.cache all over your code. Also, it isn't trivial to bypass the cache when you need to - most of the time you end up creating two versions of the "same" method: one cached and one uncached. Using cache-proxy you will encapsulate the access to the cache and will be able to bypass the cached result using a hash option.
10
+
11
+ h2. How?
12
+
13
+ First, install the gem:
14
+
15
+ <pre>
16
+ $ [sudo] gem install cache_proxy
17
+ </pre>
18
+
19
+ Then, add it as a dependency in your code using your favorite way (a simple require or mechanisms like the Bundler gem).
20
+
21
+ The gem will provide you a module to mixin into the classes the access the cache. For example:
22
+
23
+ <pre>
24
+ class User < ActiveRecord::Base
25
+ include CacheProxy
26
+
27
+ has_many :search_terms, :dependent => :destroy, :order => "term ASC"
28
+
29
+ def saved_search_terms
30
+ cache_proxy("my_cache_key") do
31
+ self.search_terms
32
+ end
33
+ end
34
+ end
35
+ </pre>
36
+
37
+ The cache_proxy method gives you the possibility of passing in two options:
38
+
39
+ <pre>
40
+ # this will bypass the cache and hit the storage, default is true
41
+ cache_proxy("my_cache_key", :cached => false) do
42
+ self.search_terms
43
+ end
44
+
45
+ # time to live, default is 30.days
46
+ cache_proxy("my_other_cache_key", :cache_expires_in => 2.hours) do
47
+ self.expensive_canculations
48
+ end
49
+ </pre>
50
+
51
+ And... that's it! You can use it both in class and instance methods.
52
+
53
+ h2. Note on Patches/Pull Requests
54
+
55
+ * Fork the project.
56
+ * Make your feature addition or bug fix.
57
+ * Add tests for it. This is important so I don't break it in a
58
+ future version unintentionally.
59
+ * Commit, do not mess with rakefile, version, or history.
60
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
61
+ * Send me a pull request. Bonus points for topic branches.
62
+
63
+ h2. Contributors
64
+
65
+ This library was implemented in conjuction with "Nando Vieira":http://simplesideias.com.br/, a really great programmer and friend.
66
+
67
+ h3. Copyright
68
+
69
+ Copyright (c) 2010 Lucas Húngaro. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'rake'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "cache_proxy"
9
+ gem.summary = %Q{A mixin that provides better encapsulation and control when using cache in Rails}
10
+ gem.description = %Q{A mixin that provides better encapsulation and control when using cache in Rails}
11
+ gem.email = "lucashungaro@gmail.com"
12
+ gem.homepage = "http://github.com/lucashungaro/cache_proxy"
13
+ gem.authors = ["Lucas Húngaro"]
14
+ gem.add_development_dependency "rspec", ">= 1.3.0"
15
+ gem.add_development_dependency "mocha", ">= 0.9.8"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "cache_proxy #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,33 @@
1
+ require "active_support"
2
+
3
+ module CacheProxy
4
+ def self.included(receiver)
5
+ receiver.extend ClassMethods
6
+ receiver.send :include, InstanceMethods
7
+ end
8
+
9
+ module Common
10
+ def cache_proxy(key, options = {}, &block)
11
+ options.reverse_merge!({
12
+ :cached => true,
13
+ :cache_expires_in => 30.days
14
+ })
15
+
16
+ repository = options[:repository] || Rails.cache
17
+
18
+ if options[:cached]
19
+ repository.fetch(key, :expires_in => options[:cache_expires_in], &block)
20
+ else
21
+ yield
22
+ end
23
+ end
24
+ end
25
+
26
+ module ClassMethods
27
+ include Common
28
+ end
29
+
30
+ module InstanceMethods
31
+ include Common
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/my_model')
3
+
4
+ describe CacheProxy do
5
+ before(:each) do
6
+ MyModel.send(:include, CacheProxy)
7
+ @model = MyModel.new
8
+ end
9
+
10
+ context "with the default options" do
11
+ it "should cache the result" do
12
+ repository = mock("repository")
13
+ repository.expects(:fetch)
14
+
15
+ @model.my_method({:repository => repository})
16
+ end
17
+
18
+ it "should expire the cache in 30 days" do
19
+ repository = mock("repository")
20
+ repository.expects(:fetch).with("my_key", :expires_in => 30.days)
21
+
22
+ @model.my_method({:repository => repository})
23
+ end
24
+ end
25
+
26
+ context "with user-specified options" do
27
+ it "should bypass the cache storage if desired by the user" do
28
+ repository = mock("repository")
29
+ repository.expects(:fetch).never
30
+
31
+ @model.my_method({:repository => repository, :cached => false})
32
+ end
33
+
34
+ it "should expire the cache using the range set by the user" do
35
+ repository = mock("repository")
36
+ repository.expects(:fetch).with("my_key", :expires_in => 3.days)
37
+
38
+ @model.my_method({:repository => repository, :cache_expires_in => 3.days})
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ class MyModel
2
+ def my_method(options = {})
3
+ cache_proxy("my_key", options) do
4
+ "Expensive call"
5
+ end
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'cache_proxy'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.mock_with :mocha
9
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_proxy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - "Lucas H\xC3\xBAngaro"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-25 00:00:00 -02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 0
34
+ version: 1.3.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mocha
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 43
46
+ segments:
47
+ - 0
48
+ - 9
49
+ - 8
50
+ version: 0.9.8
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: A mixin that provides better encapsulation and control when using cache in Rails
54
+ email: lucashungaro@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - LICENSE
61
+ - README.textile
62
+ files:
63
+ - .gitignore
64
+ - LICENSE
65
+ - README.textile
66
+ - Rakefile
67
+ - VERSION
68
+ - lib/cache_proxy.rb
69
+ - spec/cache_proxy_spec.rb
70
+ - spec/fixtures/my_model.rb
71
+ - spec/spec.opts
72
+ - spec/spec_helper.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/lucashungaro/cache_proxy
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A mixin that provides better encapsulation and control when using cache in Rails
107
+ test_files:
108
+ - spec/cache_proxy_spec.rb
109
+ - spec/fixtures/my_model.rb
110
+ - spec/spec_helper.rb