s3cache 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +27 -0
- data/README.md +22 -0
- data/lib/s3cache.rb +119 -0
- data/s3cache.gemspec +25 -0
- data/spec/s3cache_spec.rb +46 -0
- metadata +12 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2864f8ce4c8dcfeff913d831aa3f9b1b9e3adbb8
|
4
|
+
data.tar.gz: 448bb973a9b930f59cc79e67f9d935e49fe1e6a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29dff18cf4bdf462a3b2e7763b37d675df6314b5b90b0b9fc7499942c90d0e409ce1a596c0dd83c07231791e49ecfa88f91204f0c349444565b617cf19ec280d
|
7
|
+
data.tar.gz: 7135a8ec19821db2a4544476f32e6063faf3ca62e9a5bc70edd4405865af81baf1b546539020606e828253b2301d20e9a40f1366907241cd2935b701d534af4b
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
s3cache (0.9.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
aws-sdk (2.2.24)
|
10
|
+
aws-sdk-resources (= 2.2.24)
|
11
|
+
aws-sdk-core (2.2.24)
|
12
|
+
jmespath (~> 1.0)
|
13
|
+
aws-sdk-resources (2.2.24)
|
14
|
+
aws-sdk-core (= 2.2.24)
|
15
|
+
diff-lcs (1.2.5)
|
16
|
+
jmespath (1.1.3)
|
17
|
+
rake (10.4.0)
|
18
|
+
rspec (3.1.0)
|
19
|
+
rspec-core (~> 3.1.0)
|
20
|
+
rspec-expectations (~> 3.1.0)
|
21
|
+
rspec-mocks (~> 3.1.0)
|
22
|
+
rspec-core (3.1.7)
|
23
|
+
rspec-support (~> 3.1.0)
|
24
|
+
rspec-expectations (3.1.2)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.1.0)
|
27
|
+
rspec-mocks (3.1.3)
|
28
|
+
rspec-support (~> 3.1.0)
|
29
|
+
rspec-support (3.1.2)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
aws-sdk (~> 2)
|
36
|
+
rake (~> 10.x)
|
37
|
+
rspec (~> 3.x)
|
38
|
+
s3cache!
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
1.10.6
|
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,22 @@
|
|
1
|
+
#S3Cache
|
2
|
+
A simple caching library that serializes objects to the filesystem and is compatible with the Rails.cache API
|
3
|
+
|
4
|
+
#Installation
|
5
|
+
gem install s3cache
|
6
|
+
|
7
|
+
Or add the following to your Gemfile (do this for now):
|
8
|
+
|
9
|
+
gem 's3cache', :git => 'git://github.com/eddietejeda/s3cache.git'
|
10
|
+
|
11
|
+
#Usage examples
|
12
|
+
|
13
|
+
require 's3cache'
|
14
|
+
cache = S3Cache.new({:bucket_name => "aws-bucket-name"})
|
15
|
+
|
16
|
+
cache.write("cache_name", "cache_contents")
|
17
|
+
|
18
|
+
contents = filecache.read("cache_name")
|
19
|
+
|
20
|
+
filecache.fetch("cache_name") do
|
21
|
+
"cache_contents"
|
22
|
+
end
|
data/lib/s3cache.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'digest'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'pathname'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
class S3Cache
|
7
|
+
|
8
|
+
VERSION = "0.9.1"
|
9
|
+
|
10
|
+
def initialize(**params)
|
11
|
+
|
12
|
+
if not params.to_h[:bucket_name]
|
13
|
+
puts "requires {:bucket_name => String, (optional) :expires => Integer, (optional) :debug => Boolean }"
|
14
|
+
end
|
15
|
+
|
16
|
+
@bucket_name = params.to_h[:bucket_name]
|
17
|
+
@expires = params.to_h[:expires] ? params.to_h[:expires] : 32.days
|
18
|
+
@debug = params.to_h[:debug] ? params.to_h[:debug] : false
|
19
|
+
|
20
|
+
if ENV['AWS_ACCESS_KEY_ID'].nil? || ENV['AWS_SECRET_ACCESS_KEY'].nil?
|
21
|
+
puts 'Enviroment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be defined. Learn more http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#config-settings-and-precedence'
|
22
|
+
else
|
23
|
+
@s3 = Aws::S3::Client.new
|
24
|
+
if not bucket_exist?
|
25
|
+
puts "creating bucket #{@bucket_name}" if @debug
|
26
|
+
bucket_create
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def read(key, **params)
|
32
|
+
key_name = cache_key( key )
|
33
|
+
puts "read #{key_name}" if @debug
|
34
|
+
if exist?(key_name)
|
35
|
+
Marshal.load(@s3.get_object({ bucket: @bucket_name, key: key_name }).body.read)
|
36
|
+
else
|
37
|
+
return false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def write(key, contents, **params)
|
42
|
+
key_name = cache_key( key )
|
43
|
+
puts "write #{key_name}" if @debug
|
44
|
+
@s3.put_object({
|
45
|
+
bucket: @bucket_name,
|
46
|
+
key: key_name,
|
47
|
+
expires: Time.now + @expires.days,
|
48
|
+
body: Marshal.dump(contents),
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
52
|
+
def fetch(key, **params)
|
53
|
+
key_name = cache_key( key )
|
54
|
+
|
55
|
+
if(exist?(key_name) )
|
56
|
+
puts "fetch->read #{key_name}" if @debug
|
57
|
+
read (key_name)
|
58
|
+
else
|
59
|
+
puts "fetch->write #{key_name}" if @debug
|
60
|
+
value = yield
|
61
|
+
write(key_name, value)
|
62
|
+
read (key_name)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def exist?(key)
|
67
|
+
key_name = cache_key( key )
|
68
|
+
begin
|
69
|
+
response = @s3.get_object({:bucket => @bucket_name, :key => key_name})
|
70
|
+
rescue
|
71
|
+
response = nil
|
72
|
+
end
|
73
|
+
puts "exists? #{response} #{key_name}" if @debug
|
74
|
+
return !response.nil?
|
75
|
+
end
|
76
|
+
|
77
|
+
def clear
|
78
|
+
cache_keys.each do |key|
|
79
|
+
puts "deleting key #{key}" if @debug
|
80
|
+
@s3.delete_object({:bucket => @bucket_name, :key => key})
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# --------- private ---------
|
85
|
+
private
|
86
|
+
|
87
|
+
def cache_keys
|
88
|
+
@s3.list_objects({:bucket => @bucket_name}).first[:contents].collect{|e|e.key}
|
89
|
+
end
|
90
|
+
|
91
|
+
def cache_valid(key)
|
92
|
+
exist?(key)
|
93
|
+
end
|
94
|
+
|
95
|
+
def cache_key(key)
|
96
|
+
if(key.is_a?(String) && key.downcase.match(/^[a-f0-9]{64}$/) )
|
97
|
+
key
|
98
|
+
else
|
99
|
+
Digest::SHA256.hexdigest(key.to_s).to_s
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def bucket_create
|
104
|
+
@s3.create_bucket({
|
105
|
+
acl: "private", # accepts private, public-read, public-read-write, authenticated-read
|
106
|
+
bucket: @bucket_name, # required
|
107
|
+
})
|
108
|
+
end
|
109
|
+
|
110
|
+
def bucket_exist?
|
111
|
+
begin
|
112
|
+
response = @s3.head_bucket({ bucket: @bucket_name })
|
113
|
+
rescue
|
114
|
+
response = nil
|
115
|
+
end
|
116
|
+
return !response.nil?
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
data/s3cache.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require File.expand_path('../lib/s3cache.rb', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 's3cache'
|
6
|
+
gem.version = S3Cache::VERSION
|
7
|
+
gem.description = %q(A simple caching library that serializes objects to the filesystem and is compatible with the Rails.cache API)
|
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/s3cache"
|
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
|
+
gem.add_development_dependency 'aws-sdk','~> 2'
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# I can't figure out how to test s3 modules without requiring a bunch of new gems.
|
2
|
+
# Should look into fake-s3 gem
|
3
|
+
require 'aws-sdk'
|
4
|
+
|
5
|
+
# require 'spec_helper'
|
6
|
+
require_relative '../lib/s3cache'
|
7
|
+
|
8
|
+
describe S3Cache do
|
9
|
+
|
10
|
+
before do
|
11
|
+
# @s3 = Aws::S3::Client.new(stub_responses: true)
|
12
|
+
# @s3cache = S3Cache.new({:bucket_name => 'test-s3-cache'})
|
13
|
+
# @cache_name = ['spec', 'test']
|
14
|
+
# @cached_contents = 'Hello World'
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "test methods" do
|
18
|
+
|
19
|
+
it "S3Cache#write should save serialized object to tmp" do
|
20
|
+
# @s3cache.write(@cache_name, @cached_contents)
|
21
|
+
# expect(@s3cache.exist?(@cache_name)).to be_truthy
|
22
|
+
end
|
23
|
+
|
24
|
+
it "S3Cache#read should be able to read serialized objects" do
|
25
|
+
# @s3cache.read(@cache_name).eql?( @cached_contents)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "S3Cache#fetch should be able to read serialized objects" do
|
29
|
+
# cache_name_fetch = "#{@cache_name}-fetch"
|
30
|
+
# cache_contents_fetch = "Hello World Fetch"
|
31
|
+
#
|
32
|
+
# response = @s3cache.fetch( cache_name_fetch) do
|
33
|
+
# cache_contents_fetch
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# cache_name_fetch.eql?( response)
|
37
|
+
# @s3cache.read(cache_name_fetch).eql?( response)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "S3Cache#clear should clear cache" do
|
41
|
+
# @s3cache.clear
|
42
|
+
# expect(@s3cache.exist?(@cache_name)).to be_falsey
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s3cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eddie A Tejeda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -59,7 +59,15 @@ email:
|
|
59
59
|
executables: []
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
|
-
files:
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- lib/s3cache.rb
|
69
|
+
- s3cache.gemspec
|
70
|
+
- spec/s3cache_spec.rb
|
63
71
|
homepage: https://github.com/eddietejeda/s3cache
|
64
72
|
licenses:
|
65
73
|
- BSD-3-Clause
|
@@ -86,3 +94,4 @@ specification_version: 4
|
|
86
94
|
summary: A simple caching library that serializes objects to the filesystem and is
|
87
95
|
compatible with the Rails.cache API
|
88
96
|
test_files: []
|
97
|
+
has_rdoc:
|