phoet-file_cache 0.0.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.rdoc +21 -0
- data/lib/file_cache.rb +71 -0
- data/test/test_file_cache.rb +32 -0
- metadata +56 -0
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= FileCache
|
2
|
+
|
3
|
+
Simple caching utility that persists arbitrary data in files.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
$ gem install file_cache -s http://gems.github.com
|
8
|
+
|
9
|
+
== Dependencies
|
10
|
+
|
11
|
+
none
|
12
|
+
|
13
|
+
== How to use
|
14
|
+
|
15
|
+
Just include the FileCache module and use the provided +file_cache+ method
|
16
|
+
|
17
|
+
file_cache :the_name_of_the_action_you_execute do
|
18
|
+
#some stuff that should be cached, an external API call f.e.
|
19
|
+
end
|
20
|
+
|
21
|
+
The block you are providing is only executed if there is no cache-data provided or if the cache is expired.
|
data/lib/file_cache.rb
ADDED
@@ -0,0 +1,71 @@
|
|
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
|
+
module FileCache
|
19
|
+
|
20
|
+
# configures the directory where file_cache persists the payload
|
21
|
+
attr_accessor :file_cache_dir
|
22
|
+
|
23
|
+
# performs the caching with the name of +token+ for the given +payload+ block
|
24
|
+
#
|
25
|
+
# the +caching_time_in_minutes+ defaults to half an hour
|
26
|
+
def file_cache(token, caching_time_in_minutes=30, &payload)
|
27
|
+
file = file_cache_name(token)
|
28
|
+
if not_cached_or_to_old? file, caching_time_in_minutes
|
29
|
+
load_from_file_cache file
|
30
|
+
else
|
31
|
+
write_to_file_cache file, (yield payload)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# writes given +payload+ to given +file+
|
38
|
+
def write_to_file_cache(file, payload)
|
39
|
+
puts "direct access, storing in #{file}"
|
40
|
+
File.open( file, 'w' ) do |out|
|
41
|
+
Marshal.dump(payload, out)
|
42
|
+
end
|
43
|
+
payload
|
44
|
+
end
|
45
|
+
|
46
|
+
# loads payload from given +file+
|
47
|
+
def load_from_file_cache(file)
|
48
|
+
puts "loading stuff from #{file}"
|
49
|
+
File.open(file, 'r') do |input|
|
50
|
+
Marshal.load(input.read)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# checks if given +file+ already exists or if it expires the +caching_time_in_minutes+
|
55
|
+
def not_cached_or_to_old?(file, caching_time_in_minutes)
|
56
|
+
File.exists?(file) and (File.mtime(file) + caching_time_in_minutes * 60) > Time.new
|
57
|
+
end
|
58
|
+
|
59
|
+
# gets the name of the file-cache for given +token+
|
60
|
+
def file_cache_name(token)
|
61
|
+
name = "#{token}".gsub(/[^\w]/, '_')
|
62
|
+
"#{file_cache_dir}/#{name}.mrs"
|
63
|
+
end
|
64
|
+
|
65
|
+
# gets the dir where to put the file-caches
|
66
|
+
# defaults to _tmpdir_
|
67
|
+
def file_cache_dir
|
68
|
+
@file_cache_dir || Dir.tmpdir
|
69
|
+
end
|
70
|
+
|
71
|
+
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,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: phoet-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-05-16 00:00:00 -07: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
|
+
post_install_message:
|
30
|
+
rdoc_options:
|
31
|
+
- -a
|
32
|
+
- --inline-source
|
33
|
+
- --charset=UTF-8
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project: none
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
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!
|
55
|
+
test_files:
|
56
|
+
- test/test_file_cache.rb
|