callcache 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/callcache.gemspec CHANGED
@@ -3,7 +3,7 @@ require 'date'
3
3
 
4
4
  SPEC = Gem::Specification.new do |s|
5
5
  s.name = "callcache"
6
- s.version = "0.1.2"
6
+ s.version = "0.2.0"
7
7
  s.author = "Adam Wisniewski"
8
8
  s.email = "adamw@tbcn.ca"
9
9
  s.date = s.date = Date.today.to_s
data/lib/call_cache.rb CHANGED
@@ -24,17 +24,24 @@ module CallCache
24
24
  CACHE_DIRECTORY = "/tmp/cache"
25
25
  SECONDS_TO_CACHE = 60 * 60 * 3
26
26
 
27
- def CallCache.call( function_call, *parameters )
28
- #CallCache.check_or_create_tmp_dir
27
+ def CallCache.call( function_call, hashes )
28
+ parameters = hashes[:pars]
29
+ options = hashes[:options]
29
30
 
31
+ ttl = SECONDS_TO_CACHE
32
+ if !options.nil?
33
+ if options[:ttl]
34
+ ttl = options[:ttl]
35
+ end
36
+ end
37
+
30
38
  cached_call = CallCache.find( function_call, *parameters )
31
39
 
32
- if cached_call == nil || Time.now.to_i - cached_call.created_at.to_i > SECONDS_TO_CACHE
40
+ if cached_call == nil || Time.now.to_i - cached_call.created_at.to_i > ttl
33
41
  return_val = CallCache.make_call( function_call, *parameters )
34
42
  cached_call = CachedCall.new( return_val, function_call, *parameters )
35
43
 
36
44
  CallCache.save( cached_call )
37
-
38
45
  end
39
46
 
40
47
  return cached_call.return_val
@@ -46,21 +53,21 @@ module CallCache
46
53
  md5.update( Marshal.dump( parameters ) )
47
54
  return md5.hexdigest
48
55
  end
49
-
56
+
50
57
  #def CallCache.check_or_create_tmp_dir
51
58
  # file_path = CallCache::CACHE_DIRECTORY + File::SEPARATOR + "call_cache_" + id_string
52
59
  #end
53
-
60
+
54
61
  def CallCache.file_path_for( function_call, *parameters )
55
62
  CallCache::CACHE_DIRECTORY + File::SEPARATOR +
56
63
  "call_cache_" + CallCache.get_id_for( function_call, *parameters )
57
64
  end
58
-
65
+
59
66
  def CallCache.find( function_call, *parameters )
60
67
  cached_call = nil
61
-
68
+
62
69
  file_path = CallCache.file_path_for(function_call, *parameters)
63
-
70
+
64
71
  if File.exist?( file_path )
65
72
  # return saved cached result
66
73
  cached_call = Marshal.load( File.read( file_path ) )
@@ -68,30 +75,29 @@ module CallCache
68
75
 
69
76
  return cached_call
70
77
  end
71
-
78
+
72
79
  def CallCache.save( cached_call )
73
80
  # write call to cache location
74
-
81
+
75
82
  if !File.directory?( CallCache::CACHE_DIRECTORY )
76
83
  Dir.mkdir( CallCache::CACHE_DIRECTORY )
77
84
  end
78
-
85
+
79
86
  file_path = CallCache.file_path_for( cached_call.function_call, *cached_call.parameters )
80
87
  dest_file = File.new( file_path, 'wb' )
81
88
  dest_file.write( Marshal.dump( cached_call ) )
82
89
  dest_file.close
83
-
90
+
84
91
  end
85
-
92
+
86
93
  def CallCache.make_call( function_call, *parameters )
87
-
88
- # call function
89
- # parse call to handle local methods or static class methods
90
- function_call.gsub!( '::', '.' )
91
- function_call_split = function_call.split(/\./)
92
- # make the call
93
-
94
- return_val = eval( function_call_split[0..-2].join('::') ).method( function_call_split[-1] ).call( *parameters )
94
+ # call function
95
+ # parse call to handle local methods or static class methods
96
+ function_call.gsub!( '::', '.' )
97
+ function_call_split = function_call.split(/\./)
98
+ # make the call
95
99
 
96
- end
100
+ return_val = eval( function_call_split[0..-2].join('::') ).method( function_call_split[-1] ).call( *parameters )
101
+
102
+ end
97
103
  end
data/lib/main.rb CHANGED
@@ -31,5 +31,7 @@ def exp_fun( start_num, end_num )
31
31
  end
32
32
 
33
33
 
34
- p CallCache.call( 'exp_fun', 1, 10000 )
35
- p CallCache.call( 'Net::HTTP.get', URI.parse('http://www.google.com') );
34
+ blog_source = CallCache.call( 'Net::HTTP.get', :pars => [URI.parse('http://feeds.feedburner.com/adamw523?format=xml')] );
35
+
36
+ p blog_source.size
37
+ p CallCache.call( 'exp_fun', :pars => [1, 10000], :options => {:ttl => 10})
@@ -1,4 +1,9 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3
3
  <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
4
+ <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/1">
5
+ <file>file:/Users/adam/work/callcache/callcache/callcache.gemspec</file>
6
+ <file>file:/Users/adam/work/callcache/callcache/lib/call_cache.rb</file>
7
+ <file>file:/Users/adam/work/callcache/callcache/lib/main.rb</file>
8
+ </open-files>
4
9
  </project-private>
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: callcache
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
6
+ version: 0.2.0
7
7
  date: 2007-03-03 00:00:00 -05:00
8
8
  summary: Ruby library for caching results of a function call
9
9
  require_paths:
@@ -29,7 +29,6 @@ post_install_message:
29
29
  authors:
30
30
  - Adam Wisniewski
31
31
  files:
32
- - callcache-0.1.2.gem
33
32
  - callcache.gemspec
34
33
  - lib
35
34
  - nbproject
data/callcache-0.1.2.gem DELETED
File without changes