Skip to content

Commit

Permalink
INTERFACE CHANGE: Added sane logging
Browse files Browse the repository at this point in the history
* replace the crazy custom logger with standard logger / logger of your choice
  • Loading branch information
mloughran committed May 4, 2009
1 parent bdb80b3 commit 36d5d1a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
18 changes: 13 additions & 5 deletions lib/api_cache.rb
@@ -1,3 +1,5 @@
require 'logger'

# Contains the complete public API for APICache.
#
# Uses Cache and API classes to determine the correct behaviour.
Expand All @@ -11,12 +13,19 @@ class << self
attr_accessor :store
attr_accessor :api
attr_accessor :logger

def logger
@logger ||= begin
log = Logger.new(STDOUT)
log.level = Logger::INFO
log
end
end
end

# Initializes the cache
#
def self.start(store = nil, logger = nil)
APICache.logger = logger || APICache::Logger.new
def self.start(store = nil)
APICache::Cache.store = (store || APICache::MemcacheStore).new
end

Expand Down Expand Up @@ -67,12 +76,12 @@ def self.get(key, options = {}, &block)
cache.set(value)
value
rescue APICache::CannotFetch => e
APICache.logger.log "Failed to fetch new data from API because " \
APICache.logger.info "Failed to fetch new data from API because " \
"#{e.class}: #{e.message}"
if cache_state == :refetch
cache.get
else
APICache.logger.log "Data not available in the cache or from API"
APICache.logger.warn "Data not available in the cache or from API"
raise APICache::NotAvailableError, e.message
end
end
Expand All @@ -82,7 +91,6 @@ def self.get(key, options = {}, &block)

require 'api_cache/cache'
require 'api_cache/api'
require 'api_cache/logger'

APICache.autoload 'AbstractStore', 'api_cache/abstract_store'
APICache.autoload 'MemoryStore', 'api_cache/memory_store'
Expand Down
8 changes: 4 additions & 4 deletions lib/api_cache/api.rb
Expand Up @@ -34,14 +34,14 @@ def initialize(key, options, &block)
def queryable?
if query_times[@key]
if Time.now - query_times[@key] > @period
APICache.logger.log "Queryable: true - retry_time has passed"
APICache.logger.debug "Queryable: true - retry_time has passed"
true
else
APICache.logger.log "Queryable: false - queried too recently"
APICache.logger.debug "Queryable: false - queried too recently"
false
end
else
APICache.logger.log "Queryable: true - never used API before"
APICache.logger.debug "Queryable: true - never used API before"
true
end
end
Expand All @@ -56,7 +56,7 @@ def queryable?
# APICache::Invalid.
#
def get
APICache.logger.log "Fetching data from the API"
APICache.logger.debug "Fetching data from the API"
query_times[@key] = Time.now
Timeout::timeout(@timeout) do
if @block
Expand Down
11 changes: 0 additions & 11 deletions lib/api_cache/logger.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/api_cache/memcache_store.rb
Expand Up @@ -15,7 +15,7 @@ def initialize
end

def initialize
APICache.logger.log "Using memcached store"
APICache.logger.debug "Using memcached store"
namespace = 'api_cache'
host = '127.0.0.1:11211'
@memcache = MemCache.new(host, {:namespace => namespace})
Expand All @@ -28,13 +28,13 @@ def initialize
def set(key, data)
@memcache.set(key, data)
@memcache.set("#{key}_created_at", Time.now)
APICache.logger.log("cache: set (#{key})")
APICache.logger.debug("cache: set (#{key})")
true
end

def get(key)
data = @memcache.get(key)
APICache.logger.log("cache: #{data.nil? ? "miss" : "hit"} (#{key})")
APICache.logger.debug("cache: #{data.nil? ? "miss" : "hit"} (#{key})")
data
end

Expand Down
6 changes: 3 additions & 3 deletions lib/api_cache/memory_store.rb
@@ -1,20 +1,20 @@
class APICache
class MemoryStore < APICache::AbstractStore
def initialize
APICache.logger.log "Using memory store"
APICache.logger.debug "Using memory store"
@cache = {}
true
end

def set(key, value)
APICache.logger.log("cache: set (#{key})")
APICache.logger.debug("cache: set (#{key})")
@cache[key] = [Time.now, value]
true
end

def get(key)
data = @cache[key][1]
APICache.logger.log("cache: #{data.nil? ? "miss" : "hit"} (#{key})")
APICache.logger.debug("cache: #{data.nil? ? "miss" : "hit"} (#{key})")
data
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -5,3 +5,4 @@
require "spec"

APICache.start(APICache::MemoryStore)
APICache.logger.level = Logger::FATAL

0 comments on commit 36d5d1a

Please sign in to comment.