technoweenie-openuri_memcached 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +18 -0
- data/License.txt +20 -0
- data/README.markdown +40 -0
- data/lib/openuri/common.rb +90 -0
- data/lib/openuri/memcached.rb +36 -0
- data/lib/openuri/rails-cache.rb +21 -0
- data/lib/openuri_memcached.rb +1 -0
- metadata +59 -0
data/History.txt
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
== 0.2.0 2009-01-23
|
2
|
+
* Include support for Rails.cache
|
3
|
+
|
4
|
+
== 0.1.0 2008-04-17
|
5
|
+
* Included Evan Weaver's Memcached C wrapper for a healthy speed improvement
|
6
|
+
|
7
|
+
== 0.0.3 2008-01-18
|
8
|
+
* Caches are now stored by the uri as the key like they originally were
|
9
|
+
|
10
|
+
== 0.0.2 2008-01-15
|
11
|
+
* Some moron forgot to write the dependency list correctly for 0.0.1
|
12
|
+
|
13
|
+
|
14
|
+
== 0.0.1 2008-01-14
|
15
|
+
|
16
|
+
* Inital release:
|
17
|
+
* Proof of concept come used library for a personal project
|
18
|
+
|
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008-2009 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/README.markdown
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# OpenURI with Memcached caching
|
2
|
+
|
3
|
+
Carelessly make heavy OpenURI calls as many times as you like with local
|
4
|
+
memcached picking up the slack and stopping people from sending you hate mail.
|
5
|
+
|
6
|
+
Require the library using
|
7
|
+
|
8
|
+
require 'openuri/memcached'
|
9
|
+
|
10
|
+
To get started run your memcached server
|
11
|
+
|
12
|
+
$ memcached -d
|
13
|
+
|
14
|
+
The default address that this gem will terminate against is localhost:11211, you can change this using
|
15
|
+
|
16
|
+
OpenURI::Cache.host = ['10.1.1.10:11211', '10.1.1.11:11211']
|
17
|
+
|
18
|
+
The cache defaults to 15 minutes, however this can be changed using:
|
19
|
+
|
20
|
+
OpenURI::Cache.expiry = 60 * 10 # Ten long minutes
|
21
|
+
|
22
|
+
### Execution
|
23
|
+
Use exactly the same as you would openuri, only.. enable it.
|
24
|
+
|
25
|
+
require 'openuri/memcached'
|
26
|
+
OpenURI::Cache.enable!
|
27
|
+
# Slow as a wet week
|
28
|
+
open("http://germanforblack.com").read
|
29
|
+
|
30
|
+
Quit your app (leave memcached running) and run the same example, it
|
31
|
+
should now happen in less then ... some time that is really fast.
|
32
|
+
|
33
|
+
Questions and comments can be directed to ben at germanforblack.com
|
34
|
+
|
35
|
+
### Requirements
|
36
|
+
|
37
|
+
* Ruby 1.8.6
|
38
|
+
* MemCached
|
39
|
+
* memcache (gem)
|
40
|
+
* You will need to ensure that you have [corresponding version](http://blog.evanweaver.com/files/doc/fauna/memcached/files/COMPATIBILITY.html) of libmemcached to the memcached gem installed for installation to go by breezy
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
private
|
5
|
+
alias openuri_original_open open
|
6
|
+
def open(name, *rest, &block)
|
7
|
+
if name.respond_to?(:open)
|
8
|
+
name.open(*rest, &block)
|
9
|
+
elsif name.respond_to?(:to_str) &&
|
10
|
+
%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
|
11
|
+
(uri = URI.parse(name)).respond_to?(:open)
|
12
|
+
OpenURI::open(name, *rest, &block)
|
13
|
+
else
|
14
|
+
open_uri_original_open(name, *rest, &block)
|
15
|
+
end
|
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
|
+
response = Cache::get(uri.to_s)
|
25
|
+
end
|
26
|
+
|
27
|
+
unless response
|
28
|
+
response = openuri_original_open(uri, *rest).read
|
29
|
+
Cache::set(uri.to_s, response) if Cache.enabled?
|
30
|
+
end
|
31
|
+
|
32
|
+
response = StringIO.new(response)
|
33
|
+
|
34
|
+
if block_given?
|
35
|
+
begin
|
36
|
+
yield response
|
37
|
+
ensure
|
38
|
+
response.close
|
39
|
+
end
|
40
|
+
else
|
41
|
+
response
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Cache
|
46
|
+
# Cache is not enabled by default
|
47
|
+
@cache_enabled = false
|
48
|
+
|
49
|
+
class << self
|
50
|
+
attr_writer :expiry, :host
|
51
|
+
|
52
|
+
# Is the cache enabled?
|
53
|
+
def enabled?
|
54
|
+
@cache_enabled
|
55
|
+
end
|
56
|
+
|
57
|
+
# Enable caching
|
58
|
+
def enable!
|
59
|
+
raise NotImplementedError
|
60
|
+
end
|
61
|
+
|
62
|
+
# Disable caching - all queries will be run directly
|
63
|
+
# using the standard OpenURI `open` method.
|
64
|
+
def disable!
|
65
|
+
@cache_enabled = false
|
66
|
+
end
|
67
|
+
|
68
|
+
def disabled?
|
69
|
+
!@cache_enabled
|
70
|
+
end
|
71
|
+
|
72
|
+
def get(key)
|
73
|
+
raise NotImplementedError
|
74
|
+
end
|
75
|
+
|
76
|
+
def set(key, value)
|
77
|
+
raise NotImplementedError
|
78
|
+
end
|
79
|
+
|
80
|
+
# How long your caches will be kept for (in seconds)
|
81
|
+
def expiry
|
82
|
+
@expiry ||= 60 * 10
|
83
|
+
end
|
84
|
+
|
85
|
+
def host
|
86
|
+
@host ||= "127.0.0.1:11211"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'openuri/common'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'minigems'
|
5
|
+
rescue LoadError
|
6
|
+
require 'rubygems'
|
7
|
+
end
|
8
|
+
|
9
|
+
gem "memcached", ">= 0.10"
|
10
|
+
require 'memcached'
|
11
|
+
|
12
|
+
module OpenURI
|
13
|
+
class Cache
|
14
|
+
class << self
|
15
|
+
# Enable caching
|
16
|
+
def enable!
|
17
|
+
@cache ||= Memcached.new(host, {
|
18
|
+
:namespace => 'openuri',
|
19
|
+
:no_block => true,
|
20
|
+
:buffer_requests => true
|
21
|
+
})
|
22
|
+
@cache_enabled = true
|
23
|
+
end
|
24
|
+
|
25
|
+
def get(key)
|
26
|
+
@cache.get(key)
|
27
|
+
rescue Memcached::NotFound
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def set(key, value)
|
32
|
+
@cache.set(key, value, expiry)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'openuri/common'
|
2
|
+
|
3
|
+
module OpenURI
|
4
|
+
class Cache
|
5
|
+
class << self
|
6
|
+
# Enable caching
|
7
|
+
def enable!
|
8
|
+
@cache ||= Rails.cache
|
9
|
+
@cache_enabled = true
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(key)
|
13
|
+
@cache.fetch(key) { false }
|
14
|
+
end
|
15
|
+
|
16
|
+
def set(key, value)
|
17
|
+
@cache.write(key, value, expiry)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'openuri/memcached'
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: technoweenie-openuri_memcached
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Schwarz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-11 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: OpenURI with transparent caching
|
17
|
+
email: ben@germanforblack.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.markdown
|
26
|
+
- History.txt
|
27
|
+
- License.txt
|
28
|
+
- lib/openuri_memcached.rb
|
29
|
+
- lib/openuri/memcached.rb
|
30
|
+
- lib/openuri/common.rb
|
31
|
+
- lib/openuri/rails-cache.rb
|
32
|
+
has_rdoc: false
|
33
|
+
homepage: http://github.com/benschwarz/open-uri-memcached
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: The same OpenURI that you know and love with the power of Memcached
|
58
|
+
test_files: []
|
59
|
+
|