openuri_memcached 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-01-14
2
+
3
+ * Inital release:
4
+ * Proof of concept come used library for a personal project
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Ben Schwarz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/openuri_memcached.rb
9
+ lib/openuri_memcached/openuri_memcached.rb
10
+ lib/openuri_memcached/version.rb
11
+ spec/openuri_cache_spec.rb
data/README.txt ADDED
@@ -0,0 +1,24 @@
1
+ OpenURI and MemCached sitting in a tree
2
+ Require the library using
3
+ require 'openuri_memcached'
4
+
5
+ To get started run your memcached server
6
+ $ memcached -d
7
+ The default address that this gem will terminate against is localhost:11211, you can change this using
8
+ OpenURI::Cache.host = ['serverone.com:11211', 'servertwo:11211']
9
+
10
+ The cache defaults to 15 minutes, however this can be changed using:
11
+ OpenURI::Cache.expiry = 60 * 10 # Ten long minutes
12
+
13
+ Execution
14
+ Use exactly the same as you would openuri, only.. enable it.
15
+
16
+ require 'openuri_memcached'
17
+ OpenURI::Cache.enable!
18
+ open("http://germanforblack.com").read # Slow as a wet week
19
+
20
+ Quit your app (leave memcached running) and run the same example, it
21
+ should now happen in less then ... some time that is really fast.
22
+
23
+ Questions and comments can be directed to ben at germanforblack.com
24
+ All code is provided with no warranty and should be considered experimental until otherwise stated
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'openuri_memcached/version'
2
+
3
+ AUTHOR = 'Ben Schwarz' # can also be an array of Authors
4
+ EMAIL = "ben.schwarz@gmail.com"
5
+ DESCRIPTION = "The same OpenURI that you know and love with the power of Memcached"
6
+ GEM_NAME = 'openuri_memcached' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'schwarz' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = OpenURI::Cache::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'openuri_memcached documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
62
+ p.extra_deps = [["memcached", ">= 1.2.1"]] # An array of rubygem dependencies [name, ], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+
64
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
65
+
66
+ end
67
+
68
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
69
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
70
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
71
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'openuri_memcached'
@@ -0,0 +1,82 @@
1
+ require 'open-uri'
2
+ require 'rubygems'
3
+ require 'memcache'
4
+
5
+ module Kernel
6
+ private
7
+ alias openuri_original_open open
8
+ def open(uri, *rest, &block)
9
+ OpenURI::open(uri, *rest, &block)
10
+ end
11
+ module_function :open, :openuri_original_open
12
+ end
13
+
14
+ module OpenURI
15
+ alias original_open open #:nodoc:
16
+ def self.open(uri, *rest, &block)
17
+ if Cache.enabled? and Cache::alive?
18
+ begin
19
+ response = Cache::get(uri.hash)
20
+ rescue
21
+ response = false
22
+ end
23
+ end
24
+
25
+ unless response
26
+ response = openuri_original_open(uri, *rest, &block).read
27
+ Cache::set(uri.hash, response) if Cache.alive?
28
+ end
29
+ StringIO.new(response)
30
+ end
31
+
32
+ class Cache
33
+ # Cache is not enabled by default
34
+ @cache_enabled = false
35
+
36
+ class << self
37
+ attr_writer :expiry, :host
38
+
39
+ # Is the cache enabled?
40
+ def enabled?
41
+ @cache_enabled
42
+ end
43
+
44
+ # Enable caching
45
+ def enable!
46
+ @cache ||= MemCache.new(host, :namespace => "openuri")
47
+ @cache_enabled = true
48
+ end
49
+
50
+ # Disable caching - all queries will be run directly
51
+ # using the standard OpenURI `open` method.
52
+ def disable!
53
+ @cache_enabled = false
54
+ end
55
+
56
+ def disabled?
57
+ !@cache_enabled
58
+ end
59
+
60
+ def get(key)
61
+ @cache.get(key)
62
+ end
63
+
64
+ def set(key, value)
65
+ @cache.set(key, value, expiry)
66
+ end
67
+
68
+ # How long your caches will be kept for (in seconds)
69
+ def expiry
70
+ @expiry ||= 60 * 10
71
+ end
72
+
73
+ def alive?
74
+ servers = @cache.instance_variable_get(:@servers) and servers.collect{|s| s.alive?}.include?(true)
75
+ end
76
+
77
+ def host
78
+ @host ||= "localhost:11211"
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,11 @@
1
+ module OpenURI #:nodoc:
2
+ class Cache
3
+ module VERSION #:nodoc:
4
+ MAJOR = 0
5
+ MINOR = 0
6
+ TINY = 1
7
+
8
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ %w(openuri_memcached version).each{|r| require File.join(File.dirname(__FILE__), 'openuri_memcached', r)}
@@ -0,0 +1,51 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'openuri_memcached')
2
+
3
+ describe OpenURI::Cache, "init" do
4
+ it "should be disabled" do
5
+ OpenURI::Cache.should be_disabled
6
+ end
7
+ end
8
+
9
+ describe OpenURI, "module" do
10
+ it "should query a resource without cache" do
11
+ open('http://google.com').read.should =~ /html/
12
+ end
13
+
14
+ it "should query a resource with cache" do
15
+ OpenURI::Cache.enable!
16
+ OpenURI::Cache.should be_enabled
17
+ open('http://google.com').read.should =~ /html/
18
+ end
19
+ end
20
+
21
+ describe OpenURI::Cache, "class" do
22
+ it "should be able to be enabled" do
23
+ OpenURI::Cache.enable!
24
+ OpenURI::Cache.should be_enabled
25
+ end
26
+
27
+ it "should be able to be disabled" do
28
+ OpenURI::Cache.enable!
29
+ OpenURI::Cache.disable!
30
+ OpenURI::Cache.should be_disabled
31
+ end
32
+
33
+ it "should cache for a default of 10 minutes" do
34
+ OpenURI::Cache.expiry.should eql 60 * 10
35
+ end
36
+
37
+ it "should allow a userset cache expiry timeframe" do
38
+ OpenURI::Cache.expiry = 60 * 15
39
+ OpenURI::Cache.expiry.should eql 60 * 15
40
+ end
41
+
42
+ it "should allow a userset host" do
43
+ server = "10.1.1.1:11211"
44
+ OpenURI::Cache.host.should eql "localhost:11211"
45
+ OpenURI::Cache.host = server
46
+ OpenURI::Cache.host.should eql server
47
+ end
48
+
49
+ # Not actually sure how I could implement this spec!
50
+ it "should allow instant cache expiry"
51
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openuri_memcached
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Schwarz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-15 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: memcached
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.1
23
+ version:
24
+ description: The same OpenURI that you know and love with the power of Memcached
25
+ email: ben.schwarz@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - History.txt
32
+ - License.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ files:
36
+ - History.txt
37
+ - License.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - config/hoe.rb
42
+ - config/requirements.rb
43
+ - lib/openuri_memcached.rb
44
+ - lib/openuri_memcached/openuri_memcached.rb
45
+ - lib/openuri_memcached/version.rb
46
+ - spec/openuri_cache_spec.rb
47
+ has_rdoc: true
48
+ homepage: http://schwarz.rubyforge.org
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --main
52
+ - README.txt
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: schwarz
70
+ rubygems_version: 1.0.1
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: The same OpenURI that you know and love with the power of Memcached
74
+ test_files: []
75
+