opsb-ziggy 0.1.0
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/README.markdown +96 -0
- data/lib/ziggy.rb +60 -0
- data/ziggy.gemspec +36 -0
- metadata +55 -0
data/README.markdown
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
ziggy
|
|
2
|
+
=====
|
|
3
|
+
|
|
4
|
+
Cache any method on any class using rails.cache
|
|
5
|
+
|
|
6
|
+
Install
|
|
7
|
+
-------
|
|
8
|
+
|
|
9
|
+
$ gem sources -a http://gems.github.com (you only have to do this once)
|
|
10
|
+
$ sudo gem install opsb-ziggy
|
|
11
|
+
|
|
12
|
+
Introduction
|
|
13
|
+
------------
|
|
14
|
+
|
|
15
|
+
### Cache a method
|
|
16
|
+
|
|
17
|
+
class TwitterUser
|
|
18
|
+
include Ziggy
|
|
19
|
+
cached :timeline
|
|
20
|
+
|
|
21
|
+
def timeline
|
|
22
|
+
twitter_client.timeline
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
...
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
### Cache many methods
|
|
30
|
+
|
|
31
|
+
class TwitterUser
|
|
32
|
+
include Ziggy
|
|
33
|
+
cached :timeline, :direct_messages
|
|
34
|
+
|
|
35
|
+
def timeline
|
|
36
|
+
twitter_client.timeline
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def direct_messages
|
|
40
|
+
twitter_client.direct_messages
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
...
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
### expire_after
|
|
48
|
+
|
|
49
|
+
class TwitterUser
|
|
50
|
+
include Ziggy
|
|
51
|
+
cached :timeline, :expire_after => 1.5.minutes
|
|
52
|
+
|
|
53
|
+
def timeline
|
|
54
|
+
twitter_client.timeline
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
...
|
|
58
|
+
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
### Customise key
|
|
62
|
+
|
|
63
|
+
The method name and arguments are always used as the base of the cache key. Using a block you can differentiate the keys based on further criteria e.g. screen_name.
|
|
64
|
+
|
|
65
|
+
class TwitterUser
|
|
66
|
+
include Ziggy
|
|
67
|
+
cached :timeline { |twitterUser| twitterUser.screen_name }
|
|
68
|
+
|
|
69
|
+
def timeline
|
|
70
|
+
twitter_client.timeline
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
...
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
### Different options for different methods
|
|
78
|
+
|
|
79
|
+
The method name and arguments are always used as the base of the cache key. You can customise the start of the cache key using a block.
|
|
80
|
+
|
|
81
|
+
class TwitterUser
|
|
82
|
+
include Ziggy
|
|
83
|
+
cached :timeline, :expire_after => 1.minutes { |twitterUser| twitterUser.screen_name }
|
|
84
|
+
cached :direct_messages, :expire_after => 10.minutes { |twitterUser| twitterUser.screen_name }
|
|
85
|
+
|
|
86
|
+
def timeline
|
|
87
|
+
twitter_client.timeline
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def direct_messages
|
|
91
|
+
twitter_client.direct_messages
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
...
|
|
95
|
+
|
|
96
|
+
end
|
data/lib/ziggy.rb
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
module Ziggy2
|
|
2
|
+
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.extend ClassMethods
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
@should_be_cached = []
|
|
7
|
+
@cached = []
|
|
8
|
+
@keygens = {}
|
|
9
|
+
@expire_after = {}
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
def cached(*cachable_methods, &block)
|
|
15
|
+
opts = (cachable_methods.pop if cachable_methods.last.kind_of? Hash) || {}
|
|
16
|
+
@should_be_cached += cachable_methods
|
|
17
|
+
cachable_methods.each do |m|
|
|
18
|
+
@keygens[m] = block
|
|
19
|
+
@expire_after[m] = opts[:expire_after] || 2.5.minutes
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def should_be_cached?(method)
|
|
24
|
+
@should_be_cached.include? method
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def cached?(method)
|
|
28
|
+
@cached.include? method
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def method_added(method)
|
|
32
|
+
return unless should_be_cached?(method) && !cached?(method)
|
|
33
|
+
@cached << method
|
|
34
|
+
method_without_cache = "#{method}_without_cache".to_sym
|
|
35
|
+
class_eval do
|
|
36
|
+
alias_method method_without_cache, method
|
|
37
|
+
define_method(method) do |*args|
|
|
38
|
+
key = self.class.build_key(self, method, args)
|
|
39
|
+
return Rails.cache.read(key) if Rails.cache.exist?(key)
|
|
40
|
+
result = send(method_without_cache, *args)
|
|
41
|
+
Rails.cache.write(key, result, :expires_in => expire_after(method))
|
|
42
|
+
result
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
logger.debug "Caching added to #{self}.#{method}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def expire_after(method)
|
|
49
|
+
@expire_after[method]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def build_key(instance, method, args)
|
|
53
|
+
invocation_key = "#{method}#{ args.collect{ |a| a.to_s } }"
|
|
54
|
+
keygen = @keygens[method]
|
|
55
|
+
differentiator = (keygen.call(instance) unless keygen.nil?) || ""
|
|
56
|
+
differentiator + invocation_key
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
data/ziggy.gemspec
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{ziggy}
|
|
5
|
+
s.version = "0.1.0"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["Oliver Searle-Barnes"]
|
|
9
|
+
s.date = %q{2009-07-18}
|
|
10
|
+
s.description = %q{ziggy can be used to cache any method on any class and allows custom keys and expiration times}
|
|
11
|
+
s.email = %q{oliver@opsb.co.uk}
|
|
12
|
+
s.extra_rdoc_files = [
|
|
13
|
+
"README.markdown"
|
|
14
|
+
]
|
|
15
|
+
s.files = [
|
|
16
|
+
"README.markdown",
|
|
17
|
+
"lib/ziggy.rb",
|
|
18
|
+
"ziggy.gemspec"
|
|
19
|
+
]
|
|
20
|
+
s.has_rdoc = true
|
|
21
|
+
s.homepage = %q{http://github.com/opsb/ziggy}
|
|
22
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
23
|
+
s.require_paths = ["lib"]
|
|
24
|
+
s.rubygems_version = %q{1.3.1}
|
|
25
|
+
s.summary = %q{Cache any method on any class}
|
|
26
|
+
|
|
27
|
+
if s.respond_to? :specification_version then
|
|
28
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
29
|
+
s.specification_version = 2
|
|
30
|
+
|
|
31
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
32
|
+
else
|
|
33
|
+
end
|
|
34
|
+
else
|
|
35
|
+
end
|
|
36
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: opsb-ziggy
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Oliver Searle-Barnes
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-07-18 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: ziggy can be used to cache any method on any class and allows custom keys and expiration times
|
|
17
|
+
email: oliver@opsb.co.uk
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README.markdown
|
|
24
|
+
files:
|
|
25
|
+
- README.markdown
|
|
26
|
+
- lib/ziggy.rb
|
|
27
|
+
- ziggy.gemspec
|
|
28
|
+
has_rdoc: true
|
|
29
|
+
homepage: http://github.com/opsb/ziggy
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options:
|
|
32
|
+
- --charset=UTF-8
|
|
33
|
+
require_paths:
|
|
34
|
+
- lib
|
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: "0"
|
|
40
|
+
version:
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0"
|
|
46
|
+
version:
|
|
47
|
+
requirements: []
|
|
48
|
+
|
|
49
|
+
rubyforge_project:
|
|
50
|
+
rubygems_version: 1.2.0
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 2
|
|
53
|
+
summary: Cache any method on any class
|
|
54
|
+
test_files: []
|
|
55
|
+
|