benschwarz-openuri_memcached 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +35 -0
- data/lib/openuri_memcached.rb +78 -0
- metadata +62 -0
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
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
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'memcached'
|
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?
|
18
|
+
begin
|
19
|
+
response = Cache::get(uri.to_s)
|
20
|
+
rescue Memcached::NotFound
|
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.enabled?
|
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 ||= Memcached.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 host
|
74
|
+
@host ||= "127.0.0.1:11211"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benschwarz-openuri_memcached
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Schwarz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-05-14 00:00:00 -07: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: "0.10"
|
23
|
+
version:
|
24
|
+
description: OpenURI with transparent caching
|
25
|
+
email: ben@germanforblack.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- README
|
34
|
+
- lib/openuri_memcached.rb
|
35
|
+
has_rdoc: false
|
36
|
+
homepage: http://github.com/benschwarz/open-uri-memcached
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.0.1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: The same OpenURI that you know and love with the power of Memcached
|
61
|
+
test_files: []
|
62
|
+
|