Module: CallCache
- Defined in:
- lib/cached_call.rb,
lib/call_cache.rb
Overview
Copyright 2007 Adam Wisniewski <[email protected]>
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Defined Under Namespace
Classes: CachedCall
Constant Summary collapse
- CACHE_DIRECTORY =
"/tmp/cache"
- SECONDS_TO_CACHE =
60 * 60 * 3
Class Method Summary collapse
- .call(function_call, hashes) ⇒ Object
-
.file_path_for(function_call, *parameters) ⇒ Object
def CallCache.check_or_create_tmp_dir file_path = CallCache::CACHE_DIRECTORY + File::SEPARATOR + “call_cache_” + id_string end.
- .find(function_call, *parameters) ⇒ Object
- .get_id_for(function_call, *parameters) ⇒ Object
- .make_call(function_call, *parameters) ⇒ Object
- .save(cached_call) ⇒ Object
Class Method Details
.call(function_call, hashes) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/call_cache.rb', line 27 def CallCache.call( function_call, hashes ) parameters = hashes[:pars] = hashes[:options] ttl = SECONDS_TO_CACHE if !.nil? if [:ttl] ttl = [:ttl] end end cached_call = CallCache.find( function_call, *parameters ) if cached_call == nil || Time.now.to_i - cached_call.created_at.to_i > ttl return_val = CallCache.make_call( function_call, *parameters ) cached_call = CachedCall.new( return_val, function_call, *parameters ) CallCache.save( cached_call ) end return cached_call.return_val end |
.file_path_for(function_call, *parameters) ⇒ Object
def CallCache.check_or_create_tmp_dir
file_path = CallCache::CACHE_DIRECTORY + File::SEPARATOR + "call_cache_" + id_string
end
61 62 63 64 |
# File 'lib/call_cache.rb', line 61 def CallCache.file_path_for( function_call, *parameters ) CallCache::CACHE_DIRECTORY + File::SEPARATOR + "call_cache_" + CallCache.get_id_for( function_call, *parameters ) end |
.find(function_call, *parameters) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/call_cache.rb', line 66 def CallCache.find( function_call, *parameters ) cached_call = nil file_path = CallCache.file_path_for(function_call, *parameters) if File.exist?( file_path ) # return saved cached result cached_call = Marshal.load( File.read( file_path ) ) end return cached_call end |
.get_id_for(function_call, *parameters) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/call_cache.rb', line 50 def CallCache.get_id_for( function_call, *parameters) md5 = Digest::MD5::new md5.update( function_call.gsub('::', '_').gsub('.', '_') ) md5.update( Marshal.dump( parameters ) ) return md5.hexdigest end |
.make_call(function_call, *parameters) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/call_cache.rb', line 93 def CallCache.make_call( function_call, *parameters ) # call function # parse call to handle local methods or static class methods function_call.gsub!( '::', '.' ) function_call_split = function_call.split(/\./) # make the call return_val = eval( function_call_split[0..-2].join('::') ).method( function_call_split[-1] ).call( *parameters ) end |
.save(cached_call) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/call_cache.rb', line 79 def CallCache.save( cached_call ) # write call to cache location if !File.directory?( CallCache::CACHE_DIRECTORY ) Dir.mkdir( CallCache::CACHE_DIRECTORY ) end file_path = CallCache.file_path_for( cached_call.function_call, *cached_call.parameters ) dest_file = File.new( file_path, 'wb' ) dest_file.write( Marshal.dump( cached_call ) ) dest_file.close end |