Commit f919a78e authored by sergio1990's avatar sergio1990

Make caching feature alive!

parent 5a54820b
......@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]
spec.add_dependency "colorize", ">= 0.7.2"
spec.add_dependency "active_redis", "0.0.9"
spec.add_dependency "active_redis", "0.0.9.1"
spec.add_dependency "activesupport", '>= 3.0.0'
spec.add_development_dependency "bundler", "~> 1.3"
......
......@@ -16,7 +16,7 @@ module BetfairApiNgRails
end
def is_cached?
!cache_result.nil?
cache_result.count > 0
end
def responser
......@@ -30,11 +30,11 @@ module BetfairApiNgRails
private
def cache_result
@_cache_result ||= BetfairCache.where(method: method, params: sig_params).first
BetfairCache.where(method: method, params: sig_params)
end
def prepare_response
OpenStruct.new body: cache_result.response, code: '200'
OpenStruct.new body: cache_result.first.response, code: '200'
end
def expire_time
......
......@@ -10,8 +10,10 @@ module BetfairApiNgRails
base.send :class_eval, <<-CODE
def request_with_caching(method, params = {})
if is_caching_on?
BetfairApiNgRails.log.write("Cache is on! Trying to process with cache...")
process_with_caching method, params
else
BetfairApiNgRails.log.write("Cache is off!")
return request_without_caching(method, params)
end
end
......@@ -27,17 +29,24 @@ module BetfairApiNgRails
end
def process_with_caching(method, params)
cache_service = Api::Caching::ResponseCache.new(method, params)
cache_service = Api::Caching::ResponseCache.new(method, hashing(params))
if cache_service.is_cached?
BetfairApiNgRails.log.write(" Finded cached record!")
return cache_service.responser
else
BetfairApiNgRails.log.write(" Cached record not found!")
return make_real_request(method, params, cache_service)
end
end
def make_real_request(method, params, cache_service)
res = request_without_caching(method, params)
cache_service.cache!(res.result) if need_caching?(res)
if need_caching?(res)
cache_service.cache!(res.result)
BetfairApiNgRails.log.write(" Result was cached!")
else
BetfairApiNgRails.log.write(" There are some problems 0_o... No any response cached...")
end
res
end
......
......@@ -15,7 +15,7 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
context 'when cache result returns item' do
let(:cache_result) { double }
let(:cache_result) { [double] }
its(:is_cached?) { is_expected.to be_truthy }
......@@ -23,7 +23,7 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
context 'when cache result returns no item' do
let(:cache_result) { nil }
let(:cache_result) { [] }
its(:is_cached?) { is_expected.to be_falsey }
......@@ -65,7 +65,7 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
describe "#cache_result" do
it "returns result from cache" do
expect(BetfairApiNgRails::Api::Caching::Models::BetfairCache).to receive(:where).with(method: :method, params: :sig_params).and_return []
expect(BetfairApiNgRails::Api::Caching::Models::BetfairCache).to receive(:where).with(method: :method, params: :sig_params)
cache.send :cache_result
end
......@@ -75,7 +75,7 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
let(:cache_result) { double(:cache_result, response: 'some response text') }
before { allow(cache).to receive(:cache_result).and_return cache_result }
before { allow(cache).to receive(:cache_result).and_return [cache_result] }
subject(:response) { cache.send(:prepare_response) }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment