sequel_acts_as_cacheable 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sequel_acts_as_cacheable.rb +129 -0
- metadata +45 -0
@@ -0,0 +1,129 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module ActsAsCacheable
|
4
|
+
def self.configure(model, opts={})
|
5
|
+
model.instance_eval do
|
6
|
+
@acts_as_cacheable_cache = opts[:cache]
|
7
|
+
@acts_as_cacheable_time_to_live = opts[:time_to_live] || 3600
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
attr_accessor :acts_as_cacheable_cache
|
13
|
+
attr_accessor :acts_as_cacheable_time_to_live
|
14
|
+
|
15
|
+
# Copy the necessary class instance variables to the subclass.
|
16
|
+
def inherited(subclass)
|
17
|
+
super
|
18
|
+
subclass.acts_as_cacheable_cache = acts_as_cacheable_cache
|
19
|
+
subclass.acts_as_cacheable_time_to_live = acts_as_cacheable_time_to_live
|
20
|
+
end
|
21
|
+
|
22
|
+
def model_cache_key model_id
|
23
|
+
base_model_klass = self
|
24
|
+
base_model_klass = base_model_klass.superclass while Sequel::Model != base_model_klass.superclass
|
25
|
+
"#{base_model_klass.name}~#{model_id}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def [](*args)
|
29
|
+
is_primary_key_lookup =
|
30
|
+
if 1 == args.size
|
31
|
+
if Fixnum == args.first.class
|
32
|
+
true
|
33
|
+
else
|
34
|
+
if String == args.first.class
|
35
|
+
begin
|
36
|
+
Integer(args.first)
|
37
|
+
true
|
38
|
+
rescue
|
39
|
+
false
|
40
|
+
end
|
41
|
+
else
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
else
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
if is_primary_key_lookup
|
50
|
+
key = model_cache_key args.first
|
51
|
+
begin
|
52
|
+
cache_value = @acts_as_cacheable_cache.get key
|
53
|
+
rescue Exception => e
|
54
|
+
logger.error "CACHE.get failed in primary key lookup with args #{args.inspect} and model #{self.class.name} so using mysql lookup instead, exception was #{e.inspect}"
|
55
|
+
cache_value = super *args
|
56
|
+
end
|
57
|
+
|
58
|
+
if !cache_value
|
59
|
+
cache_value = super *args
|
60
|
+
cache_value.cache! if cache_value
|
61
|
+
end
|
62
|
+
|
63
|
+
cache_value
|
64
|
+
else
|
65
|
+
super *args
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
module InstanceMethods
|
71
|
+
def cache!
|
72
|
+
unless new?
|
73
|
+
marshallable!
|
74
|
+
if respond_to? :before_cache
|
75
|
+
before_cache
|
76
|
+
end
|
77
|
+
acts_as_cacheable_cache.set model_cache_key, self, acts_as_cacheable_time_to_live
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def uncache!
|
82
|
+
acts_as_cacheable_cache.delete self.class.model_cache_key id
|
83
|
+
end
|
84
|
+
|
85
|
+
alias_method :invalidate_cache!, :uncache!
|
86
|
+
|
87
|
+
def cached?
|
88
|
+
acts_as_cacheable_cache.get(model_cache_key).present?
|
89
|
+
end
|
90
|
+
|
91
|
+
def save *columns
|
92
|
+
result = super *columns
|
93
|
+
invalidate_cache! if result && !new?
|
94
|
+
result
|
95
|
+
end
|
96
|
+
|
97
|
+
def delete
|
98
|
+
result = super
|
99
|
+
invalidate_cache!
|
100
|
+
result
|
101
|
+
end
|
102
|
+
|
103
|
+
def destroy opts = {}
|
104
|
+
result = super opts
|
105
|
+
invalidate_cache! unless false == result
|
106
|
+
result
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
def model_cache_key
|
111
|
+
model.model_cache_key id
|
112
|
+
end
|
113
|
+
|
114
|
+
def acts_as_cacheable_cache
|
115
|
+
model.acts_as_cacheable_cache
|
116
|
+
end
|
117
|
+
|
118
|
+
def acts_as_cacheable_time_to_live
|
119
|
+
model.acts_as_cacheable_time_to_live
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel_acts_as_cacheable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Misha Conway
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-13 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: MishaAConway@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/sequel_acts_as_cacheable.rb
|
21
|
+
homepage:
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ! '>='
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
none: false
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
none: false
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project: nowarning
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Memcaching for sequel models.
|
45
|
+
test_files: []
|