Class: Aws::S3::BucketRegionCache
- Inherits:
-
Object
- Object
- Aws::S3::BucketRegionCache
- Defined in:
- lib/aws-sdk-s3/bucket_region_cache.rb
Instance Method Summary collapse
-
#[](bucket_name) ⇒ String?
private
Returns the cached region for the named bucket.
-
#[]=(bucket_name, region_name) ⇒ void
private
Caches a bucket’s region.
-
#bucket_added(&block) ⇒ void
Registers a block as a callback.
- #clear ⇒ Object private
-
#initialize ⇒ BucketRegionCache
constructor
A new instance of BucketRegionCache.
-
#to_hash ⇒ Hash
(also: #to_h)
Returns a hash of cached bucket names and region names.
Constructor Details
#initialize ⇒ BucketRegionCache
Returns a new instance of BucketRegionCache.
9 10 11 12 13 |
# File 'lib/aws-sdk-s3/bucket_region_cache.rb', line 9 def initialize @regions = {} @listeners = [] @mutex = Mutex.new end |
Instance Method Details
#[](bucket_name) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the cached region for the named bucket. Returns ‘nil` if the bucket is not in the cache.
45 46 47 |
# File 'lib/aws-sdk-s3/bucket_region_cache.rb', line 45 def [](bucket_name) @mutex.synchronize { @regions[bucket_name] } end |
#[]=(bucket_name, region_name) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Caches a bucket’s region. Calling this method will trigger each of the #bucket_added listener callbacks.
55 56 57 58 59 60 |
# File 'lib/aws-sdk-s3/bucket_region_cache.rb', line 55 def []=(bucket_name, region_name) @mutex.synchronize do @regions[bucket_name] = region_name @listeners.each { |block| block.call(bucket_name, region_name) } end end |
#bucket_added(&block) ⇒ void
This method returns an undefined value.
Registers a block as a callback. This listener is called when a new bucket/region pair is added to the cache.
S3::BUCKET_REGIONS.bucket_added do |bucket_name, region_name|
# ...
end
This happens when a request is made against the classic endpoint, “s3.amazonaws.com” and an error is returned requiring the request to be resent with Signature Version 4. At this point, multiple requests are made to discover the bucket region so that a v4 signature can be generated.
An application can register listeners here to avoid these extra requests in the future. By constructing an Client with the proper region, a proper signature can be generated and redirects avoided.
33 34 35 36 37 38 39 |
# File 'lib/aws-sdk-s3/bucket_region_cache.rb', line 33 def bucket_added(&block) if block @mutex.synchronize { @listeners << block } else raise ArgumentError, 'missing required block' end end |
#clear ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
63 64 65 |
# File 'lib/aws-sdk-s3/bucket_region_cache.rb', line 63 def clear @mutex.synchronize { @regions = {} } end |
#to_hash ⇒ Hash Also known as: to_h
Returns a hash of cached bucket names and region names.
68 69 70 71 72 |
# File 'lib/aws-sdk-s3/bucket_region_cache.rb', line 68 def to_hash @mutex.synchronize do @regions.dup end end |