callcache 0.1.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/README +0 -0
- data/callcache-0.1.0.gem +0 -0
- data/callcache-0.1.gem +0 -0
- data/callcache.gemspec +18 -0
- data/lib/Rakefile.rb +7 -0
- data/lib/cached_call.rb +37 -0
- data/lib/call_cache.rb +97 -0
- data/lib/main.rb +34 -0
- data/nbproject/private/private.xml +4 -0
- data/nbproject/project.properties +3 -0
- data/nbproject/project.xml +15 -0
- metadata +60 -0
data/README
ADDED
File without changes
|
data/callcache-0.1.0.gem
ADDED
Binary file
|
data/callcache-0.1.gem
ADDED
Binary file
|
data/callcache.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
SPEC = Gem::Specification.new do |s|
|
5
|
+
s.name = "callcache"
|
6
|
+
s.version = "0.1.1"
|
7
|
+
s.author = "Adam Wisniewski"
|
8
|
+
s.email = "adamw@tbcn.ca"
|
9
|
+
s.date = s.date = Date.today.to_s
|
10
|
+
s.homepage = "http://www.tbcn.ca/callcache"
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.summary = "Ruby library for caching results of a function call"
|
13
|
+
candidates = Dir.glob("{bin,docs,lib,test}/**/*")
|
14
|
+
s.files = Dir.glob('**/*')
|
15
|
+
s.require_path = "lib"
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.extra_rdoc_files = ["README"]
|
18
|
+
end
|
data/lib/Rakefile.rb
ADDED
data/lib/cached_call.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module CallCache
|
20
|
+
class CachedCall
|
21
|
+
attr :return_val
|
22
|
+
attr :function_call
|
23
|
+
attr :parameters
|
24
|
+
attr :created_at
|
25
|
+
|
26
|
+
def initialize( return_val, function_call, *parameters )
|
27
|
+
@created_at = Time.now
|
28
|
+
@return_val = return_val
|
29
|
+
@function_call = function_call
|
30
|
+
@parameters = parameters
|
31
|
+
end
|
32
|
+
|
33
|
+
def id
|
34
|
+
CallCache.get_id( @function_call, *@parameters)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/call_cache.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
require 'digest/md5'
|
20
|
+
require 'cached_call'
|
21
|
+
|
22
|
+
module CallCache
|
23
|
+
|
24
|
+
CACHE_DIRECTORY = "/tmp/cache"
|
25
|
+
SECONDS_TO_CACHE = 60 * 60 * 3
|
26
|
+
|
27
|
+
def CallCache.call( function_call, *parameters )
|
28
|
+
#CallCache.check_or_create_tmp_dir
|
29
|
+
|
30
|
+
cached_call = CallCache.find( function_call, *parameters )
|
31
|
+
|
32
|
+
if cached_call == nil || Time.now.to_i - cached_call.created_at.to_i > SECONDS_TO_CACHE
|
33
|
+
return_val = CallCache.make_call( function_call, *parameters )
|
34
|
+
cached_call = CachedCall.new( return_val, function_call, *parameters )
|
35
|
+
|
36
|
+
CallCache.save( cached_call )
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
return cached_call.return_val
|
41
|
+
end
|
42
|
+
|
43
|
+
def CallCache.get_id_for( function_call, *parameters)
|
44
|
+
md5 = Digest::MD5::new
|
45
|
+
md5.update( function_call )
|
46
|
+
md5.update( Marshal.dump( parameters ) )
|
47
|
+
return md5.hexdigest
|
48
|
+
end
|
49
|
+
|
50
|
+
#def CallCache.check_or_create_tmp_dir
|
51
|
+
# file_path = CallCache::CACHE_DIRECTORY + File::SEPARATOR + "call_cache_" + id_string
|
52
|
+
#end
|
53
|
+
|
54
|
+
def CallCache.file_path_for( function_call, *parameters )
|
55
|
+
CallCache::CACHE_DIRECTORY + File::SEPARATOR +
|
56
|
+
"call_cache_" + CallCache.get_id_for( function_call, *parameters )
|
57
|
+
end
|
58
|
+
|
59
|
+
def CallCache.find( function_call, *parameters )
|
60
|
+
cached_call = nil
|
61
|
+
|
62
|
+
file_path = CallCache.file_path_for(function_call, *parameters)
|
63
|
+
|
64
|
+
if File.exist?( file_path )
|
65
|
+
# return saved cached result
|
66
|
+
cached_call = Marshal.load( File.read( file_path ) )
|
67
|
+
end
|
68
|
+
|
69
|
+
return cached_call
|
70
|
+
end
|
71
|
+
|
72
|
+
def CallCache.save( cached_call )
|
73
|
+
# write call to cache location
|
74
|
+
|
75
|
+
if !File.directory?( CallCache::CACHE_DIRECTORY )
|
76
|
+
Dir.mkdir( CallCache::CACHE_DIRECTORY )
|
77
|
+
end
|
78
|
+
|
79
|
+
file_path = CallCache.file_path_for( cached_call.function_call, *cached_call.parameters )
|
80
|
+
dest_file = File.new( file_path, 'wb' )
|
81
|
+
dest_file.write( Marshal.dump( cached_call ) )
|
82
|
+
dest_file.close
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
def CallCache.make_call( function_call, *parameters )
|
87
|
+
# call function
|
88
|
+
|
89
|
+
# parse call to handle local methods or static class methods
|
90
|
+
function_call.gsub!( '::', '.' )
|
91
|
+
function_call_split = function_call.split(/\./)
|
92
|
+
|
93
|
+
# make the call
|
94
|
+
return_val = eval(function_call_split[0..-2].join('::')).method( function_call_split[-1] ).call( *parameters )
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
data/lib/main.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
require 'call_cache'
|
20
|
+
|
21
|
+
def exp_fun( start_num, end_num )
|
22
|
+
ret_val = 1
|
23
|
+
|
24
|
+
start_num.upto( end_num ) do |num|
|
25
|
+
ret_val = ret_val * num
|
26
|
+
end
|
27
|
+
|
28
|
+
return ret_val.to_s.size
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
p CallCache.call( 'exp_fun', 1, 10000 )
|
33
|
+
|
34
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project xmlns="http://www.netbeans.org/ns/project/1">
|
3
|
+
<type>org.netbeans.modules.ruby.rubyproject</type>
|
4
|
+
<configuration>
|
5
|
+
<data xmlns="http://www.netbeans.org/ns/ruby-project/1">
|
6
|
+
<name>CallCache</name>
|
7
|
+
<source-roots>
|
8
|
+
<root id="src.dir"/>
|
9
|
+
</source-roots>
|
10
|
+
<test-roots>
|
11
|
+
<root id="test.src.dir"/>
|
12
|
+
</test-roots>
|
13
|
+
</data>
|
14
|
+
</configuration>
|
15
|
+
</project>
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: callcache
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2007-02-25 00:00:00 -05:00
|
8
|
+
summary: Ruby library for caching results of a function call
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: adamw@tbcn.ca
|
12
|
+
homepage: http://www.tbcn.ca/callcache
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Adam Wisniewski
|
31
|
+
files:
|
32
|
+
- callcache-0.1.0.gem
|
33
|
+
- callcache-0.1.gem
|
34
|
+
- callcache.gemspec
|
35
|
+
- lib
|
36
|
+
- nbproject
|
37
|
+
- README
|
38
|
+
- test
|
39
|
+
- lib/cached_call.rb
|
40
|
+
- lib/call_cache.rb
|
41
|
+
- lib/main.rb
|
42
|
+
- lib/Rakefile.rb
|
43
|
+
- nbproject/private
|
44
|
+
- nbproject/project.properties
|
45
|
+
- nbproject/project.xml
|
46
|
+
- nbproject/private/private.xml
|
47
|
+
test_files: []
|
48
|
+
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
dependencies: []
|
60
|
+
|