Skip to content

Instantly share code, notes, and snippets.

@bridiver
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bridiver/2c8db1dca34e706c5291 to your computer and use it in GitHub Desktop.
Save bridiver/2c8db1dca34e706c5291 to your computer and use it in GitHub Desktop.
S3 strong consistency
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