fscache 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +27 -0
- data/README.md +19 -0
- data/fscache.gemspec +25 -0
- data/lib/fscache.rb +92 -0
- data/spec/fscache_spec.rb +59 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6852a0a360322ada25a04284e5244b4d823481b0
|
4
|
+
data.tar.gz: 47aa94a3ea0271df8ec82e7329c5c5e22b04f01e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e5ba067ece566cfc159f32e0ce6beec1d11f3fe165adef27d8eb7112b60de779dc3174e3a25111596c3fc24987fae30f5ac2561e341208b45c33ab3d248c5ea
|
7
|
+
data.tar.gz: 8579434af0133d29174e90c2bfa575ffd64a8684e97ed353005e57b8c44d1b3e100c2de903d8eed3fc9bcf5733babb2ff5c27feecacf7928bc7be8563b970ea5
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fscache (0.9.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rake (10.4.0)
|
11
|
+
rspec (3.1.0)
|
12
|
+
rspec-core (~> 3.1.0)
|
13
|
+
rspec-expectations (~> 3.1.0)
|
14
|
+
rspec-mocks (~> 3.1.0)
|
15
|
+
rspec-core (3.1.7)
|
16
|
+
rspec-support (~> 3.1.0)
|
17
|
+
rspec-expectations (3.1.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.1.0)
|
20
|
+
rspec-mocks (3.1.3)
|
21
|
+
rspec-support (~> 3.1.0)
|
22
|
+
rspec-support (3.1.2)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
fscache!
|
29
|
+
rake (~> 10.x)
|
30
|
+
rspec (~> 3.x)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Redistribution and use in source and binary forms, with or without
|
2
|
+
modification, are permitted provided that the following conditions are
|
3
|
+
met:
|
4
|
+
|
5
|
+
(1) Redistributions of source code must retain the above copyright
|
6
|
+
notice, this list of conditions and the following disclaimer.
|
7
|
+
|
8
|
+
(2) Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in
|
10
|
+
the documentation and/or other materials provided with the
|
11
|
+
distribution.
|
12
|
+
|
13
|
+
(3)The name of the author may not be used to
|
14
|
+
endorse or promote products derived from this software without
|
15
|
+
specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
18
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
21
|
+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
23
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
24
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
25
|
+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
26
|
+
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27
|
+
POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#FsCache
|
2
|
+
Simple FileCache with that uses similar API to Rails.cache
|
3
|
+
|
4
|
+
#Installation
|
5
|
+
gem install fscache
|
6
|
+
|
7
|
+
Or add the following to your Gemfile (do this for now):
|
8
|
+
|
9
|
+
gem 'fscache', :git => 'git://github.com/eddietejeda/fscache.git'
|
10
|
+
|
11
|
+
#Usage examples
|
12
|
+
|
13
|
+
require 'fscache'
|
14
|
+
filecache = FsCache.new
|
15
|
+
filecache.write("cache_name", "cache_contents")
|
16
|
+
contents = filecache.read("cache_name")
|
17
|
+
filecache.fetch("cache_name") do
|
18
|
+
"cache_contents"
|
19
|
+
end
|
data/fscache.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require File.expand_path('../lib/fscache.rb', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'fscache'
|
6
|
+
gem.version = FsCache::VERSION
|
7
|
+
gem.description = %q(A simple file cache library that serializes objects into temporary directory)
|
8
|
+
gem.summary = gem.description
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split("\n")
|
11
|
+
gem.require_paths = ['lib']
|
12
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
|
15
|
+
gem.homepage = "https://github.com/eddietejeda/fscache"
|
16
|
+
|
17
|
+
gem.license = 'BSD-3-Clause'
|
18
|
+
gem.authors = ['Eddie A Tejeda']
|
19
|
+
gem.email = ['eddie@codeforamerica.org']
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake','~> 10.x'
|
22
|
+
gem.add_development_dependency 'rspec','~> 3.x'
|
23
|
+
|
24
|
+
end
|
25
|
+
|
data/lib/fscache.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'digest'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'pathname'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
class FsCache
|
7
|
+
|
8
|
+
VERSION = "0.9.0"
|
9
|
+
|
10
|
+
def initialize(path = nil, expire = 7, debug = false)
|
11
|
+
@file_cache_expire = expire
|
12
|
+
@debug = debug
|
13
|
+
default_tmp = defined?(Rails).nil? ? Pathname.new("/tmp").join('file_cache') : Rails.root.join('tmp').join('file_cache')
|
14
|
+
@file_cache_dir = path.nil? ? default_tmp : path
|
15
|
+
FileUtils::mkdir_p @file_cache_dir if !File.directory?(@file_cache_dir)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
def exist?(cache_name)
|
21
|
+
cache_name = cache_key( cache_name )
|
22
|
+
cache_path = @file_cache_dir.join(cache_name)
|
23
|
+
|
24
|
+
cache_valid(cache_path)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def read(cache_name)
|
29
|
+
cache_name = cache_key( cache_name )
|
30
|
+
cache_path = @file_cache_dir.join(cache_name)
|
31
|
+
|
32
|
+
if @debug
|
33
|
+
p exist?(cache_name) ? "#{cache_name}: cache hit" : "#{cache_name}: cache miss"
|
34
|
+
end
|
35
|
+
|
36
|
+
if File.exist?(cache_path)
|
37
|
+
YAML.load(File.read(cache_path))
|
38
|
+
else
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def write(cache_name, cache_value)
|
45
|
+
cache_name = cache_key( cache_name )
|
46
|
+
cache_path = @file_cache_dir.join(cache_name)
|
47
|
+
|
48
|
+
File.open(cache_path, 'w') do |f|
|
49
|
+
f.write(YAML.dump(cache_value))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def fetch(cache_name)
|
55
|
+
cache_name = cache_key( cache_name )
|
56
|
+
|
57
|
+
if(exist? (cache_name) )
|
58
|
+
read (cache_name)
|
59
|
+
else
|
60
|
+
value = yield
|
61
|
+
write(cache_name, value)
|
62
|
+
read (cache_name)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def clear
|
67
|
+
Dir["#{@file_cache_dir}/*"].each do |cache_name|
|
68
|
+
# lets make sure we're only deleting files with md5 hashes as filenames.
|
69
|
+
if cache_key( File.basename(cache_name) )
|
70
|
+
FileUtils.rm(cache_name)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# --------- private ---------
|
76
|
+
private
|
77
|
+
def cache_valid(filename)
|
78
|
+
if File.exist?(filename)
|
79
|
+
((Time.now - File.stat(filename).mtime).to_i / 86400.0).to_i < @file_cache_expire
|
80
|
+
else
|
81
|
+
return false
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def cache_key(key)
|
86
|
+
if(key.is_a?(String) && key.downcase.match(/^[a-f0-9]{32}$/) )
|
87
|
+
key
|
88
|
+
else
|
89
|
+
Digest::MD5.hexdigest(key.to_s).to_s
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# require 'spec_helper'
|
2
|
+
require_relative '../lib/fscache'
|
3
|
+
|
4
|
+
describe FsCache do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@fscache = FsCache.new
|
8
|
+
@cache_name = ['spec', 'test']
|
9
|
+
@cached_contents = 'Hello World'
|
10
|
+
@file_cache_dir = @fscache.instance_variable_get(:@file_cache_dir)
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "test methods" do
|
15
|
+
|
16
|
+
it "FsCache#initilize should create a tmp directory" do
|
17
|
+
expect(File.directory?(@file_cache_dir)).to be_truthy
|
18
|
+
end
|
19
|
+
|
20
|
+
it "FsCache#write should save serialized object to tmp" do
|
21
|
+
@fscache.write(@cache_name, @cached_contents)
|
22
|
+
expect(@fscache.exist?(@cache_name)).to be_truthy
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
it "FsCache#read should be able to read serialized objects" do
|
27
|
+
@fscache.read(@cache_name).eql?( @cached_contents)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
it "FsCache#fetch should be able to read serialized objects" do
|
32
|
+
|
33
|
+
cache_name_fetch = "#{@cache_name}-fetch"
|
34
|
+
cache_contents_fetch = "Hello World Fetch"
|
35
|
+
|
36
|
+
response = @fscache.fetch( cache_name_fetch) do
|
37
|
+
cache_contents_fetch
|
38
|
+
end
|
39
|
+
|
40
|
+
cache_name_fetch.eql?( response)
|
41
|
+
@fscache.read(cache_name_fetch).eql?( response)
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
it "FsCache#clear should clear cache" do
|
47
|
+
@fscache.clear
|
48
|
+
expect(@fscache.exist?(@cache_name)).to be_falsey
|
49
|
+
end
|
50
|
+
|
51
|
+
# it "FsCache#clear should clear cache" do
|
52
|
+
#
|
53
|
+
# expect(@fscache.VERSION).to be_truthy
|
54
|
+
# end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fscache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eddie A Tejeda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 10.x
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 10.x
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.x
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.x
|
41
|
+
description: A simple file cache library that serializes objects into temporary directory
|
42
|
+
email:
|
43
|
+
- eddie@codeforamerica.org
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- fscache.gemspec
|
54
|
+
- lib/fscache.rb
|
55
|
+
- spec/fscache_spec.rb
|
56
|
+
homepage: https://github.com/eddietejeda/fscache
|
57
|
+
licenses:
|
58
|
+
- BSD-3-Clause
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.2.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: A simple file cache library that serializes objects into temporary directory
|
80
|
+
test_files:
|
81
|
+
- spec/fscache_spec.rb
|