ziggy 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/LICENSE +20 -0
  2. data/README.markdown +103 -0
  3. data/RakeFile +15 -0
  4. data/VERSION.yml +4 -0
  5. data/lib/ziggy.rb +70 -0
  6. data/ziggy.gemspec +40 -0
  7. metadata +68 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Oliver Searle-Barnes
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.
@@ -0,0 +1,103 @@
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
+ ziggy will cache methods and then expire them. The results from method invocations are stored in and read from the Rails.cache using Rails.cache.write(key, value) and Rails.cache.read(key).
16
+
17
+ ### Cache a method
18
+
19
+ class TwitterUser
20
+ include Ziggy
21
+ cached :timeline
22
+
23
+ def timeline
24
+ twitter_client.timeline
25
+ end
26
+
27
+ ...
28
+
29
+ end
30
+
31
+ ### Cache many methods
32
+
33
+ class TwitterUser
34
+ include Ziggy
35
+ cached :timeline, :direct_messages
36
+
37
+ def timeline
38
+ twitter_client.timeline
39
+ end
40
+
41
+ def direct_messages
42
+ twitter_client.direct_messages
43
+ end
44
+
45
+ ...
46
+
47
+ end
48
+
49
+ ### expire_after
50
+
51
+ The default expire_after time is 2.5.minutes, to customise do
52
+
53
+ class TwitterUser
54
+ include Ziggy
55
+ cached :timeline, :expire_after => 1.5.minutes
56
+
57
+ def timeline
58
+ twitter_client.timeline
59
+ end
60
+
61
+ ...
62
+
63
+ end
64
+
65
+ ### Customise key
66
+
67
+ 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.
68
+
69
+ class TwitterUser
70
+ include Ziggy
71
+ cached :timeline { |twitterUser| twitterUser.screen_name }
72
+
73
+ def timeline
74
+ twitter_client.timeline
75
+ end
76
+
77
+ ...
78
+
79
+ end
80
+
81
+ ### Different options for different methods
82
+
83
+ class TwitterUser
84
+ include Ziggy
85
+ cached :timeline, :expire_after => 1.minutes { |twitterUser| twitterUser.screen_name }
86
+ cached :direct_messages, :expire_after => 10.minutes { |twitterUser| twitterUser.screen_name }
87
+
88
+ def timeline
89
+ twitter_client.timeline
90
+ end
91
+
92
+ def direct_messages
93
+ twitter_client.direct_messages
94
+ end
95
+
96
+ ...
97
+
98
+ end
99
+
100
+ ### Disable ziggy
101
+ Perhaps you want to disable ziggy for tests? Add the following to your test.rb
102
+
103
+ Ziggy::active = false
@@ -0,0 +1,15 @@
1
+ task :default => [:gemspec]
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = "ziggy"
7
+ gemspec.summary = "Cache any method on any class"
8
+ gemspec.description = "ziggy can be used to cache any method on any class and allows custom keys and expiration times"
9
+ gemspec.email = "oliver@opsb.co.uk"
10
+ gemspec.homepage = "http://github.com/opsb/ziggy"
11
+ gemspec.authors = ["Oliver Searle-Barnes"]
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 7
4
+ :major: 0
@@ -0,0 +1,70 @@
1
+ module Ziggy
2
+ @@active = true
3
+
4
+ def self.active=(active)
5
+ @@active = active
6
+ end
7
+
8
+ def self.active
9
+ @@active
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend ClassMethods
14
+ base.instance_eval do
15
+ @should_be_cached = []
16
+ @cached = []
17
+ @keygens = {}
18
+ @expire_after = {}
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+ def cached(*cachable_methods, &block)
24
+ return unless Ziggy::active
25
+ opts = (cachable_methods.pop if cachable_methods.last.kind_of? Hash) || {}
26
+ @should_be_cached += cachable_methods
27
+ cachable_methods.each do |m|
28
+ @keygens[m] = block
29
+ @expire_after[m] = opts[:expire_after] || 2.5.minutes
30
+ end
31
+ end
32
+
33
+ def should_be_cached?(method)
34
+ @should_be_cached.include? method
35
+ end
36
+
37
+ def cached?(method)
38
+ @cached.include? method
39
+ end
40
+
41
+ def method_added(method)
42
+ return unless should_be_cached?(method) && !cached?(method)
43
+ @cached << method
44
+ method_without_cache = "#{method}_without_cache".to_sym
45
+ class_eval do
46
+ alias_method method_without_cache, method
47
+ define_method(method) do |*args|
48
+ key = self.class.build_key(self, method, args)
49
+ return Rails.cache.read(key) if Rails.cache.exist?(key)
50
+ result = send(method_without_cache, *args)
51
+ Rails.cache.write(key, result, :expires_in => self.class.expire_after(method))
52
+ result
53
+ end
54
+ end
55
+ logger.debug "Caching added to #{self}.#{method}"
56
+ end
57
+
58
+ def expire_after(method)
59
+ @expire_after[method]
60
+ end
61
+
62
+ def build_key(instance, method, args)
63
+ invocation_key = "#{method}#{ args.collect{ |a| a.to_s } }"
64
+ keygen = @keygens[method]
65
+ differentiator = (keygen.call(instance) unless keygen.nil?) || ""
66
+ differentiator + invocation_key
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{ziggy}
5
+ s.version = "0.1.7"
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-19}
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
+ "LICENSE",
14
+ "README.markdown"
15
+ ]
16
+ s.files = [
17
+ "LICENSE",
18
+ "README.markdown",
19
+ "RakeFile",
20
+ "VERSION.yml",
21
+ "lib/ziggy.rb",
22
+ "ziggy.gemspec"
23
+ ]
24
+ s.has_rdoc = true
25
+ s.homepage = %q{http://github.com/opsb/ziggy}
26
+ s.rdoc_options = ["--charset=UTF-8"]
27
+ s.require_paths = ["lib"]
28
+ s.rubygems_version = %q{1.3.1}
29
+ s.summary = %q{Cache any method on any class}
30
+
31
+ if s.respond_to? :specification_version then
32
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
33
+ s.specification_version = 2
34
+
35
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
36
+ else
37
+ end
38
+ else
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ziggy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 7
9
+ version: 0.1.7
10
+ platform: ruby
11
+ authors:
12
+ - Oliver Searle-Barnes
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2009-07-19 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: ziggy can be used to cache any method on any class and allows custom keys and expiration times
22
+ email: oliver@opsb.co.uk
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.markdown
30
+ files:
31
+ - LICENSE
32
+ - README.markdown
33
+ - RakeFile
34
+ - VERSION.yml
35
+ - lib/ziggy.rb
36
+ - ziggy.gemspec
37
+ has_rdoc: true
38
+ homepage: http://github.com/opsb/ziggy
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.6
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Cache any method on any class
67
+ test_files: []
68
+