Skip to content

Commit

Permalink
release v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-zakharov committed Apr 24, 2018
1 parent bf8c709 commit dffd33a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Idempotent Request Changelog #

## v0.1.2 ##

* Fix rack response
* Disuse SETNX (@espadrine)

## v0.1.1 ##

* Add caching for successful responses (@gomayonqui)
* Add caching for successful responses (@gomayonqui)
10 changes: 8 additions & 2 deletions lib/idempotent-request/redis_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ def read(key)
end

def write(key, payload)
redis.setnx(namespaced_key(key), payload)
redis.expire(namespaced_key(key), expire_time.to_i) if expire_time.to_i > 0
redis.set(
namespaced_key(key),
payload,
{}.tap do |options|
options[:nx] = true
options[:ex] = expire_time.to_i if expire_time.to_i > 0
end
)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/idempotent-request/request_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def payload(status, headers, response)
Oj.dump({
status: status,
headers: headers.to_h,
response: response
response: Array(response)
})
end

Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module IdempotentRequest
VERSION = "0.1.1"
VERSION = "0.1.2"
end
15 changes: 9 additions & 6 deletions spec/idempotent-request/redis_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
RSpec.describe IdempotentRequest::RedisStorage do
let(:redis) { FakeRedis::Redis.new }
let(:expire_time) { 3600 }
let(:redis_storage) { described_class.new(redis, expire_time: expire_time) }
let(:namespace) { 'idempotency_keys' }
let(:redis_storage) { described_class.new(redis, expire_time: expire_time, namespace: namespace) }

describe '#read' do
it 'should be called' do
Expand All @@ -15,27 +16,29 @@
describe '#write' do
let(:key) { 'key' }
let(:payload) { {} }
let(:namespace) { nil }

context 'when expire time is not set' do
let(:redis_storage) { described_class.new(redis) }
let(:expire_time) { nil }

it 'should not set expiration' do
expect(redis).to receive(:setnx)
expect(redis).to receive(:set).with(key, payload, nx: true)
expect(redis).not_to receive(:expire)
redis_storage.write(key, payload)
end
end

context 'when expire time is set' do
it 'should set expiration' do
expect(redis).to receive(:setnx)
expect(redis).to receive(:expire).with(String, expire_time)
expect(redis).to receive(:set).with(key, payload, nx: true, ex: expire_time)
redis_storage.write(key, payload)
end
end
end

describe '#namespaced_key' do
let(:namespace) { 'idempotency_keys' }

subject { redis_storage.send(:namespaced_key, key) }

context 'when key contains a space' do
Expand All @@ -55,7 +58,7 @@
end

context 'when namespace is set to nil' do
let(:redis_storage) { described_class.new(redis, namespace: nil) }
let(:namespace) { nil }
let(:key) { 'REQUEST-1' }

it 'should return with default' do
Expand Down
2 changes: 1 addition & 1 deletion spec/idempotent-request/request_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
Oj.dump({
status: data[0],
headers: data[1],
response: data[2]
response: [data[2]]
})
end

Expand Down

0 comments on commit dffd33a

Please sign in to comment.