model-cache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ 0.1.0 2010-01-12
2
+ intial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Frantisek Havluj
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.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ h1. ModelCache
2
+
3
+ ModelCache is a simple caching plugin for Rails, using memcached. It provides caching abilities for your models, allowing to:
4
+ * cache blocks of code in your model instance methods, based on a generic key (ActiveRecord cache_key is added behind the scenes)
5
+ * cache your instance methods, optionally with a time-to-live setting
6
+ * cache some generic code (e.g. in your class methods)
7
+
8
+ h1. Example
9
+
10
+ p. environment.rb:
11
+
12
+ bc. require 'memcache'
13
+ CACHE = MemCache.new('127.0.0.1')
14
+
15
+ p. Your model:
16
+
17
+ bc.. class Stuff < ActiveRecord::Base
18
+
19
+ def expensive_method
20
+ ...
21
+ end
22
+
23
+ def another_expensive_method
24
+ ...
25
+ end
26
+
27
+ cache_method :expensive_method, :another_expensive_method
28
+
29
+ def third_expensive_method
30
+ ...
31
+ end
32
+
33
+ cache_method_for_time :third_expensive_method, 1.hour
34
+
35
+ def partially_expensive_method
36
+ ...
37
+ cache :calculation do
38
+ ...
39
+ end
40
+ end
41
+
42
+ def self.some_class_method
43
+ ModelCache.cache(:this_would_be_persistent, 5.minutes) do
44
+ ...
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ p. Cached!
51
+
52
+ Copyright (c) 2010 Frantisek Havluj, released under the MIT license
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # include the library
2
+ ActiveRecord::Base.send(:include, ModelCache)
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../init.rb'
@@ -0,0 +1,62 @@
1
+ module ModelCache
2
+
3
+ DO_CACHE = Rails.configuration.action_controller.perform_caching
4
+ NIL_OBJECT = Object.new
5
+ DEFAULT_TIME = 12.hours
6
+
7
+ def self.included(klass)
8
+ klass.extend ClassMethods
9
+ end
10
+
11
+ def cache(key, time = DEFAULT_TIME, &block)
12
+ ModelCache::cache([self.cache_key, key], time, &block)
13
+ end
14
+
15
+ def self.cache(ckey, time = DEFAULT_TIME, &block)
16
+ if DO_CACHE && (result = CACHE.get(ckey))
17
+ if result == NIL_OBJECT
18
+ nil
19
+ else
20
+ result
21
+ end
22
+ else
23
+ result = block.call
24
+ if DO_CACHE
25
+ if result
26
+ CACHE.set(ckey, result, time)
27
+ else
28
+ CACHE.set(ckey, NIL_OBJECT, time)
29
+ end
30
+ end
31
+ result
32
+ end
33
+ end
34
+
35
+
36
+ module ClassMethods
37
+ def cache_method(*args)
38
+ args.each do |sym|
39
+ cache_method_for_time(sym, DEFAULT_TIME)
40
+ end
41
+ end
42
+
43
+ def cache_method_for_time(sym, time)
44
+ alias_method :"__noncached_#{sym}", sym
45
+ define_method sym do |*args|
46
+ ckey = [self.cache_key, sym, *args]
47
+ ModelCache.cache(ckey, time) do
48
+ self.send(:"__noncached_#{sym}", *args)
49
+ end
50
+ end
51
+ define_method :"__is_cached_#{sym}?" do |*args|
52
+ ckey = [self.cache_key, sym, *args]
53
+ !!( DO_CACHE && CACHE.get(ckey) )
54
+ end
55
+ define_method :"__uncache_#{sym}" do |*args|
56
+ ckey = [self.cache_key, sym, *args]
57
+ CACHE.delete(ckey)
58
+ end
59
+ end
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frantisek Havluj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-12 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: moskyt@rozhled.cz
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/model-cache.rb
26
+ - lib/model_cache.rb
27
+ - init.rb
28
+ - README.rdoc
29
+ - MIT-LICENSE
30
+ - CHANGELOG
31
+ has_rdoc: true
32
+ homepage: http://github.com/moskyt/model-cache
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Rails plugin for caching in models.
59
+ test_files: []
60
+