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

Commit

Permalink
Added library to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben authored and Ben committed Mar 12, 2008
0 parents commit d70e010
Show file tree
Hide file tree
Showing 45 changed files with 4,107 additions and 0 deletions.
12 changes: 12 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
== 0.0.3 2008-01-18
* Caches are now stored by the uri as the key like they originally were

== 0.0.2 2008-01-15
* Some moron forgot to write the dependency list correctly for 0.0.1


== 0.0.1 2008-01-14

* Inital release:
* Proof of concept come used library for a personal project

20 changes: 20 additions & 0 deletions License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2008 Ben Schwarz

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
History.txt
License.txt
Manifest.txt
README.txt
Rakefile
config/hoe.rb
config/requirements.rb
lib/openuri_memcached.rb
lib/openuri_memcached/._openuri_memcached.rb
lib/openuri_memcached/._version.rb
lib/openuri_memcached/openuri_memcached.rb
lib/openuri_memcached/version.rb
script/destroy
script/generate
setup.rb
spec/openuri_cache_spec.rb
tasks/deployment.rake
tasks/environment.rake
tasks/website.rake
24 changes: 24 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
OpenURI and MemCached sitting in a tree
Require the library using
require 'openuri_memcached'

To get started run your memcached server
$ memcached -d
The default address that this gem will terminate against is localhost:11211, you can change this using
OpenURI::Cache.host = ['serverone.com:11211', 'servertwo:11211']

The cache defaults to 15 minutes, however this can be changed using:
OpenURI::Cache.expiry = 60 * 10 # Ten long minutes

Execution
Use exactly the same as you would openuri, only.. enable it.

require 'openuri_memcached'
OpenURI::Cache.enable!
open("http://germanforblack.com").read # Slow as a wet week

Quit your app (leave memcached running) and run the same example, it
should now happen in less then ... some time that is really fast.

Questions and comments can be directed to ben at germanforblack.com
All code is provided with no warranty and should be considered experimental until otherwise stated
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'config/requirements'
require 'config/hoe' # setup Hoe + all gem configuration

Dir['tasks/**/*.rake'].each { |rake| load rake }
Binary file added config/._hoe.rb
Binary file not shown.
Binary file added config/._requirements.rb
Binary file not shown.
71 changes: 71 additions & 0 deletions config/hoe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'openuri_memcached/version'

AUTHOR = 'Ben Schwarz' # can also be an array of Authors
EMAIL = "ben.schwarz@gmail.com"
DESCRIPTION = "The same OpenURI that you know and love with the power of Memcached"
GEM_NAME = 'openuri_memcached' # what ppl will type to install your gem
RUBYFORGE_PROJECT = 'schwarz' # The unix name for your project
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"

@config_file = "~/.rubyforge/user-config.yml"
@config = nil
RUBYFORGE_USERNAME = "unknown"
def rubyforge_username
unless @config
begin
@config = YAML.load(File.read(File.expand_path(@config_file)))
rescue
puts <<-EOS
ERROR: No rubyforge config file found: #{@config_file}
Run 'rubyforge setup' to prepare your env for access to Rubyforge
- See http://newgem.rubyforge.org/rubyforge.html for more details
EOS
exit
end
end
RUBYFORGE_USERNAME.replace @config["username"]
end


REV = nil
# UNCOMMENT IF REQUIRED:
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
VERS = OpenURI::Cache::VERSION::STRING + (REV ? ".#{REV}" : "")
RDOC_OPTS = ['--quiet', '--title', 'openuri_memcached documentation',
"--opname", "index.html",
"--line-numbers",
"--main", "README",
"--inline-source"]

class Hoe
def extra_deps
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
@extra_deps
end
end

# Generate all the Rake tasks
# Run 'rake -T' to see list of generated tasks (from gem root directory)
hoe = Hoe.new(GEM_NAME, VERS) do |p|
p.author = AUTHOR
p.description = DESCRIPTION
p.email = EMAIL
p.summary = DESCRIPTION
p.url = HOMEPATH
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
p.test_globs = ["test/**/test_*.rb"]
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.

# == Optional
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
p.extra_deps = [["memcache-client", ">= 1.2.1"]] # An array of rubygem dependencies [name, ], e.g. [ ['active_support', '>= 1.3.1'] ]

#p.spec_extras = {} # A hash of extra values to set in the gemspec.

end

CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
hoe.remote_rdoc_dir = ''
hoe.rsync_args = '-av --delete --ignore-errors'
17 changes: 17 additions & 0 deletions config/requirements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'fileutils'
include FileUtils

require 'rubygems'
%w[rake hoe newgem rubigen].each do |req_gem|
begin
require req_gem
rescue LoadError
puts "This Rakefile requires the '#{req_gem}' RubyGem."
puts "Installation: gem install #{req_gem} -y"
exit
end
end

$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))

require 'openuri_memcached'
Binary file added lib/._openuri_memcached.rb
Binary file not shown.
3 changes: 3 additions & 0 deletions lib/openuri_memcached.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$:.unshift File.dirname(__FILE__)

%w(openuri_memcached version).each{|r| require File.join(File.dirname(__FILE__), 'openuri_memcached', r)}
Binary file added lib/openuri_memcached/._openuri_memcached.rb
Binary file not shown.
Binary file added lib/openuri_memcached/._version.rb
Binary file not shown.
82 changes: 82 additions & 0 deletions lib/openuri_memcached/openuri_memcached.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'open-uri'
require 'rubygems'
require 'memcache'

module Kernel
private
alias openuri_original_open open
def open(uri, *rest, &block)
OpenURI::open(uri, *rest, &block)
end
module_function :open, :openuri_original_open
end

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

unless response
response = openuri_original_open(uri, *rest, &block).read
Cache::set(uri.to_s, response) if Cache.alive?
end
StringIO.new(response)
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 ||= MemCache.new(host, :namespace => "openuri")
@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 alive?
servers = @cache.instance_variable_get(:@servers) and servers.collect{|s| s.alive?}.include?(true)
end

def host
@host ||= "localhost:11211"
end
end
end
end
11 changes: 11 additions & 0 deletions lib/openuri_memcached/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module OpenURI #:nodoc:
class Cache
module VERSION #:nodoc:
MAJOR = 0
MINOR = 0
TINY = 3

STRING = [MAJOR, MINOR, TINY].join('.')
end
end
end
Empty file added log/debug.log
Empty file.
Loading

0 comments on commit d70e010

Please sign in to comment.