file_cache 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +28 -0
- data/lib/file_cache.rb +72 -0
- data/test/test_file_cache.rb +32 -0
- metadata +58 -0
data/README.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= FileCache
|
2
|
+
|
3
|
+
Simple caching utility that persists arbitrary data in files.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
$ gem install phoet-file_cache -s http://gems.github.com
|
8
|
+
|
9
|
+
== How to use
|
10
|
+
|
11
|
+
Just include the FileCache module and use the provided +file_cache+ method
|
12
|
+
|
13
|
+
include FileCache
|
14
|
+
[...]
|
15
|
+
file_cache :the_name_of_the_action_you_execute do
|
16
|
+
#some stuff that should be cached, an external API call f.e.
|
17
|
+
end
|
18
|
+
|
19
|
+
The block you are providing is only executed if there is no cache-data provided or if the cache is expired.
|
20
|
+
By default, the cache expires after half an hour, but you can override this behavior by passing in time you want the content to be cached as a second argument.
|
21
|
+
|
22
|
+
file_cache :the_name_of_the_action_you_execute, 60 do ...
|
23
|
+
|
24
|
+
FileCache persists the return-value of the given block in a file in your local +tmpfolder+.
|
25
|
+
You may override this behavior by setting the +file_cache_dir+.
|
26
|
+
|
27
|
+
self.file_cache_dir='path_to_other_dir'
|
28
|
+
|
data/lib/file_cache.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
|
3
|
+
# = FileCache
|
4
|
+
#
|
5
|
+
# FileCache is a simple utility to persist arbitrary data into a file.
|
6
|
+
# Just wrap the action you want to cache into a FileCache call and it will be available for 30 minutes by default.
|
7
|
+
#
|
8
|
+
# FileCache uses the default tmp-directory of your OS, but you may override that setting via +file_cache_dir+
|
9
|
+
#
|
10
|
+
# == Example
|
11
|
+
#
|
12
|
+
# require 'file_cache'
|
13
|
+
# include FileCache
|
14
|
+
#
|
15
|
+
# file_cache :cache_token_name do
|
16
|
+
# #some_stuff_that_should_be_cached_executed_here
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
module FileCache
|
20
|
+
|
21
|
+
# configures the directory where file_cache persists the payload
|
22
|
+
attr_accessor :file_cache_dir
|
23
|
+
|
24
|
+
# performs the caching with the name of +token+ for the given +payload+ block
|
25
|
+
#
|
26
|
+
# the +caching_time_in_minutes+ defaults to half an hour
|
27
|
+
def file_cache(token, caching_time_in_minutes=30, &payload)
|
28
|
+
file = file_cache_name(token)
|
29
|
+
if not_cached_or_to_old? file, caching_time_in_minutes
|
30
|
+
load_from_file_cache file
|
31
|
+
else
|
32
|
+
write_to_file_cache file, (yield payload)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# writes given +payload+ to given +file+
|
39
|
+
def write_to_file_cache(file, payload)
|
40
|
+
puts "direct access, storing in #{file}"
|
41
|
+
File.open( file, 'w' ) do |out|
|
42
|
+
Marshal.dump(payload, out)
|
43
|
+
end
|
44
|
+
payload
|
45
|
+
end
|
46
|
+
|
47
|
+
# loads payload from given +file+
|
48
|
+
def load_from_file_cache(file)
|
49
|
+
puts "loading stuff from #{file}"
|
50
|
+
File.open(file, 'r') do |input|
|
51
|
+
Marshal.load(input.read)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# checks if given +file+ already exists or if it expires the +caching_time_in_minutes+
|
56
|
+
def not_cached_or_to_old?(file, caching_time_in_minutes)
|
57
|
+
File.exists?(file) and (File.mtime(file) + caching_time_in_minutes * 60) > Time.new
|
58
|
+
end
|
59
|
+
|
60
|
+
# gets the name of the file-cache for given +token+
|
61
|
+
def file_cache_name(token)
|
62
|
+
name = "#{token}".gsub(/[^\w]/, '_')
|
63
|
+
"#{file_cache_dir}/#{name}.mrs"
|
64
|
+
end
|
65
|
+
|
66
|
+
# gets the dir where to put the file-caches
|
67
|
+
# defaults to _tmpdir_
|
68
|
+
def file_cache_dir
|
69
|
+
@file_cache_dir || Dir.tmpdir
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','..','lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'file_cache'
|
5
|
+
|
6
|
+
class TestFileCache < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include FileCache
|
9
|
+
|
10
|
+
def test_file_cache_name_with_strange_tokens_only_characters_remain
|
11
|
+
self.file_cache_dir = 'tmp'
|
12
|
+
assert_equal('tmp/a_b_c.mrs', file_cache_name(:'a+b*c'), 'Expected token to be just charactes and underscores!')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_file_checking_working
|
16
|
+
file = 'timestamp'
|
17
|
+
FileUtils.touch file
|
18
|
+
assert(not_cached_or_to_old?(file, 1), 'Expected File to be existing and not that old!')
|
19
|
+
assert(!not_cached_or_to_old?(file, 0), 'Expected File to be too old!')
|
20
|
+
assert(!not_cached_or_to_old?('other', 1), 'Expected File not to exist!')
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_cache_with_object_tmp_file_is_created
|
24
|
+
file = file_cache_name(:name)
|
25
|
+
File.delete file if File.exists? file
|
26
|
+
payload = [1,2,3]
|
27
|
+
result = file_cache :name do payload end
|
28
|
+
assert_equal(payload, result, 'Expected result to be same as paylaod!')
|
29
|
+
assert(File.exists?(file), 'Expected tmpfile to be created!')
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Peter Schr\xC3\xB6der"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-05 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Caches arbitrary data in files.
|
17
|
+
email: phoetmail@googlemail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- lib/file_cache.rb
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/phoet/file_cache
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- -a
|
34
|
+
- --inline-source
|
35
|
+
- --charset=UTF-8
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project: none
|
53
|
+
rubygems_version: 1.3.5
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Caching is a need for nearly every application. Especially if you are integrating lots of external APIs. So just load the stuff and push it into a file - done!
|
57
|
+
test_files:
|
58
|
+
- test/test_file_cache.rb
|