Commit 3e021f39 authored by sergio1990's avatar sergio1990

Moved to naked redis gem

active_redis has some side effects that makes me crazy
parent 8e8c39f2
......@@ -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.1"
spec.add_dependency "redis", '3.0.7'
spec.add_dependency "activesupport", '>= 3.0.0'
spec.add_development_dependency "bundler", "~> 1.3"
......
......@@ -2,7 +2,7 @@ require "betfair_api_ng_rails/version"
require "betfair_api_ng_rails/errors"
require "betfair_api_ng_rails/api/request_methods"
require "betfair_api_ng_rails/railtie" if defined?(Rails)
require 'active_redis'
require 'redis'
module BetfairApiNgRails
......@@ -25,12 +25,6 @@ module BetfairApiNgRails
autoload :Helper, 'betfair_api_ng_rails/api/caching/helper'
autoload :ResponseCache, 'betfair_api_ng_rails/api/caching/response_cache'
module Models
autoload :BetfairCache, 'betfair_api_ng_rails/api/caching/models/betfair_cache'
end
end
module Logs
......
module BetfairApiNgRails
module Api
module Caching
module Models
class BetfairCache < ActiveRedis::Base
attributes method: :string,
params: :string,
response: :string
end
end
end
end
end
\ No newline at end of file
......@@ -6,7 +6,6 @@ module BetfairApiNgRails
module Caching
class ResponseCache
include Api::Caching::Helper
include Api::Caching::Models
attr_reader :method, :sig_params
......@@ -15,8 +14,12 @@ module BetfairApiNgRails
@sig_params = createsig(params)
end
def self.redis_connection
@_redis_connection ||= Redis.new
end
def is_cached?
cache_result.count > 0
!cache_result.nil?
end
def responser
......@@ -24,25 +27,30 @@ module BetfairApiNgRails
end
def cache!(response)
BetfairCache.create(create_params(response)).expire expire_time
cache_adapter.set cache_key, JSON.dump(response)
cache_adapter.expire cache_key, expire_time
end
private
def cache_result
BetfairCache.where(method: method, params: sig_params)
@_cache_result ||= cache_adapter.get cache_key
end
def prepare_response
OpenStruct.new body: cache_result.first.response, code: '200'
OpenStruct.new body: cache_result, code: '200'
end
def expire_time
BetfairApiNgRails.config.cache_expire
end
def create_params(response)
{ method: method, params: sig_params, response: JSON.dump(response) }
def cache_adapter
self.class.redis_connection
end
def cache_key
"betfair_api_ng_rails:#{method}:#{sig_params}"
end
end
......
......@@ -9,13 +9,22 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
before { allow_any_instance_of(described_class).to receive(:createsig).with(:params).and_return :sig_params }
describe ".redis_connection" do
it "initializes connection to redis" do
expect(Redis).to receive :new
described_class.redis_connection
end
end
describe "#is_cached?" do
before { expect(cache).to receive(:cache_result).and_return(cache_result) }
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 +32,7 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
context 'when cache result returns no item' do
let(:cache_result) { [] }
let(:cache_result) { nil }
its(:is_cached?) { is_expected.to be_falsey }
......@@ -45,16 +54,16 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
describe "#cache!" do
let(:response) { { some: :key } }
let(:record) { double(:record) }
let(:cache_adapter) { double(:cache_adapter) }
before do
expect(cache).to receive(:create_params).with(response).and_return :create_params
expect(cache).to receive(:expire_time).and_return :expire_time
before do
allow(cache).to receive(:cache_adapter).and_return cache_adapter
expect(cache).to receive(:expire_time).and_return 5
end
it "saves response in cache" do
expect(BetfairApiNgRails::Api::Caching::Models::BetfairCache).to receive(:create).with(:create_params).and_return record
expect(record).to receive(:expire).with :expire_time
expect(cache_adapter).to receive(:set).with("betfair_api_ng_rails:method:sig_params", "{\"some\":\"key\"}")
expect(cache_adapter).to receive(:expire).with("betfair_api_ng_rails:method:sig_params", 5)
cache.cache! response
end
......@@ -63,9 +72,13 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
context 'private method' do
describe "#cache_result" do
let(:cache_adapter) { double(:cache_adapter) }
before { expect(cache).to receive(:cache_adapter).and_return cache_adapter }
it "returns result from cache" do
expect(BetfairApiNgRails::Api::Caching::Models::BetfairCache).to receive(:where).with(method: :method, params: :sig_params)
expect(cache_adapter).to receive(:get).with("betfair_api_ng_rails:method:sig_params")
cache.send :cache_result
end
......@@ -73,9 +86,7 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
describe "#prepare_response" 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 :result }
subject(:response) { cache.send(:prepare_response) }
......@@ -83,7 +94,7 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
it { is_expected.to respond_to(:code) }
its(:body) { is_expected.to eq 'some response text' }
its(:body) { is_expected.to eq :result }
its(:code) { is_expected.to eq '200' }
......@@ -98,10 +109,19 @@ describe BetfairApiNgRails::Api::Caching::ResponseCache do
end
describe "#create_params" do
describe "#cache_adapter" do
it "returns redis connection" do
expect(cache).to receive_message_chain(:class, :redis_connection)
cache.send :cache_adapter
end
end
describe "#cache_key" do
it "returns params for saving cache" do
expect(cache.send(:create_params, {some: :key})).to eq(method: :method, params: :sig_params, response: "{\"some\":\"key\"}")
it "returns proper key for cache" do
expect(cache.send(:cache_key)).to eq "betfair_api_ng_rails:method:sig_params"
end
end
......
$TESTING=true
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
module ActiveRedis
class Base
def self.attributes(*args); end
end
end
require 'rspec/its'
require 'betfair_api_ng_rails'
......
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