cacheify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +67 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/cacheify.gemspec +58 -0
- data/lib/cacheify.rb +49 -0
- data/spec/cacheify_spec.rb +28 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +86 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Saša_Branković
|
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.rdoc
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
= Cacheify
|
2
|
+
|
3
|
+
Simple caching of methods calls.
|
4
|
+
|
5
|
+
Cacheify uses ActiveSupport::Cache::Store to perform caching so, depending on
|
6
|
+
what you choose, it may cache to memory, disk, memcache or
|
7
|
+
whatever is supported by the cache store.
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
sudo gem install cacheify
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
Mixin the Cacheify module, use the +cacheify+ method.
|
16
|
+
|
17
|
+
require 'cacheify'
|
18
|
+
|
19
|
+
class Foo
|
20
|
+
include Cacheify
|
21
|
+
cacheify :bar, :expires_in => 1.hour
|
22
|
+
|
23
|
+
def bar
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
And you're +foo+ instance calls to +bar+ will get cached.
|
28
|
+
|
29
|
+
Foo.new.bar # will hit cache if cached, or get cached otherwise
|
30
|
+
|
31
|
+
To enable cacheify on existing class just +extend+:
|
32
|
+
|
33
|
+
Thing.extend Cacheify
|
34
|
+
Thing.caches :very_expensive_operation
|
35
|
+
|
36
|
+
If you use Cacheify <b>a lot</b>, you may even do:
|
37
|
+
|
38
|
+
Object.extend Cacheify
|
39
|
+
|
40
|
+
But that's just evil.
|
41
|
+
|
42
|
+
=== Choosing your Cache store
|
43
|
+
|
44
|
+
To setup what cache store Cacheify is using:
|
45
|
+
|
46
|
+
Cacheify.cache_store = :file_store, "tmp/cacheify"
|
47
|
+
|
48
|
+
The arguments are the same as in ActiveSupport::Cache#lookup_store[http://api.rubyonrails.org/classes/ActiveSupport/Cache.html#M001011]
|
49
|
+
|
50
|
+
== More info
|
51
|
+
|
52
|
+
* ActiveSupport::Cache[http://api.rubyonrails.org/classes/ActiveSupport/Cache.html] is what's used for caching
|
53
|
+
* Why cacheify? {Introductory blog post}[http://blog.hakeraj.com].
|
54
|
+
|
55
|
+
== Note on Patches/Pull Requests
|
56
|
+
|
57
|
+
* Fork the project.
|
58
|
+
* Make your feature addition or bug fix.
|
59
|
+
* Add tests for it. This is important so I don't break it in a
|
60
|
+
future version unintentionally.
|
61
|
+
* Commit, do not mess with rakefile, version, or history.
|
62
|
+
(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)
|
63
|
+
* Send me a pull request. Bonus points for topic branches.
|
64
|
+
|
65
|
+
== Copyright
|
66
|
+
|
67
|
+
Copyright (c) 2010 Sasa Brankovic. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "cacheify"
|
8
|
+
gem.summary = %Q{Simple caching of method calls}
|
9
|
+
gem.description = %Q{Cacheify can cache method calls to memory, disk, memcache or whatever is supported by the ActiveSupport::Cache::Store.}
|
10
|
+
gem.email = "sasa@hakeraj.com"
|
11
|
+
gem.homepage = "http://github.com/fox/cacheify"
|
12
|
+
gem.authors = ["Saša_Branković"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency "activesupport"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "cacheify #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/cacheify.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{cacheify}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Sa\305\241a_Brankovi\304\207"]
|
12
|
+
s.date = %q{2010-03-12}
|
13
|
+
s.description = %q{Cacheify can cache method calls to memory, disk, memcache or whatever is supported by the ActiveSupport::Cache::Store.}
|
14
|
+
s.email = %q{sasa@hakeraj.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"cacheify.gemspec",
|
27
|
+
"lib/cacheify.rb",
|
28
|
+
"spec/cacheify_spec.rb",
|
29
|
+
"spec/spec.opts",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/fox/cacheify}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{Simple caching of method calls}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/cacheify_spec.rb",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
48
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/cacheify.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'digest/md5'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Enables caching of method calls
|
6
|
+
module Cacheify
|
7
|
+
##
|
8
|
+
# Set cache store to be used by Cachify (or leave it be to use defaults).
|
9
|
+
#
|
10
|
+
# The default is FileStore tmp/cacheify or, if you're running
|
11
|
+
# inside Rails app, whatever Rails is using.
|
12
|
+
#
|
13
|
+
# === Parameters
|
14
|
+
# store options as understood by ActiveSupport::Cache#lookup_store
|
15
|
+
def self.cache_store=(store_option)
|
16
|
+
@cache_store = ActiveSupport::Cache.lookup_store(store_option)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.cache # :nodoc:
|
20
|
+
@cache_store ||= defined?(Rails) ? Rails.cache : ActiveSupport::Cache.lookup_store(:file_store, "tmp/cacheify")
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Cache results of methods.
|
25
|
+
#
|
26
|
+
# === Parameters
|
27
|
+
# * method names to cache
|
28
|
+
# * optionally, last argument can be a hash of options passed to ActiveSupport::Cache::Store for caching.
|
29
|
+
#
|
30
|
+
# === Example
|
31
|
+
# cache_method :big_calculation, :expires_in => 5.minutes
|
32
|
+
def cacheify(*symbols)
|
33
|
+
options = symbols.extract_options!
|
34
|
+
|
35
|
+
symbols.each do |method|
|
36
|
+
non_cached_method = "_non_cached_#{method}".to_sym
|
37
|
+
return if respond_to? non_cached_method # cached already, skip it
|
38
|
+
|
39
|
+
alias_method non_cached_method, method
|
40
|
+
|
41
|
+
define_method(method) do |*args, &block|
|
42
|
+
cache_name = Digest::MD5.hexdigest(args.inspect)
|
43
|
+
marshalled_result = Cacheify.cache.fetch(cache_name, options) do
|
44
|
+
send(non_cached_method, *args, &block)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
# Example class we'll use in specs
|
4
|
+
class Foo
|
5
|
+
def bar(something = nil)
|
6
|
+
"#{rand(99999999)}#{something}"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "Cacheify" do
|
11
|
+
before :all do
|
12
|
+
Foo.extend Cacheify
|
13
|
+
Foo.cacheify :bar
|
14
|
+
|
15
|
+
@foo = Foo.new
|
16
|
+
@first_call = @foo.bar("hello")
|
17
|
+
end
|
18
|
+
|
19
|
+
context "calling method twice on a cachified object" do
|
20
|
+
before do
|
21
|
+
@second_call = @foo.bar("hello")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return cached result second time" do
|
25
|
+
@first_call.should == @second_call
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cacheify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Sa\xC5\xA1a_Brankovi\xC4\x87"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-12 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Cacheify can cache method calls to memory, disk, memcache or whatever is supported by the ActiveSupport::Cache::Store.
|
36
|
+
email: sasa@hakeraj.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- cacheify.gemspec
|
52
|
+
- lib/cacheify.rb
|
53
|
+
- spec/cacheify_spec.rb
|
54
|
+
- spec/spec.opts
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/fox/cacheify
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.3.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Simple caching of method calls
|
84
|
+
test_files:
|
85
|
+
- spec/cacheify_spec.rb
|
86
|
+
- spec/spec_helper.rb
|