Permalink
Browse files

Update README

  • Loading branch information...
1 parent 03c6283 commit 7a11a0e8e681b3763fe0c04eebcbd7e8ce3d83cb Kristoffer committed Mar 12, 2011
View
@@ -0,0 +1,4 @@
+== 0.0.1
+
+* major enhancements
+ * Initial release.
View
@@ -1,4 +1,4 @@
-Copyright (c) 2011 Karel Minarik and Kristoffer Sachse
+Copyright (c) 2011 Kristoffer Sachse
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -46,9 +46,11 @@ Cache responses for 5 minutes on the system in the directory "/tmp":
## Authors
-- [Karel Minarik](http://karmi.cz)
- [Kristoffer Sachse](https://github.com/sachse)
+Based on code by
+- [Karel Minarik](http://karmi.cz) (Original gist at https://gist.github.com/209521/)
+
## Contribute
Fork the project, implement your changes in it's own branch, and send
@@ -2,3 +2,7 @@
dir = File.expand_path(File.dirname(__FILE__))
require File.join(dir, 'httparty-icebox', 'icebox')
+require File.join(dir, 'httparty-icebox', 'cache')
+require File.join(dir, 'httparty-icebox', 'filestore')
+require File.join(dir, 'httparty-icebox', 'memorystore')
+require File.join(dir, 'httparty-icebox', 'memcachedstore')
@@ -0,0 +1,58 @@
+require 'logger'
+
+module HTTParty
+ module Icebox
+ class Cache
+ attr_accessor :store
+
+ def initialize(store, options = {})
+ self.class.logger = options[:logger]
+ @store = self.class.lookup_store(store).new(options)
+ end
+
+ def get(key)
+ @store.get(encode(key)) unless stale?(key)
+ end
+
+ def set(key, value)
+ self.class.logger.info("Cache: set (#{key})")
+ @store.set(encode(key), value)
+ end
+
+ def exists?(key)
+ @store.exists?(encode(key))
+ end
+
+ def stale?(key)
+ @store.stale?(encode(key))
+ end
+
+ def self.logger
+ @logger || default_logger
+ end
+
+ def self.default_logger
+ logger = ::Logger.new(STDERR)
+ end
+
+ def self.logger=(device)
+ @logger = device.kind_of?(::Logger) ? device : ::Logger.new(device)
+ end
+
+ private
+ def self.lookup_store(name)
+ store_name = "#{name.capitalize}Store"
+
+ return Store::const_get(store_name)
+
+ rescue NameError => e
+ raise NameError, "The cache store '#{store_name}' was " <<
+ "not found. Did you load any such class?"
+ end
+
+ def encode(key)
+ Digest::MD5.hexdigest(key)
+ end
+ end
+ end
+end
@@ -0,0 +1,48 @@
+require 'pathname'
+require 'tmpdir'
+
+module HTTParty
+ module Icebox
+ module Store
+ class FileStore < AbstractStore
+ def initialize(options = {})
+ super
+
+ options[:location] ||= Dir::tmpdir
+ @path = Pathname.new(options[:location])
+
+ self
+ end
+
+ def set(key, value)
+ Cache.logger.info("Cache: set (#{key})")
+ File.open(@path.join(key), 'w') { |file| file << Marshal.dump(value) }
+
+ true
+ end
+
+ def get(key)
+ data = Marshal.load(File.read(@path.join(key)))
+ Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
+
+ data
+ end
+
+ def exists?(key)
+ File.exists?(@path.join(key))
+ end
+
+ def stale?(key)
+ return true unless exists?(key)
+
+ Time.now - created(key) > @timeout
+ end
+
+ private
+ def created(key)
+ File.mtime(@path.join(key))
+ end
+ end
+ end
+ end
+end
@@ -1,7 +1,3 @@
-require 'logger'
-require 'fileutils'
-require 'tmpdir'
-require 'pathname'
require 'digest/md5'
module HTTParty
@@ -17,11 +13,11 @@ def cache(options = {})
end
def self.included(receiver)
- receiver.extend ClassMethods
+ receiver.extend(ClassMethods)
receiver.class_eval do
def self.get_without_caching(path, options = {})
- perform_request Net::HTTP::Get, path, options
+ perform_request(Net::HTTP::Get, path, options)
end
def self.get_with_caching(path, options = {})
@@ -47,67 +43,12 @@ def self.get(path, options = {})
end
end
- class Cache
- attr_accessor :store
-
- def initialize(store, options = {})
- self.class.logger = options[:logger]
- @store = self.class.lookup_store(store).new(options)
- end
-
- def get(key)
- @store.get(encode(key)) unless stale?(key)
- end
-
- def set(key, value)
- self.class.logger.info("Cache: set (#{key})")
- @store.set(encode(key), value)
- end
-
- def exists?(key)
- @store.exists?(encode(key))
- end
-
- def stale?(key)
- @store.stale?(encode(key))
- end
-
- def self.logger
- @logger || default_logger
- end
-
- def self.default_logger
- logger = ::Logger.new(STDERR)
- end
-
- def self.logger=(device)
- @logger = device.kind_of?(::Logger) ? device : ::Logger.new(device)
- end
-
- private
- def self.lookup_store(name)
- store_name = "#{name.capitalize}Store"
-
- return Store::const_get(store_name)
-
- rescue NameError => e
- raise Store::StoreNotFound, "The cache store '#{store_name}' was " <<
- "not found. Did you load any such class?"
- end
-
- def encode(key)
- Digest::MD5.hexdigest(key)
- end
- end
-
module Store
- class StoreNotFound < StandardError; end
-
class AbstractStore
def initialize(options = {})
@timeout = options[:timeout]
message = "Cache: Using #{self.class.to_s.split('::').last} " <<
- " in location: #{options[:location]} " if options[:location] <<
+ "in location: #{options[:location]} " if options[:location] <<
"with timeout #{options[:timeout]} sec"
Cache.logger.info(message) unless options[:logger].nil?
@@ -122,88 +63,6 @@ def initialize(options = {})
end
end
end
-
- class MemoryStore < AbstractStore
- def initialize(options = {})
- super
-
- @store = {}
-
- self
- end
-
- def set(key, value)
- Cache.logger.info("Cache: set (#{key})")
-
- @store[key] = [Time.now, value]
-
- true
- end
-
- def get(key)
- data = @store[key][1]
- Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
-
- data
- end
-
- def exists?(key)
- !@store[key].nil?
- end
-
- def stale?(key)
- return true unless exists?(key)
-
- Time.now - created(key) > @timeout
- end
-
- private
- def created(key)
- @store[key][0]
- end
- end
-
- class FileStore < AbstractStore
- def initialize(options = {})
- super
-
- options[:location] ||= Dir::tmpdir
- @path = Pathname.new(options[:location])
-
- FileUtils.mkdir_p(@path)
-
- self
- end
-
- def set(key, value)
- Cache.logger.info("Cache: set (#{key})")
- File.open(@path.join(key), 'w') { |file| file << Marshal.dump(value) }
-
- true
- end
-
- def get(key)
- data = Marshal.load(File.read(@path.join(key)))
- Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
-
- data
- end
-
- def exists?(key)
- File.exists?(@path.join(key))
- end
-
- def stale?(key)
- return true unless exists?(key)
-
- Time.now - created(key) > @timeout
- end
-
- private
- def created(key)
- File.mtime(@path.join(key))
- end
- end
end
end
end
@@ -0,0 +1,39 @@
+module HTTParty
+ module Icebox
+ module Store
+ class MemcachedStore < AbstractStore
+ def initialize(options = {})
+ super
+
+ options[:name] ||= MEMCACHE
+
+ self
+ end
+
+ def set(key, value)
+ Cache.logger.info("Cache: set (#{key})")
+
+ res = options[:name].set(key, value, @timeout)
+
+ true
+ end
+
+ def get(key)
+ data = options[:name].get(key) rescue nil
+ Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
+
+ data
+ end
+
+ def exists?(key)
+ data = options[:name].get(key) rescue nil
+ !data.nil?
+ end
+
+ def stale?(key)
+ return true unless exists?(key)
+ end
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 7a11a0e

Please sign in to comment.