openuri_memcached 0.0.3 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,16 @@
1
- OpenURI and MemCached sitting in a tree
1
+ OpenURI with Memcached caching
2
+ --
3
+
4
+ Carelessly make heavy OpenURI calls as many times as you like with local
5
+ memcached picking up the slack and stopping people from sending you hate mail.
6
+
2
7
  Require the library using
3
8
  require 'openuri_memcached'
4
9
 
5
10
  To get started run your memcached server
6
11
  $ memcached -d
7
12
  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']
13
+ OpenURI::Cache.host = ['10.1.1.10:11211', '10.1.1.11:11211']
9
14
 
10
15
  The cache defaults to 15 minutes, however this can be changed using:
11
16
  OpenURI::Cache.expiry = 60 * 10 # Ten long minutes
@@ -21,4 +26,10 @@ Quit your app (leave memcached running) and run the same example, it
21
26
  should now happen in less then ... some time that is really fast.
22
27
 
23
28
  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
29
+
30
+ Requirements:
31
+ » Ruby 1.8.6
32
+ » Memcached
33
+ » memcache - Refer to blog.evanweaver.com/files/doc/fauna/memcached/files/README.html for installation notes
34
+ For the gem, do this `sudo env ARCHFLAGS="-arch i386" gem install memcached --no-rdoc --no-ri -- --with-opt-dir=/opt/local`
35
+
@@ -1,3 +1,88 @@
1
- $:.unshift File.dirname(__FILE__)
1
+ require 'open-uri'
2
2
 
3
- %w(openuri_memcached version).each{|r| require File.join(File.dirname(__FILE__), 'openuri_memcached', r)}
3
+ begin
4
+ require 'minigems'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ end
8
+
9
+ require 'memcached'
10
+
11
+ module Kernel
12
+ private
13
+ alias openuri_original_open open
14
+ def open(uri, *rest, &block)
15
+ OpenURI::open(uri, *rest, &block)
16
+ end
17
+ module_function :open, :openuri_original_open
18
+ end
19
+
20
+ module OpenURI
21
+ alias original_open open #:nodoc:
22
+ def self.open(uri, *rest, &block)
23
+ if Cache.enabled?
24
+ begin
25
+ response = Cache::get(uri.to_s)
26
+ rescue Memcached::NotFound
27
+ response = false
28
+ end
29
+ end
30
+
31
+ unless response
32
+ response = openuri_original_open(uri, *rest, &block).read
33
+ Cache::set(uri.to_s, response) if Cache.enabled?
34
+ end
35
+ StringIO.new(response)
36
+ end
37
+
38
+ class Cache
39
+ # Cache is not enabled by default
40
+ @cache_enabled = false
41
+
42
+ class << self
43
+ attr_writer :expiry, :host
44
+
45
+ # Is the cache enabled?
46
+ def enabled?
47
+ @cache_enabled
48
+ end
49
+
50
+ # Enable caching
51
+ def enable!
52
+ @cache ||= Memcached.new(host, {
53
+ :namespace => 'openuri',
54
+ :no_block => true,
55
+ :buffer_requests => true
56
+ })
57
+ @cache_enabled = true
58
+ end
59
+
60
+ # Disable caching - all queries will be run directly
61
+ # using the standard OpenURI `open` method.
62
+ def disable!
63
+ @cache_enabled = false
64
+ end
65
+
66
+ def disabled?
67
+ !@cache_enabled
68
+ end
69
+
70
+ def get(key)
71
+ @cache.get(key)
72
+ end
73
+
74
+ def set(key, value)
75
+ @cache.set(key, value, expiry)
76
+ end
77
+
78
+ # How long your caches will be kept for (in seconds)
79
+ def expiry
80
+ @expiry ||= 60 * 10
81
+ end
82
+
83
+ def host
84
+ @host ||= "127.0.0.1:11211"
85
+ end
86
+ end
87
+ end
88
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openuri_memcached
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Schwarz
@@ -9,55 +9,35 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-18 00:00:00 +11:00
12
+ date: 2008-11-17 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: memcache-client
16
+ name: memcached
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
20
21
  - - ">="
21
22
  - !ruby/object:Gem::Version
22
- version: 1.2.1
23
+ version: "0.10"
23
24
  version:
24
- description: The same OpenURI that you know and love with the power of Memcached
25
- email: ben.schwarz@gmail.com
25
+ description: OpenURI with transparent caching
26
+ email: ben@germanforblack.com
26
27
  executables: []
27
28
 
28
29
  extensions: []
29
30
 
30
- extra_rdoc_files:
31
- - History.txt
32
- - License.txt
33
- - Manifest.txt
34
- - README.txt
31
+ extra_rdoc_files: []
32
+
35
33
  files:
36
- - History.txt
37
- - License.txt
38
- - Manifest.txt
39
- - README.txt
40
- - Rakefile
41
- - config/hoe.rb
42
- - config/requirements.rb
34
+ - README
43
35
  - lib/openuri_memcached.rb
44
- - lib/openuri_memcached/._openuri_memcached.rb
45
- - lib/openuri_memcached/._version.rb
46
- - lib/openuri_memcached/openuri_memcached.rb
47
- - lib/openuri_memcached/version.rb
48
- - script/destroy
49
- - script/generate
50
- - setup.rb
51
- - spec/openuri_cache_spec.rb
52
- - tasks/deployment.rake
53
- - tasks/environment.rake
54
- - tasks/website.rake
55
- has_rdoc: true
56
- homepage: http://schwarz.rubyforge.org
36
+ has_rdoc: false
37
+ homepage: http://github.com/benschwarz/open-uri-memcached
57
38
  post_install_message:
58
- rdoc_options:
59
- - --main
60
- - README.txt
39
+ rdoc_options: []
40
+
61
41
  require_paths:
62
42
  - lib
63
43
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -74,8 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
54
  version:
75
55
  requirements: []
76
56
 
77
- rubyforge_project: schwarz
78
- rubygems_version: 1.0.1
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
79
59
  signing_key:
80
60
  specification_version: 2
81
61
  summary: The same OpenURI that you know and love with the power of Memcached
@@ -1,12 +0,0 @@
1
- == 0.0.3 2008-01-18
2
- * Caches are now stored by the uri as the key like they originally were
3
-
4
- == 0.0.2 2008-01-15
5
- * Some moron forgot to write the dependency list correctly for 0.0.1
6
-
7
-
8
- == 0.0.1 2008-01-14
9
-
10
- * Inital release:
11
- * Proof of concept come used library for a personal project
12
-
@@ -1,20 +0,0 @@
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.
@@ -1,19 +0,0 @@
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
- lib/openuri_memcached/openuri_memcached.rb
12
- lib/openuri_memcached/version.rb
13
- script/destroy
14
- script/generate
15
- setup.rb
16
- spec/openuri_cache_spec.rb
17
- tasks/deployment.rake
18
- tasks/environment.rake
19
- tasks/website.rake
data/Rakefile DELETED
@@ -1,4 +0,0 @@
1
- require 'config/requirements'
2
- require 'config/hoe' # setup Hoe + all gem configuration
3
-
4
- Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -1,71 +0,0 @@
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 = [["memcache-client", ">= 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 = ''
71
- hoe.rsync_args = '-av --delete --ignore-errors'
@@ -1,17 +0,0 @@
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'
@@ -1,82 +0,0 @@
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.to_s)
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.to_s, 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