benschwarz-openuri_memcached 0.1.3 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +18 -0
- data/License.txt +20 -0
- data/README.markdown +48 -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 -107
- metadata +9 -12
- data/README +0 -35
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,48 @@
|
|
1
|
+
# OpenURI with caching
|
2
|
+
|
3
|
+
Carelessly make OpenURI requests without getting hate mail.
|
4
|
+
|
5
|
+
## Running with MemCached
|
6
|
+
|
7
|
+
Require the library
|
8
|
+
|
9
|
+
require 'openuri/memcached'
|
10
|
+
|
11
|
+
Start memcached server
|
12
|
+
|
13
|
+
ben@Spinners ~/ Ϟ memcached -d
|
14
|
+
|
15
|
+
Set your memcached host/s (defaults to 127.0.0.1:11211)
|
16
|
+
|
17
|
+
OpenURI::Cache.host = ['10.1.1.10:11211', '10.1.1.11:11211']
|
18
|
+
|
19
|
+
The default expiry is 15 minutes, this can be changed using the `expiry` method
|
20
|
+
|
21
|
+
# Ten long minutes
|
22
|
+
OpenURI::Cache.expiry = 600
|
23
|
+
|
24
|
+
## Running using Rails cache
|
25
|
+
|
26
|
+
You can also cache your OpenURI calls using Rails cache.
|
27
|
+
require the library using `require openuri/rails-cache`
|
28
|
+
|
29
|
+
### Execution
|
30
|
+
Use exactly the same as you would OpenURI, only.. enable it.
|
31
|
+
|
32
|
+
OpenURI::Cache.enable!
|
33
|
+
# As slow a wet week
|
34
|
+
open("http://ab-c.com.au").read
|
35
|
+
|
36
|
+
Quit your app (leave memcached running) and re-run the same request, It will come from cache.
|
37
|
+
|
38
|
+
### Requirements
|
39
|
+
|
40
|
+
* Ruby
|
41
|
+
* MemCached
|
42
|
+
* memcache (gem)
|
43
|
+
* 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
|
44
|
+
|
45
|
+
### Contributors
|
46
|
+
|
47
|
+
* [Ben Askins](http://github.com/benaskins)
|
48
|
+
* [Rick Olson](http://github.com/technoweenie)
|
@@ -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
|
data/lib/openuri_memcached.rb
CHANGED
@@ -1,107 +1 @@
|
|
1
|
-
require '
|
2
|
-
|
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(name, *rest, &block)
|
15
|
-
if name.respond_to?(:open)
|
16
|
-
name.open(*rest, &block)
|
17
|
-
elsif name.respond_to?(:to_str) &&
|
18
|
-
%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
|
19
|
-
(uri = URI.parse(name)).respond_to?(:open)
|
20
|
-
OpenURI::open(name, *rest, &block)
|
21
|
-
else
|
22
|
-
open_uri_original_open(name, *rest, &block)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
module_function :open, :openuri_original_open
|
26
|
-
end
|
27
|
-
|
28
|
-
module OpenURI
|
29
|
-
alias original_open open #:nodoc:
|
30
|
-
def self.open(uri, *rest, &block)
|
31
|
-
if Cache.enabled?
|
32
|
-
begin
|
33
|
-
response = Cache::get(uri.to_s)
|
34
|
-
rescue Memcached::NotFound
|
35
|
-
response = false
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
unless response
|
40
|
-
response = openuri_original_open(uri, *rest).read
|
41
|
-
Cache::set(uri.to_s, response) if Cache.enabled?
|
42
|
-
end
|
43
|
-
|
44
|
-
response = StringIO.new(response)
|
45
|
-
|
46
|
-
if block_given?
|
47
|
-
begin
|
48
|
-
yield response
|
49
|
-
ensure
|
50
|
-
response.close
|
51
|
-
end
|
52
|
-
else
|
53
|
-
response
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
class Cache
|
58
|
-
# Cache is not enabled by default
|
59
|
-
@cache_enabled = false
|
60
|
-
|
61
|
-
class << self
|
62
|
-
attr_writer :expiry, :host
|
63
|
-
|
64
|
-
# Is the cache enabled?
|
65
|
-
def enabled?
|
66
|
-
@cache_enabled
|
67
|
-
end
|
68
|
-
|
69
|
-
# Enable caching
|
70
|
-
def enable!
|
71
|
-
@cache ||= Memcached.new(host, {
|
72
|
-
:namespace => 'openuri',
|
73
|
-
:no_block => true,
|
74
|
-
:buffer_requests => true
|
75
|
-
})
|
76
|
-
@cache_enabled = true
|
77
|
-
end
|
78
|
-
|
79
|
-
# Disable caching - all queries will be run directly
|
80
|
-
# using the standard OpenURI `open` method.
|
81
|
-
def disable!
|
82
|
-
@cache_enabled = false
|
83
|
-
end
|
84
|
-
|
85
|
-
def disabled?
|
86
|
-
!@cache_enabled
|
87
|
-
end
|
88
|
-
|
89
|
-
def get(key)
|
90
|
-
@cache.get(key)
|
91
|
-
end
|
92
|
-
|
93
|
-
def set(key, value)
|
94
|
-
@cache.set(key, value, expiry)
|
95
|
-
end
|
96
|
-
|
97
|
-
# How long your caches will be kept for (in seconds)
|
98
|
-
def expiry
|
99
|
-
@expiry ||= 60 * 10
|
100
|
-
end
|
101
|
-
|
102
|
-
def host
|
103
|
-
@host ||= "127.0.0.1:11211"
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
1
|
+
require 'openuri/memcached'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: benschwarz-openuri_memcached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.2"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Schwarz
|
@@ -11,16 +11,8 @@ cert_chain: []
|
|
11
11
|
|
12
12
|
date: 2009-01-11 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
name: memcached
|
17
|
-
version_requirement:
|
18
|
-
version_requirements: !ruby/object:Gem::Requirement
|
19
|
-
requirements:
|
20
|
-
- - ">="
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: "0.10"
|
23
|
-
version:
|
14
|
+
dependencies: []
|
15
|
+
|
24
16
|
description: OpenURI with transparent caching
|
25
17
|
email: ben@germanforblack.com
|
26
18
|
executables: []
|
@@ -30,8 +22,13 @@ extensions: []
|
|
30
22
|
extra_rdoc_files: []
|
31
23
|
|
32
24
|
files:
|
33
|
-
- README
|
25
|
+
- README.markdown
|
26
|
+
- History.txt
|
27
|
+
- License.txt
|
34
28
|
- lib/openuri_memcached.rb
|
29
|
+
- lib/openuri/memcached.rb
|
30
|
+
- lib/openuri/common.rb
|
31
|
+
- lib/openuri/rails-cache.rb
|
35
32
|
has_rdoc: false
|
36
33
|
homepage: http://github.com/benschwarz/open-uri-memcached
|
37
34
|
post_install_message:
|
data/README
DELETED
@@ -1,35 +0,0 @@
|
|
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
|
-
|
7
|
-
Require the library using
|
8
|
-
require 'openuri_memcached'
|
9
|
-
|
10
|
-
To get started run your memcached server
|
11
|
-
$ memcached -d
|
12
|
-
The default address that this gem will terminate against is localhost:11211, you can change this using
|
13
|
-
OpenURI::Cache.host = ['10.1.1.10:11211', '10.1.1.11:11211']
|
14
|
-
|
15
|
-
The cache defaults to 15 minutes, however this can be changed using:
|
16
|
-
OpenURI::Cache.expiry = 60 * 10 # Ten long minutes
|
17
|
-
|
18
|
-
Execution
|
19
|
-
Use exactly the same as you would openuri, only.. enable it.
|
20
|
-
|
21
|
-
require 'openuri_memcached'
|
22
|
-
OpenURI::Cache.enable!
|
23
|
-
open("http://germanforblack.com").read # Slow as a wet week
|
24
|
-
|
25
|
-
Quit your app (leave memcached running) and run the same example, it
|
26
|
-
should now happen in less then ... some time that is really fast.
|
27
|
-
|
28
|
-
Questions and comments can be directed to ben at germanforblack.com
|
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
|
-
|