methodcache 0.0.0
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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/methodcache/everywhere.rb +3 -0
- data/lib/methodcache.rb +77 -0
- data/test/helper.rb +11 -0
- data/test/test_methodcache.rb +18 -0
- metadata +96 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Citizen Logistics, Inc.
|
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,17 @@
|
|
1
|
+
= methodcache
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(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)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Citizen Logistics, Inc.. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "methodcache"
|
8
|
+
gem.summary = %Q{like memoizable etc but more production oriented}
|
9
|
+
gem.description = %Q{like memoizable etc but more production oriented}
|
10
|
+
gem.email = "joe@citizenlogistics.com"
|
11
|
+
gem.homepage = "http://github.com/citizenlogistics/methodcache"
|
12
|
+
gem.authors = ["Joe Edelman"]
|
13
|
+
gem.add_development_dependency "minitest"
|
14
|
+
gem.add_development_dependency "yard"
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
begin
|
46
|
+
require 'yard'
|
47
|
+
YARD::Rake::YardocTask.new
|
48
|
+
rescue LoadError
|
49
|
+
task :yardoc do
|
50
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
51
|
+
end
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/methodcache.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
|
3
|
+
class MethodCache
|
4
|
+
def initialize obj, name, type, options
|
5
|
+
@name = name
|
6
|
+
@type = type
|
7
|
+
@timestamps = {}
|
8
|
+
@values = {}
|
9
|
+
@last_clean = Time.now
|
10
|
+
@clean_every = options[:clean_every] || 10*60
|
11
|
+
@duration = options[:for] || 60
|
12
|
+
@warn_time = options[:warn_time] || 0.03
|
13
|
+
@umeth = obj.instance_method(name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def timebox title, max_t = 0.01
|
17
|
+
result = nil
|
18
|
+
t = Benchmark.realtime{ result = yield }
|
19
|
+
warn "#{title}: #{(time*1000).to_i}ms" unless t < max_t
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_proc
|
24
|
+
cache = self
|
25
|
+
proc{ |*params| cache.try(self, *params) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def compute?(key, obj, *params)
|
29
|
+
ts = @timestamps[key]
|
30
|
+
return unless !ts or ts <= Time.now - @duration
|
31
|
+
timebox "#{@name}(#{key}) was not cached #{ts} < #{Time.now - @duration}", @warn_time do
|
32
|
+
@values[key] = @umeth.bind(obj).call(*params)
|
33
|
+
@timestamps[key] = Time.now
|
34
|
+
end
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
def clean?
|
39
|
+
expiry = Time.now - @duration
|
40
|
+
return unless @last_clean < Time.now - @clean_every
|
41
|
+
@timestamps.keys.each do |key|
|
42
|
+
next unless @timestamps[key] < expiry
|
43
|
+
@timestamps.delete(key)
|
44
|
+
@values.delete(key)
|
45
|
+
end
|
46
|
+
@last_clean = Time.now
|
47
|
+
end
|
48
|
+
|
49
|
+
def try(obj, *params)
|
50
|
+
key = (@type == :instance ? params + [obj.id] : params).hash
|
51
|
+
compute?(key, obj, *params) and clean?
|
52
|
+
@values[key]
|
53
|
+
end
|
54
|
+
|
55
|
+
module ModuleExtensions
|
56
|
+
def instance_cache name, options = {}
|
57
|
+
define_method(name, &MethodCache.new(self, name, :instance, options))
|
58
|
+
end
|
59
|
+
|
60
|
+
def singleton_cache name, options = {}
|
61
|
+
define_method(name, &MethodCache.new(self, name, :singleton, options))
|
62
|
+
end
|
63
|
+
|
64
|
+
def class_cache name, options = {}
|
65
|
+
metaclass = (class<<self;self;end)
|
66
|
+
cache = MethodCache.new(metaclass, name, :singleton, options)
|
67
|
+
metaclass.send(:define_method, name, &cache)
|
68
|
+
end
|
69
|
+
|
70
|
+
def module_cache name, options = {}
|
71
|
+
class_cache name, options
|
72
|
+
singleton_cache name, options
|
73
|
+
end
|
74
|
+
|
75
|
+
alias_method :cache, :singleton_cache
|
76
|
+
end
|
77
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'methodcache/everywhere'
|
3
|
+
|
4
|
+
class TestMethodcache < MiniTest::Unit::TestCase
|
5
|
+
A = [0]
|
6
|
+
|
7
|
+
def run_once
|
8
|
+
A[0] += 1
|
9
|
+
end
|
10
|
+
|
11
|
+
singleton_cache :run_once
|
12
|
+
|
13
|
+
def test_it
|
14
|
+
run_once
|
15
|
+
run_once
|
16
|
+
assert_equal 1, A[0]
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: methodcache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 0.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Joe Edelman
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-10 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: minitest
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: yard
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :development
|
43
|
+
version_requirements: *id002
|
44
|
+
description: like memoizable etc but more production oriented
|
45
|
+
email: joe@citizenlogistics.com
|
46
|
+
executables: []
|
47
|
+
|
48
|
+
extensions: []
|
49
|
+
|
50
|
+
extra_rdoc_files:
|
51
|
+
- LICENSE
|
52
|
+
- README.rdoc
|
53
|
+
files:
|
54
|
+
- .document
|
55
|
+
- .gitignore
|
56
|
+
- LICENSE
|
57
|
+
- README.rdoc
|
58
|
+
- Rakefile
|
59
|
+
- VERSION
|
60
|
+
- lib/methodcache.rb
|
61
|
+
- lib/methodcache/everywhere.rb
|
62
|
+
- test/helper.rb
|
63
|
+
- test/test_methodcache.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://github.com/citizenlogistics/methodcache
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options:
|
70
|
+
- --charset=UTF-8
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
requirements: []
|
88
|
+
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 1.3.6
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: like memoizable etc but more production oriented
|
94
|
+
test_files:
|
95
|
+
- test/helper.rb
|
96
|
+
- test/test_methodcache.rb
|