s3_cache_store 0.1.0
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.
- checksums.yaml +7 -0
- data/.github/workflows/lint.yml +31 -0
- data/.github/workflows/publish.yml +41 -0
- data/.github/workflows/test.yml +38 -0
- data/.gitignore +14 -0
- data/.standard.yml +4 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +23 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +11 -0
- data/benchmark.rb +58 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_support/cache/s3_cache_store.rb +94 -0
- data/lib/s3_cache_store/s3_client.rb +66 -0
- data/lib/s3_cache_store/version.rb +3 -0
- data/s3_cache_store.gemspec +42 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9b156780f94493bfeaaa330c0ddacd8b99fe61306ce5074f749c53a4eca3cce0
|
4
|
+
data.tar.gz: e8398c595a879bc570a0006374aede2c1cec81b0c70923ecb26410c51970b330
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c7626e82b4ae636ead19411fb034dbca253223feeb896c90ac0d6b0882c6cf0459d65460681a9c705cf977425418cd2f581a95f9b600855a66c591cab400292
|
7
|
+
data.tar.gz: a29b3f1e46f3de5da463d77caec9aaedcae93a60d3924a185facf2ce20f01d14a982740d8239b7ed866ed6a663edaf01f309a8d90699a087a3433a2ddb866f85
|
@@ -0,0 +1,31 @@
|
|
1
|
+
name: Lint
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [master]
|
6
|
+
pull_request:
|
7
|
+
paths:
|
8
|
+
- '.standard.yml'
|
9
|
+
- 'Gemfile.lock'
|
10
|
+
- '**/*.rb'
|
11
|
+
- '.github/workflows/lint.yml'
|
12
|
+
|
13
|
+
jobs:
|
14
|
+
lint:
|
15
|
+
runs-on: ubuntu-latest
|
16
|
+
strategy:
|
17
|
+
matrix:
|
18
|
+
ruby-version:
|
19
|
+
- '2.7'
|
20
|
+
- '3.0'
|
21
|
+
- '3.1'
|
22
|
+
- '3.2'
|
23
|
+
- '3.3'
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v4
|
26
|
+
- uses: ruby/setup-ruby@v1
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
29
|
+
bundler-cache: true
|
30
|
+
- name: Run standard
|
31
|
+
run: bundle exec rake standard
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# refs: https://ybiquitous.me/blog/2022/release-gem-using-github-actions
|
2
|
+
name: Publish
|
3
|
+
|
4
|
+
on:
|
5
|
+
workflow_dispatch:
|
6
|
+
inputs:
|
7
|
+
otp:
|
8
|
+
description: "One-time password for RubyGems.org"
|
9
|
+
required: true
|
10
|
+
|
11
|
+
concurrency:
|
12
|
+
group: ${{ github.workflow }}
|
13
|
+
cancel-in-progress: true
|
14
|
+
|
15
|
+
jobs:
|
16
|
+
release:
|
17
|
+
runs-on: ubuntu-latest
|
18
|
+
permissions:
|
19
|
+
contents: write
|
20
|
+
steps:
|
21
|
+
- name: Checkout
|
22
|
+
uses: actions/checkout@v4
|
23
|
+
|
24
|
+
- name: Set up Ruby
|
25
|
+
uses: ruby/setup-ruby@v1
|
26
|
+
with:
|
27
|
+
ruby-version: '3.1'
|
28
|
+
bundler-cache: true
|
29
|
+
|
30
|
+
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-ruby#publishing-gems
|
31
|
+
- name: Publish to RubyGems
|
32
|
+
run: |
|
33
|
+
mkdir -p $HOME/.gem
|
34
|
+
touch $HOME/.gem/credentials
|
35
|
+
chmod 0600 $HOME/.gem/credentials
|
36
|
+
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
37
|
+
gem build *.gemspec
|
38
|
+
gem push --otp "${GEM_HOST_OTP_CODE}" *.gem
|
39
|
+
env:
|
40
|
+
GEM_HOST_API_KEY: "${{secrets.GEM_HOST_API_KEY}}"
|
41
|
+
GEM_HOST_OTP_CODE: "${{ inputs.otp }}"
|
@@ -0,0 +1,38 @@
|
|
1
|
+
name: Test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [master]
|
6
|
+
pull_request:
|
7
|
+
paths:
|
8
|
+
- 'Gemfile.lock'
|
9
|
+
- '**/*.rb'
|
10
|
+
- '.github/workflows/test.yml'
|
11
|
+
|
12
|
+
env:
|
13
|
+
AWS_REGION: ap-northeast-1
|
14
|
+
AWS_S3_BUCKET: "${{secrets.AWS_S3_BUCKET}}"
|
15
|
+
AWS_ACCESS_KEY_ID: "${{secrets.AWS_ACCESS_KEY_ID}}"
|
16
|
+
AWS_SECRET_ACCESS_KEY: "${{secrets.AWS_SECRET_ACCESS_KEY}}"
|
17
|
+
|
18
|
+
jobs:
|
19
|
+
test:
|
20
|
+
runs-on: ubuntu-latest
|
21
|
+
strategy:
|
22
|
+
matrix:
|
23
|
+
ruby-version:
|
24
|
+
- '2.7'
|
25
|
+
- '3.0'
|
26
|
+
- '3.1'
|
27
|
+
- '3.2'
|
28
|
+
- '3.3'
|
29
|
+
fail-fast: true
|
30
|
+
max-parallel: 1 # because conflict cache key
|
31
|
+
steps:
|
32
|
+
- uses: actions/checkout@v4
|
33
|
+
- uses: ruby/setup-ruby@v1
|
34
|
+
with:
|
35
|
+
ruby-version: ${{ matrix.ruby-version }}
|
36
|
+
bundler-cache: true
|
37
|
+
- name: Run minitest
|
38
|
+
run: bundle exec rake test
|
data/.gitignore
ADDED
data/.standard.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at ryoheisonoda@outlook.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in s3_cache_store.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# https://github.com/rails/rails/pull/45370
|
7
|
+
if RUBY_VERSION < "3"
|
8
|
+
gem "minitest", ">= 5.15.0", "< 5.16"
|
9
|
+
else
|
10
|
+
gem "minitest", ">= 5.15.0"
|
11
|
+
end
|
12
|
+
|
13
|
+
# for development
|
14
|
+
gem "rake", "~> 13.0"
|
15
|
+
gem "standard", "~> 1.3"
|
16
|
+
|
17
|
+
# for test
|
18
|
+
gem "msgpack"
|
19
|
+
gem "dalli"
|
20
|
+
|
21
|
+
# for benchmark
|
22
|
+
gem "redis"
|
23
|
+
gem "redis-clustering"
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Yusuke Nakamura
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# S3CacheStore
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/s3_cache_store`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 's3_cache_store'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install s3_cache_store
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
```shell
|
30
|
+
export AWS_REGION=ap-northeast-1
|
31
|
+
export AWS_ACCESS_KEY_ID=xxxxxxx
|
32
|
+
export AWS_SECRET_ACCESS_KEY=xxxxxxx
|
33
|
+
export AWS_S3_BUCKET=xxxxxxx
|
34
|
+
bundle exec rake test
|
35
|
+
```
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sonodar/s3_cache_store. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
48
|
+
|
49
|
+
## Code of Conduct
|
50
|
+
|
51
|
+
Everyone interacting in the S3CacheStore project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sonodar/s3_cache_store/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/benchmark.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Usage:
|
4
|
+
# export AWS_ACCESS_KEY_ID=xxxxxxx ...
|
5
|
+
# export REDIS_URL=rediss://:xxxxxx@xxxx.com:6379/0
|
6
|
+
# export AWS_S3_GENERAL_BUCKET=general-bucket-name
|
7
|
+
# export AWS_S3_DIRECTORY_BUCKET=directory-bucket-name
|
8
|
+
# bundle exec ruby benchmark.rb | tee benchmark.txt
|
9
|
+
require "active_support"
|
10
|
+
require "active_support/cache/redis_cache_store"
|
11
|
+
require "active_support/cache/s3_cache_store"
|
12
|
+
require "redis"
|
13
|
+
require "redis-clustering"
|
14
|
+
|
15
|
+
COUNT = 1000
|
16
|
+
KEYS = COUNT.times.map { "{benchmark}:#{SecureRandom.hex(16)}" }
|
17
|
+
|
18
|
+
puts "Generated #{COUNT} keys"
|
19
|
+
|
20
|
+
# @param [ActiveSupport::Cache::Store] store
|
21
|
+
def bench(store, subject)
|
22
|
+
puts "Start #{subject} benchmark"
|
23
|
+
|
24
|
+
start = Time.now
|
25
|
+
KEYS.each { |key| store.write(key, key) }
|
26
|
+
duration = Time.now - start
|
27
|
+
puts "#{subject} duration: #{duration} sec (#{duration / COUNT} s/key write)"
|
28
|
+
|
29
|
+
start = Time.now
|
30
|
+
KEYS.each { |key| store.read(key) }
|
31
|
+
duration = Time.now - start
|
32
|
+
puts "#{subject} duration: #{duration} sec (#{duration / COUNT} s/key read)"
|
33
|
+
|
34
|
+
start = Time.now
|
35
|
+
KEYS.each { |key| store.delete(key) }
|
36
|
+
duration = Time.now - start
|
37
|
+
puts "#{subject} duration: #{duration} sec (#{duration / COUNT} s/key delete)"
|
38
|
+
end
|
39
|
+
|
40
|
+
def bench_redis_cache_store
|
41
|
+
redis = Redis::Cluster.new(nodes: [ENV["REDIS_URL"]])
|
42
|
+
store = ActiveSupport::Cache::RedisCacheStore.new(redis: redis)
|
43
|
+
bench(store, store.class.name)
|
44
|
+
end
|
45
|
+
|
46
|
+
def bench_s3_cache_store
|
47
|
+
store = ActiveSupport::Cache::S3CacheStore.new(bucket: ENV["AWS_S3_GENERAL_BUCKET"], prefix: "benchmark/")
|
48
|
+
bench(store, store.class.name + " (general)")
|
49
|
+
end
|
50
|
+
|
51
|
+
def bench_s3_express_cache_store
|
52
|
+
store = ActiveSupport::Cache::S3CacheStore.new(bucket: ENV["AWS_S3_DIRECTORY_BUCKET"], prefix: "benchmark/")
|
53
|
+
bench(store, store.class.name + " (express)")
|
54
|
+
end
|
55
|
+
|
56
|
+
bench_redis_cache_store
|
57
|
+
bench_s3_cache_store
|
58
|
+
bench_s3_express_cache_store
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "s3_cache_store"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "s3_cache_store/s3_client"
|
4
|
+
require "active_support"
|
5
|
+
require "aws-sdk-s3"
|
6
|
+
require "pathname"
|
7
|
+
require "tempfile"
|
8
|
+
require "uri"
|
9
|
+
require "digest/sha2"
|
10
|
+
|
11
|
+
module ActiveSupport
|
12
|
+
module Cache
|
13
|
+
# = \S3 \Cache \Store
|
14
|
+
#
|
15
|
+
# A cache store implementation which stores everything on the Amazon S3 Bucket.
|
16
|
+
class S3CacheStore < Store
|
17
|
+
attr_reader :s3_client
|
18
|
+
|
19
|
+
OBJECT_KEY_MAX_SIZE = 900 # max is 1024, plus some room
|
20
|
+
|
21
|
+
def initialize(bucket:, region: ENV["AWS_REGION"] || ENV["AWS_DEFAULT_REGION"], prefix: "", **options)
|
22
|
+
super(options)
|
23
|
+
@s3_client = ::S3CacheStore::S3Client.new(bucket: bucket, region: region)
|
24
|
+
@prefix = prefix.is_a?(Pathname) ? prefix : Pathname(prefix)
|
25
|
+
end
|
26
|
+
|
27
|
+
def increment(name, amount = 1, options = nil)
|
28
|
+
current = read(name, options)
|
29
|
+
value = (current&.to_i || 0) + amount
|
30
|
+
write(name, value, options)
|
31
|
+
value
|
32
|
+
end
|
33
|
+
|
34
|
+
def decrement(name, amount = 1, options = nil)
|
35
|
+
current = read(name, options)
|
36
|
+
value = (current&.to_i || 0) - amount
|
37
|
+
write(name, value, options)
|
38
|
+
value
|
39
|
+
end
|
40
|
+
|
41
|
+
def clear(options = nil)
|
42
|
+
s3_client.clear(@prefix.to_s) if @prefix
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete_matched(matcher, options = nil)
|
46
|
+
options = merged_options(options)
|
47
|
+
matcher = key_matcher(matcher, options)
|
48
|
+
|
49
|
+
instrument(:delete_matched, matcher.inspect) do
|
50
|
+
s3_client.list_objects(@prefix.to_s).each do |object|
|
51
|
+
s3_client.delete_object(object.key) if object.key.match(matcher)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def read_entry(key, **)
|
59
|
+
payload = s3_client.read_object(object_key(key))
|
60
|
+
return unless payload
|
61
|
+
|
62
|
+
entry = deserialize_entry(payload)
|
63
|
+
entry if entry.is_a?(Cache::Entry)
|
64
|
+
end
|
65
|
+
|
66
|
+
def write_entry(key, entry, **options)
|
67
|
+
return false if options[:unless_exist] && s3_client.exists_object?(object_key(key))
|
68
|
+
|
69
|
+
payload = serialize_entry(entry, **options)
|
70
|
+
s3_client.write_object(object_key(key), payload)
|
71
|
+
end
|
72
|
+
|
73
|
+
def delete_entry(key, **)
|
74
|
+
if s3_client.exists_object?(object_key(key))
|
75
|
+
s3_client.delete_object(object_key(key))
|
76
|
+
true
|
77
|
+
else
|
78
|
+
false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def object_key(key)
|
83
|
+
filename = ::URI.encode_www_form_component(key).gsub("%2F", "/")
|
84
|
+
result = @prefix.join(filename).to_s
|
85
|
+
|
86
|
+
if result.size > OBJECT_KEY_MAX_SIZE
|
87
|
+
return object_key(::Digest::SHA2.hexdigest(key))
|
88
|
+
end
|
89
|
+
|
90
|
+
result
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "s3_cache_store/version"
|
4
|
+
require "aws-sdk-s3"
|
5
|
+
|
6
|
+
module S3CacheStore
|
7
|
+
class S3Client
|
8
|
+
attr_reader :bucket
|
9
|
+
|
10
|
+
def initialize(bucket:, region: ENV["AWS_REGION"] || ENV["AWS_DEFAULT_REGION"])
|
11
|
+
@bucket = bucket
|
12
|
+
|
13
|
+
if @bucket.nil? || @bucket.empty?
|
14
|
+
raise ArgumentError, "Bucket name not specified"
|
15
|
+
end
|
16
|
+
|
17
|
+
@client = Aws::S3::Client.new(region: region)
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_object(key)
|
21
|
+
return unless exists_object?(key)
|
22
|
+
|
23
|
+
@client.get_object(bucket: bucket, key: key).body.read
|
24
|
+
end
|
25
|
+
|
26
|
+
def write_object(key, payload)
|
27
|
+
if payload.is_a?(String) || payload.is_a?(File)
|
28
|
+
@client.put_object(bucket: bucket, key: key, body: payload)
|
29
|
+
else
|
30
|
+
Tempfile.open do |f|
|
31
|
+
f.write(payload)
|
32
|
+
f.rewind
|
33
|
+
@client.put_object(bucket: bucket, key: key, body: f)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete_object(key)
|
40
|
+
@client.delete_object(bucket: bucket, key: key) if exists_object?(key)
|
41
|
+
true
|
42
|
+
rescue => e
|
43
|
+
# Just in case the error was caused by another process deleting the file first.
|
44
|
+
raise e if exists_object?(key)
|
45
|
+
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
def exists_object?(key)
|
50
|
+
@client.head_object(bucket: bucket, key: key)
|
51
|
+
true
|
52
|
+
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NotFound
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def clear(prefix = "")
|
57
|
+
@client.list_objects_v2(bucket: bucket, prefix: prefix).contents.each do |object|
|
58
|
+
@client.delete_object(bucket: bucket, key: object.key)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def list_objects(prefix)
|
63
|
+
@client.list_objects_v2(bucket: bucket, prefix: prefix).contents
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "s3_cache_store/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "s3_cache_store"
|
7
|
+
spec.version = S3CacheStore::VERSION
|
8
|
+
spec.authors = ["Sonoda Ryohei"]
|
9
|
+
spec.email = ["ryoheisonoda@outlook.com"]
|
10
|
+
|
11
|
+
spec.summary = "Use Amazon S3 to Rails cache store backend."
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/sonodar/s3_cache_store"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/sonodar/s3_cache_store"
|
23
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
24
|
+
else
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
"public gem pushes."
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_dependency "activesupport", ">= 6"
|
39
|
+
spec.add_dependency "aws-sdk-s3", "~> 1"
|
40
|
+
|
41
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3_cache_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sonoda Ryohei
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk-s3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
description: Use Amazon S3 to Rails cache store backend.
|
56
|
+
email:
|
57
|
+
- ryoheisonoda@outlook.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".github/workflows/lint.yml"
|
63
|
+
- ".github/workflows/publish.yml"
|
64
|
+
- ".github/workflows/test.yml"
|
65
|
+
- ".gitignore"
|
66
|
+
- ".standard.yml"
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- benchmark.rb
|
73
|
+
- bin/console
|
74
|
+
- bin/setup
|
75
|
+
- lib/active_support/cache/s3_cache_store.rb
|
76
|
+
- lib/s3_cache_store/s3_client.rb
|
77
|
+
- lib/s3_cache_store/version.rb
|
78
|
+
- s3_cache_store.gemspec
|
79
|
+
homepage: https://github.com/sonodar/s3_cache_store
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata:
|
83
|
+
homepage_uri: https://github.com/sonodar/s3_cache_store
|
84
|
+
source_code_uri: https://github.com/sonodar/s3_cache_store
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubygems_version: 3.3.26
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Use Amazon S3 to Rails cache store backend.
|
104
|
+
test_files: []
|