Skip to content
This repository has been archived by the owner on Jun 24, 2023. It is now read-only.

Commit

Permalink
add support for rails-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jan 23, 2009
1 parent 43b727f commit 8773d91
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 111 deletions.
3 changes: 3 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
== 0.2.0 2009-01-23
* Include support for Rails.cache

== 0.1.0 2008-04-17
* Included Evan Weaver's Memcached C wrapper for a healthy speed improvement

Expand Down
2 changes: 1 addition & 1 deletion License.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2008 Ben Schwarz
Copyright (c) 2008-2009 Ben Schwarz

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ memcached picking up the slack and stopping people from sending you hate mail.

Require the library using

require 'openuri_memcached'
require 'openuri/memcached'

To get started run your memcached server

Expand All @@ -22,7 +22,7 @@ The cache defaults to 15 minutes, however this can be changed using:
### Execution
Use exactly the same as you would openuri, only.. enable it.

require 'openuri_memcached'
require 'openuri/memcached'
OpenURI::Cache.enable!
# Slow as a wet week
open("http://germanforblack.com").read
Expand Down
90 changes: 90 additions & 0 deletions lib/openuri/common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'open-uri'

module Kernel
private
alias openuri_original_open open
def open(name, *rest, &block)
if name.respond_to?(:open)
name.open(*rest, &block)
elsif name.respond_to?(:to_str) &&
%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
(uri = URI.parse(name)).respond_to?(:open)
OpenURI::open(name, *rest, &block)
else
open_uri_original_open(name, *rest, &block)
end
end
module_function :open, :openuri_original_open
end

module OpenURI
alias original_open open #:nodoc:
def self.open(uri, *rest, &block)
if Cache.enabled?
response = Cache::get(uri.to_s)
end

unless response
response = openuri_original_open(uri, *rest).read
Cache::set(uri.to_s, response) if Cache.enabled?
end

response = StringIO.new(response)

if block_given?
begin
yield response
ensure
response.close
end
else
response
end
end

class Cache
# Cache is not enabled by default
@cache_enabled = false

class << self
attr_writer :expiry, :host

# Is the cache enabled?
def enabled?
@cache_enabled
end

# Enable caching
def enable!
raise NotImplementedError
end

# Disable caching - all queries will be run directly
# using the standard OpenURI `open` method.
def disable!
@cache_enabled = false
end

def disabled?
!@cache_enabled
end

def get(key)
raise NotImplementedError
end

def set(key, value)
raise NotImplementedError
end

# How long your caches will be kept for (in seconds)
def expiry
@expiry ||= 60 * 10
end

def host
@host ||= "127.0.0.1:11211"
end
end
end
end
35 changes: 35 additions & 0 deletions lib/openuri/memcached.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'openuri/common'

begin
require 'minigems'
rescue LoadError
require 'rubygems'
end

require 'memcached'

module OpenURI
class Cache
class << self
# Enable caching
def enable!
@cache ||= Memcached.new(host, {
:namespace => 'openuri',
:no_block => true,
:buffer_requests => true
})
@cache_enabled = true
end

def get(key)
@cache.get(key)
rescue Memcached::NotFound
false
end

def set(key, value)
@cache.set(key, value, expiry)
end
end
end
end
21 changes: 21 additions & 0 deletions lib/openuri/rails-cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'openuri/common'

module OpenURI
class Cache
class << self
# Enable caching
def enable!
@cache ||= Rails.cache
@cache_enabled = true
end

def get(key)
@cache.fetch(key) { false }
end

def set(key, value)
@cache.write(key, value, expiry)
end
end
end
end
108 changes: 1 addition & 107 deletions lib/openuri_memcached.rb
Original file line number Diff line number Diff line change
@@ -1,107 +1 @@
require 'open-uri'

begin
require 'minigems'
rescue LoadError
require 'rubygems'
end

require 'memcached'

module Kernel
private
alias openuri_original_open open
def open(name, *rest, &block)
if name.respond_to?(:open)
name.open(*rest, &block)
elsif name.respond_to?(:to_str) &&
%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
(uri = URI.parse(name)).respond_to?(:open)
OpenURI::open(name, *rest, &block)
else
open_uri_original_open(name, *rest, &block)
end
end
module_function :open, :openuri_original_open
end

module OpenURI
alias original_open open #:nodoc:
def self.open(uri, *rest, &block)
if Cache.enabled?
begin
response = Cache::get(uri.to_s)
rescue Memcached::NotFound
response = false
end
end

unless response
response = openuri_original_open(uri, *rest).read
Cache::set(uri.to_s, response) if Cache.enabled?
end

response = StringIO.new(response)

if block_given?
begin
yield response
ensure
response.close
end
else
response
end
end

class Cache
# Cache is not enabled by default
@cache_enabled = false

class << self
attr_writer :expiry, :host

# Is the cache enabled?
def enabled?
@cache_enabled
end

# Enable caching
def enable!
@cache ||= Memcached.new(host, {
:namespace => 'openuri',
:no_block => true,
:buffer_requests => true
})
@cache_enabled = true
end

# Disable caching - all queries will be run directly
# using the standard OpenURI `open` method.
def disable!
@cache_enabled = false
end

def disabled?
!@cache_enabled
end

def get(key)
@cache.get(key)
end

def set(key, value)
@cache.set(key, value, expiry)
end

# How long your caches will be kept for (in seconds)
def expiry
@expiry ||= 60 * 10
end

def host
@host ||= "127.0.0.1:11211"
end
end
end
end
require 'openuri/memcached'
2 changes: 1 addition & 1 deletion open-uri-memcached.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "openuri_memcached"
s.version = '0.1.3'
s.version = '0.2'
s.email = "ben@germanforblack.com"
s.homepage = "http://github.com/benschwarz/open-uri-memcached"
s.description = "OpenURI with transparent caching"
Expand Down

0 comments on commit 8773d91

Please sign in to comment.