Last active
August 29, 2015 14:05
-
-
Save bridiver/2c8db1dca34e706c5291 to your computer and use it in GitHub Desktop.
S3 strong consistency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def write_with_strong_consistency(*args, &block) | |
result = write(*args, &block) | |
redis.set("s3:key:#{key}", result.etag, ex: 24*60*60) | |
result | |
end | |
def delete_with_strong_consistency(options = {}) | |
result = delete(options) | |
redis.set("s3:key:#{key}", 'deleted', ex: 24*60*60) unless options[:version_id] | |
result | |
end | |
def read_with_strong_consistency(options = {}, &read_block) | |
read(options, &read_block) if options[:version_id] # short-circuit the whole thing for versions | |
redis_etag = redis.get("s3:key:#{key}") | |
options[:if_match] ||= redis_etag | |
raise NoSuchKey, "The specified key does not exist" if redis_etag == 'deleted' | |
if redis_etag && redis_etag == options[:if_match] | |
begin | |
read(options, &read_block) | |
rescue AWS::S3::Errors::NoSuchKey => e | |
# if the key has actually been deleted, the next time you check you will get | |
# a real NoSuchKey exception | |
raise AWS::S3::Errors::PreconditionFailed, "The specified key does not exist yet" | |
end | |
else | |
read(options, &read_block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment