cache-aws-s3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cache-aws-s3.gemspec
4
+ gemspec
@@ -0,0 +1,34 @@
1
+ #cache-aws-s3
2
+
3
+ This gem adds local file caching for the AWS::S3 library. The best use case is if you use S3 to store the readonly/library copy of some object that you need occasionally transform (resize, retouch, etc).
4
+
5
+ Usage is simple, just add the gem to your bundle
6
+
7
+ require 'cache-aws-s3'
8
+
9
+ From there on, all (supported) calls to AWS::S3::S3Object will be first checked against the cache
10
+
11
+ S3FileCache.enable true
12
+ S3FileCache.cache_dir /var/s3filecache
13
+
14
+ AWS::S3::S3Object.value 'key', 'bucket'
15
+
16
+ AWS::S3::S3Object.cached_local? 'key', 'bucket'
17
+ AWS::S3::S3Object.perge_local! 'key', 'bucket'
18
+
19
+
20
+ ##Note on Patches/Pull Requests
21
+
22
+ * Fork the project.
23
+ * Make your feature addition or bug fix.
24
+ * Add spec for it. This is important so I don't break it in a
25
+ future version unintentionally.
26
+ * Commit, do not mess with rakefile, version, or history.
27
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
28
+ * Send me a pull request. Bonus points for topic branches.
29
+
30
+ ##Copyright
31
+
32
+ Copyright (c) 2011 Kyle Fritz
33
+
34
+ This gem was heavily inspired by [mock-aws-s3](https://github.com/jkrall/mock-aws-s3)
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cache/aws/s3/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cache-aws-s3"
7
+ s.version = Cache::AWS::S3::VERSION
8
+ s.authors = ["Kyle Fritz"]
9
+ s.email = ["kyle.p.fritz@gmail.com"]
10
+ s.homepage = "https://github.com/kylefritz/cache-aws-s3"
11
+ s.summary = %q{local cache for s3 data}
12
+ s.description = %q{speed up repeated access to s3 data with a local cache}
13
+
14
+ s.rubyforge_project = "cache-aws-s3"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "aws-s3"
22
+
23
+ s.add_development_dependency "rspec"
24
+ #s.add_development_dependency "cucumber"
25
+ end
@@ -0,0 +1,2 @@
1
+ require 'aws/s3'
2
+ require 'cache/aws/s3/object'
@@ -0,0 +1,78 @@
1
+ require 'fileutils'
2
+ require 'ostruct'
3
+
4
+ class S3FileCache
5
+ class << self
6
+ attr_accessor :enabled, :cache_dir
7
+ def enabled?
8
+ self.enabled
9
+ end
10
+ end
11
+ @enabled=true
12
+ @cache_dir = "./tmp"
13
+ end
14
+
15
+ module AWS
16
+ module S3
17
+
18
+ class S3Object
19
+ class << self
20
+
21
+ #save old value method
22
+ alias s3_value value
23
+
24
+ #TODO: ignoring the &block for the moment
25
+ def value(key, bucket = nil, options = {}, &block)
26
+
27
+ return s3_value(key,bucket,options,&block) if not S3FileCache.enabled?
28
+
29
+ if cached_local? key, bucket
30
+ #return cached object
31
+ read_cache(key,bucket,options)
32
+ else
33
+ #1. open normal S3 file
34
+ value = s3_value(key,bucket,options) #omitting the block for now
35
+
36
+ #2. save local to file_path! (make parent directories)
37
+ cache_local(value,key,bucket,options)
38
+
39
+ #3. return the value object
40
+ value
41
+ end
42
+ end
43
+
44
+ def read_cache(key,bucket,options={})
45
+ data = File.open(file_path!(key,bucket, options)) {|f| f.read}
46
+ Value.new OpenStruct.new(:body=>data)
47
+ end
48
+
49
+ def cache_local(value,key,bucket,options={})
50
+ fp=file_path!(key,bucket,options)
51
+ FileUtils.mkdir_p File.dirname fp
52
+ File.open(fp,'w') {|io| io.write value.response}
53
+ end
54
+
55
+ def file_path!(key,bucket, options = {}) #:nodoc:
56
+ # We're using the second argument for options
57
+ if bucket.is_a?(Hash)
58
+ options.replace(bucket)
59
+ bucket = nil
60
+ end
61
+ File.join(S3FileCache.cache_dir, bucket_name(bucket), key)
62
+ end
63
+
64
+ def perge_local!(key,bucket)
65
+ #return delete file at file_path!(bucket,name)
66
+ file_path= file_path!(key,bucket)
67
+ File.unlink file_path if File.exists? file_path
68
+ end
69
+
70
+ def cached_local?(key,bucket)
71
+ #return file exists? at file_path!(bucket,name)
72
+ File.exists? file_path!(key,bucket)
73
+ end
74
+
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,7 @@
1
+ module Cache
2
+ module AWS
3
+ module S3
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Cache::AWS::S3" do
4
+
5
+ describe 'when using S3Object' do
6
+ before (:each){
7
+ S3FileCache.enabled=true
8
+ @bucket="mittens"
9
+ @key="these/are/our/vista.txt"
10
+ @fp=AWS::S3::S3Object.file_path! @key,@bucket
11
+ FileUtils.mkdir_p File.dirname @fp
12
+ File.open(@fp, 'w') {|f| f.write 'test data'}
13
+ }
14
+ after (:each){
15
+ File.unlink @fp
16
+ }
17
+
18
+ it 'resolves files locally' do
19
+ @fp.should == "#{S3FileCache.cache_dir}/#{@bucket}/#{@key}"
20
+ end
21
+ it 'should hit local cache first' do
22
+ AWS::S3::S3Object.should_not_receive(:get)
23
+ AWS::S3::S3Object.should_receive(:read_cache)
24
+ AWS::S3::S3Object.value @key, @bucket
25
+ end
26
+ it 'passes through to regular aws when disabled' do
27
+ S3FileCache.enabled=false
28
+ lambda { AWS::S3::S3Object.value( @key, @bucket)}.should raise_error(AWS::S3::NoConnectionEstablished)
29
+ end
30
+ it 'reads data out of local cache' do
31
+ val=AWS::S3::S3Object.read_cache @key, @bucket
32
+ val.should == 'test data'
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'cache-aws-s3'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache-aws-s3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kyle Fritz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: aws-s3
16
+ requirement: &2211214360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2211214360
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2211209740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2211209740
36
+ description: speed up repeated access to s3 data with a local cache
37
+ email:
38
+ - kyle.p.fritz@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.md
46
+ - Rakefile
47
+ - cache-aws-s3.gemspec
48
+ - lib/cache-aws-s3.rb
49
+ - lib/cache/aws/s3/object.rb
50
+ - lib/cache/aws/s3/version.rb
51
+ - spec/cache-aws-s3_spec.rb
52
+ - spec/spec.opts
53
+ - spec/spec_helper.rb
54
+ homepage: https://github.com/kylefritz/cache-aws-s3
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project: cache-aws-s3
74
+ rubygems_version: 1.8.11
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: local cache for s3 data
78
+ test_files:
79
+ - spec/cache-aws-s3_spec.rb
80
+ - spec/spec.opts
81
+ - spec/spec_helper.rb