This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

More cache options (#17)

  • Loading branch information...
aaronsamuel137 committed Dec 8, 2016
1 parent 45d2b6e commit 566026adffb2bc3c1183ca765f47f96bda00a810
View
@@ -1,22 +1,31 @@
PATH
remote: .
specs:
tangocard (6.1.0)
tangocard (7.0.0)
activesupport
httparty (~> 0.11)
i18n (~> 0.7)
money (~> 6.1)
GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.6)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
diff-lcs (1.2.5)
httparty (0.14.0)
multi_xml (>= 0.5.2)
i18n (0.7.0)
json (1.8.3)
minitest (5.8.4)
money (6.7.1)
i18n (>= 0.6.4, <= 0.7.0)
sixarm_ruby_unaccent (>= 1.1.1, < 2)
multi_xml (0.5.5)
multi_xml (0.6.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
@@ -31,6 +40,9 @@ GEM
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sixarm_ruby_unaccent (1.1.1)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
ruby
View
@@ -44,8 +44,9 @@ There are also optional configuration parameters:
* `default_brands` - An array of strings for the brands you want to retrieve with `Tangocard::Brand.default`. The strings should match the unique brand `description` fields exactly.
* `local_images` - An array of local image names/URIs that you want to display instead of the default Tango Card-provided `image_url`. `image_url` is sometimes blank, so this can be handy in those cases.
* `sku_blacklist` - Reward SKUs that are blacklisted, ie. should never be returned as a purchasable reward.
* `use_cache` - Use cache for Tangocard::Brand queries, defaults to `true`.
* `cache_ttl` - Cache time-to-Live in seconds, only effective if `use_cache` is enabled. Default is `0` (cache never expires).
* `use_cache` - Use cache for Tangocard::Brand queries, defaults to `true`. The cache can be refreshed by calling `Tangocard.warm_cache`
* `cache` - Which cache to use, defaults to `ActiveSupport::Cache::MemoryStore`. Using an out-of-process cache e.g. hosted memcache will improve performance and stability.
* `logger` - i.e. `Rails.logger`
## Getting Started
View
@@ -1,11 +1,16 @@
require 'httparty'
require 'money'
require 'ostruct'
require 'active_support'
require 'active_support/cache/memory_store'
module Tangocard
CACHE_PREFIX = "tangocard:#{VERSION}:"
class Configuration
attr_accessor :name, :key, :base_uri, :default_brands, :local_images, :sku_blacklist,
:use_cache, :cache_ttl
:use_cache, :cache, :logger
def initialize
self.name = nil
@@ -15,7 +20,8 @@ def initialize
self.local_images = {}
self.sku_blacklist = []
self.use_cache = true
self.cache_ttl = 0
self.cache = ActiveSupport::Cache::MemoryStore.new
self.logger = Logger.new(STDOUT)
end
end
@@ -25,6 +31,12 @@ def self.configuration
def self.configure
yield(configuration) if block_given?
warm_cache if configuration.use_cache
end
def self.warm_cache
configuration.cache.write("#{Tangocard::CACHE_PREFIX}rewards_index", Tangocard::Raas.rewards_index(use_cache: false))
configuration.logger.info('Warmed Tangocard cache')
end
end
@@ -43,4 +55,4 @@ def self.configure
require 'tangocard/raas_exception'
require 'tangocard/reward'
require 'tangocard/exchange_rate'
require 'tangocard/version'
View
@@ -1,18 +1,6 @@
class Tangocard::Brand
attr_reader :description, :rewards
# Clear all cached responses. Next request for brand info will pull fresh from the Tango Card API.
#
# Example:
# >> Tangocard::Brand.clear_cache!
# => true
#
# Arguments:
# none
def self.clear_cache!
Tangocard::Raas.clear_cache!
end
# Return an array of all brands.
#
# Example:
@@ -1,18 +1,6 @@
class Tangocard::ExchangeRate
attr_reader :currency_code, :rate
# Clear all cached responses. Next request for exchange rate info will pull fresh from the Tango Card API.
#
# Example:
# >> Tangocard::ExchangeRate.clear_cache!
# => true
#
# Arguments:
# none
def self.clear_cache!
Tangocard::Raas.clear_cache!
end
# Return current currency exchange rate timestamp.
#
# Example:
View
@@ -1,8 +1,6 @@
class Tangocard::Raas
include HTTParty
@@rewards_response_expires_at = 0
# Create a new account. Returns Tangocard::Response object.
#
# Example:
@@ -71,13 +69,9 @@ def self.delete_credit_card(params)
#
# Arguments:
# none
def self.rewards_index
if Tangocard.configuration.use_cache
clear_cache! if cache_expired?
@@rewards_response ||= Tangocard::Response.new(get_request('/rewards'))
@@rewards_response_expires_at = (Time.now.to_i + Tangocard.configuration.cache_ttl) if cache_expired?
@@rewards_response
def self.rewards_index(use_cache: true)
if Tangocard.configuration.use_cache && use_cache
Tangocard.configuration.cache.read("#{Tangocard::CACHE_PREFIX}rewards_index")
else
Tangocard::Response.new(get_request('/rewards'))
end
@@ -127,25 +121,12 @@ def self.orders_index(params = {})
Tangocard::Response.new(get_request("/orders#{query_string}"))
end
def self.clear_cache!
@@rewards_response = nil
@@rewards_response_expires_at = 0
end
private
def self.basic_auth_param
{ basic_auth: { username: Tangocard.configuration.name, password: Tangocard.configuration.key } }
end
def self.use_cache_ttl?
Tangocard.configuration.use_cache && Tangocard.configuration.cache_ttl > 0
end
def self.cache_expired?
use_cache_ttl? && @@rewards_response_expires_at < Time.now.to_i
end
def self.endpoint
"#{Tangocard.configuration.base_uri}/raas/v1.1"
end
View
@@ -1,3 +1,3 @@
module Tangocard
VERSION = '6.2.0'
VERSION = '7.0.0'
end
@@ -10,14 +10,6 @@
allow(Tangocard::Raas).to receive(:rewards_index) { rewards_index }
end
describe 'self.clear_cache!' do
it 'should call Tangocard::Raas.clear_cache!' do
expect(Tangocard::Raas).to receive(:clear_cache!) { true }
Tangocard::Brand.clear_cache!
end
end
describe 'self.all' do
context 'Tangocard is behaving' do
it 'should return an array of Tangocard::Brand objects' do
@@ -4,12 +4,6 @@
include TangocardHelpers
describe 'class methods' do
describe 'self.clear_cache!' do
it 'should call Tangocard::Raas.clear_cache!' do
expect(Tangocard::Raas).to receive(:clear_cache!) { true }
Tangocard::Brand.clear_cache!
end
end
describe 'self.timestamp' do
before do
@@ -13,6 +13,7 @@
Tangocard.configure do |c|
c.name = name
c.key = key
c.use_cache = false
end
end
@@ -75,61 +76,23 @@
end
describe 'self.rewards_index' do
before do
Tangocard::Raas.class_variable_set :@@rewards_response, nil
Tangocard::Raas.class_variable_set :@@rewards_response_expires_at, 0
end
context 'use_cache == true' do
before do
expect(Tangocard::Raas).to receive(:get_request).with('/rewards') { raw_response }
Tangocard.configure do |c|
c.use_cache = true
end
end
context 'cache_ttl == 0' do
before do
Tangocard.configure do |c|
c.cache_ttl = 0
end
end
it 'should do the GET request once, then hit cache' do
expect(Tangocard::Raas).to receive(:get_request).with('/rewards').twice { raw_response }
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
Tangocard::Raas.clear_cache!
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
end
end
context 'cache_ttl > 0' do
before do
Tangocard.configure do |c|
c.cache_ttl = 3600
end
end
it 'should do GET requests only if cache expired' do
expect(Tangocard::Raas).to receive(:get_request).with('/rewards').twice { raw_response }
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
Tangocard::Raas.class_variable_set :@@rewards_response_expires_at, 0
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
end
it 'should not do any get requests if the cache is warm' do
expect(Tangocard::Raas).not_to receive(:get_request).with('/rewards')
Tangocard::Raas.rewards_index
end
end
context 'use_cache == false' do
before do
Tangocard.configure do |c|
c.use_cache = false
end
end
it 'should do the GET request once, then hit cache' do
expect(Tangocard::Raas).to receive(:get_request).with('/rewards').twice { raw_response }
expect(Tangocard::Raas.rewards_index.parsed_response).to eq response
View
@@ -19,5 +19,6 @@ Gem::Specification.new do |s|
s.add_dependency 'httparty', '~> 0.11'
s.add_dependency 'money', '~> 6.1'
s.add_dependency 'i18n', '~> 0.7'
s.add_dependency 'activesupport'
s.add_development_dependency 'rspec'
end

0 comments on commit 566026a

Please sign in to comment.