cachely 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ Transparent method level caching using redis. Event machine optional.
10
10
  cachely :foo
11
11
 
12
12
  def foo
13
- sleep(10)
13
+ sleep(10) #Simulate blocking with a 10s sleep. Could be an ARModel.all command? An API call?
14
14
  return rand(500) #Holy moly, what a HUGE, LONG CALL! Returns a different value every time.
15
15
  end
16
16
 
@@ -56,7 +56,7 @@ use
56
56
 
57
57
  cachely :foo, time_to_expiry: 3.minutes
58
58
 
59
- If you have a class method of the same name as an instance variable,
59
+ If you have a class method of the same name as an instance method,
60
60
 
61
61
  cachely :foo
62
62
 
@@ -69,7 +69,10 @@ keyed locations, of course. To specify instance or class method, do
69
69
  Cachely is able to figure out whether or not to use the cache by looking at the arguments you pass in.
70
70
  Generally, if your arguments are the same, and the object you're calling the method on is the same(Cachely uses
71
71
  the to_json method on the object you call the method on to determine this), then it assumes the result
72
- will be the same and uses the cached result.
72
+ will be the same and uses the cached result.
73
+
74
+ Cachely expects that any objects you return or pass in to methods as arguments have to_json methods. Without
75
+ this, it won't work.
73
76
 
74
77
  CAVEAT: Do NOT use Cachely for functions that depend on time of day or random numbers, as these are inherently uncachable.
75
78
  If you check the tests out, you'll see random number functions are used exhaustively to test the caching ability of cachely,
@@ -86,12 +86,11 @@ module Cachely
86
86
  # Converts method name and arguments into a coherent key. Creates a hash and to_jsons it
87
87
  # And that becomes the redis key. Spiffy, I know.
88
88
  #
89
- # @method [String,Symbol] The method name
89
+ # @method [Object, Symbol, Args] The context, method name symbol, and args.
90
90
  # @return [String] The proper redis key to be used in storage.
91
- def self.redis_key(obj, method, *args)
91
+ def self.redis_key(context, method, *args)
92
92
  map_param_to_s({
93
- :obj => obj.to_s.match(/^#</) ? "instance:#{obj.class}" : obj.to_s,
94
- :attributes => obj.to_json, #Best way to identify objects is to just to_json them.
93
+ :context => context,
95
94
  :method => method,
96
95
  :args => args
97
96
  })
@@ -132,9 +131,10 @@ module Cachely
132
131
  # @s The string to convert
133
132
  # @return The respawned object
134
133
  def self.map_s_to_obj(s)
135
- type = s.split(":").first
136
- data = s.gsub(/^#{type}:/,'')
137
-
134
+ class_or_instance = s.split("|").first
135
+ type = s.split("|")[1]
136
+ data = s.gsub(/^#{class_or_instance}\|#{type}\|/,'')
137
+
138
138
  case type
139
139
  when "TrueClass"
140
140
  return true
@@ -151,10 +151,12 @@ module Cachely
151
151
  when "NilClass"
152
152
  return nil
153
153
  else
154
- obj = Object.const_get(type).new
154
+ class_or_instance == "instance" ? obj = Object.const_get(type).new : obj = Object.const_get(type)
155
+
155
156
  JSON.parse(data).each do |key, value|
156
- obj.send(key+"=",value)
157
+ obj.send(key+"=",value) if obj.respond_to?(key+"=")
157
158
  end
159
+
158
160
  return obj
159
161
  end
160
162
  end
@@ -170,24 +172,31 @@ module Cachely
170
172
  elsif(p.is_a?(Array))
171
173
  return map_array_to_s(p)
172
174
  elsif(p.respond_to?("to_json"))
173
- #below do extra search if string bc string puts annoying quotes in json like "\"1\""
174
- #breaks the parser on the return translation.
175
175
  translated = nil
176
+
176
177
  if p.is_a?(String)
177
- translated = p
178
+ translated = "instance|"+p.class.to_s+"|"+p
178
179
  elsif p.is_a?(Symbol)
179
- translated = p.to_s
180
+ translated = "instance|"+p.class.to_s+"|"+p.to_s
180
181
  elsif p.nil?
181
- translated = "nil"
182
+ translated = "instance|NilClass|nil"
183
+ elsif p.is_a?(TrueClass)
184
+ translated = "instance|TrueClass|true"
185
+ elsif p.is_a?(FalseClass)
186
+ translated = "instance|FalseClass|false"
187
+ elsif p.is_a?(Fixnum)
188
+ translated = "instance|Fixnum|" + p.to_s
189
+ elsif p.is_a?(Float)
190
+ translated = "instance|Float|" + p.to_s
182
191
  elsif p.is_a?(ActiveRecord::Base)
183
192
  #don't want { "dummy_model" = > {:attributes => 1}}
184
193
  #want {:attributes => 1}
185
- translated = JSON.parse(p.to_json)[p.class.to_s.underscore].to_json
186
- else
187
- translated = p.to_json
194
+ translated = "instance|#{p.class.to_s}|#{JSON.parse(p.to_json)[p.class.to_s.underscore].to_json}"
195
+ else
196
+ translated = (p.to_s.match(/^#</) ? "instance|#{p.class}" : "class|#{p.to_s}") + "|"+ p.to_json
188
197
  end
189
198
 
190
- return p.class.to_s + ":" + translated
199
+ return translated
191
200
  end
192
201
  end
193
202
 
@@ -215,4 +224,4 @@ module Cachely
215
224
 
216
225
 
217
226
  end
218
- end
227
+ end
@@ -1,3 +1,3 @@
1
1
  module Cachely
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -135,7 +135,7 @@ class CachelyTest < BaseTest
135
135
  klazz = DummyClass
136
136
  assert_not_equal(obj.class_diff, klazz.class_diff)
137
137
  end
138
-
138
+
139
139
  test "cache expires" do
140
140
  obj = DummyClass.new
141
141
  old = obj.cache_expiry
@@ -17,7 +17,6 @@ class ConversionsTest < BaseTest
17
17
  :method => :foo,
18
18
  :args => [3,4]
19
19
  })
20
- assert_equal("{\"Symbol:method\":\"Symbol:foo\",\"Symbol:args\":\"[\\\"Fixnum:3\\\",\\\"Fixnum:4\\\"]\"}",str)
21
20
  reformed = Cachely::Mechanics.map_s_to_param(str)
22
21
  assert_equal(2, reformed.keys.size)
23
22
  assert_equal(:foo, reformed[:method])
@@ -37,6 +36,12 @@ class ConversionsTest < BaseTest
37
36
  reformed = Cachely::Mechanics.map_s_to_param(str)
38
37
  assert_equal(d.random_no, reformed.random_no)
39
38
  end
39
+
40
+ test "class conversion" do
41
+ str = Cachely::Mechanics.map_param_to_s(DummyClass)
42
+ reformed = Cachely::Mechanics.map_s_to_param(str)
43
+ assert_equal(reformed, DummyClass)
44
+ end
40
45
 
41
46
  test "orm conversion" do
42
47
  d = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
@@ -48,21 +53,21 @@ class ConversionsTest < BaseTest
48
53
  end
49
54
 
50
55
  test "primitives conversion" do
51
- assert_equal("TrueClass:true", Cachely::Mechanics.map_param_to_s(true))
52
- assert_equal("FalseClass:false", Cachely::Mechanics.map_param_to_s(false))
53
- assert_equal("Fixnum:1", Cachely::Mechanics.map_param_to_s(1))
54
- assert_equal("Float:1.1", Cachely::Mechanics.map_param_to_s(1.1))
55
- assert_equal("String:1", Cachely::Mechanics.map_param_to_s("1"))
56
- assert_equal("NilClass:nil", Cachely::Mechanics.map_param_to_s(nil))
57
- assert_equal("Symbol:shit", Cachely::Mechanics.map_param_to_s(:shit))
58
- assert_equal(true, Cachely::Mechanics.map_s_to_param("TrueClass:true"))
59
- assert_equal(false, Cachely::Mechanics.map_s_to_param("FalseClass:false"))
60
- assert_equal(1, Cachely::Mechanics.map_s_to_param("Fixnum:1"))
61
- assert_equal(1.1, Cachely::Mechanics.map_s_to_param("Float:1.1"))
62
- assert_equal("1", Cachely::Mechanics.map_s_to_param("String:1"))
63
- assert_equal("1:2", Cachely::Mechanics.map_s_to_param("String:1:2"))
64
- assert_equal(:shit, Cachely::Mechanics.map_s_to_param("Symbol:shit"))
65
- assert_equal(nil, Cachely::Mechanics.map_s_to_param("NilClass:nil"))
56
+ assert_equal("instance|TrueClass|true", Cachely::Mechanics.map_param_to_s(true))
57
+ assert_equal("instance|FalseClass|false", Cachely::Mechanics.map_param_to_s(false))
58
+ assert_equal("instance|Fixnum|1", Cachely::Mechanics.map_param_to_s(1))
59
+ assert_equal("instance|Float|1.1", Cachely::Mechanics.map_param_to_s(1.1))
60
+ assert_equal("instance|String|1", Cachely::Mechanics.map_param_to_s("1"))
61
+ assert_equal("instance|NilClass|nil", Cachely::Mechanics.map_param_to_s(nil))
62
+ assert_equal("instance|Symbol|shit", Cachely::Mechanics.map_param_to_s(:shit))
63
+ assert_equal(true, Cachely::Mechanics.map_s_to_param("instance|TrueClass|true"))
64
+ assert_equal(false, Cachely::Mechanics.map_s_to_param("instance|FalseClass|false"))
65
+ assert_equal(1, Cachely::Mechanics.map_s_to_param("instance|Fixnum|1"))
66
+ assert_equal(1.1, Cachely::Mechanics.map_s_to_param("instance|Float|1.1"))
67
+ assert_equal("1", Cachely::Mechanics.map_s_to_param("instance|String|1"))
68
+ assert_equal("1|2", Cachely::Mechanics.map_s_to_param("instance|String|1|2"))
69
+ assert_equal(:shit, Cachely::Mechanics.map_s_to_param("instance|Symbol|shit"))
70
+ assert_equal(nil, Cachely::Mechanics.map_s_to_param("instance|NilClass|nil"))
66
71
 
67
72
  end
68
- end
73
+ end
metadata CHANGED
@@ -1,71 +1,67 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cachely
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
4
5
  prerelease:
5
- version: 0.0.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Jordan Prince
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2013-03-04 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: redis
17
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &16110720 !ruby/object:Gem::Requirement
18
17
  none: false
19
- requirements:
18
+ requirements:
20
19
  - - ~>
21
- - !ruby/object:Gem::Version
20
+ - !ruby/object:Gem::Version
22
21
  version: 3.0.1
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
24
+ version_requirements: *16110720
25
+ - !ruby/object:Gem::Dependency
27
26
  name: hiredis
28
- requirement: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &16121280 !ruby/object:Gem::Requirement
29
28
  none: false
30
- requirements:
29
+ requirements:
31
30
  - - ~>
32
- - !ruby/object:Gem::Version
31
+ - !ruby/object:Gem::Version
33
32
  version: 0.4.5
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
35
+ version_requirements: *16121280
36
+ - !ruby/object:Gem::Dependency
38
37
  name: em-synchrony
39
- requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &16118820 !ruby/object:Gem::Requirement
40
39
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
45
44
  type: :runtime
46
45
  prerelease: false
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
46
+ version_requirements: *16118820
47
+ - !ruby/object:Gem::Dependency
49
48
  name: json
50
- requirement: &id004 !ruby/object:Gem::Requirement
49
+ requirement: &16116540 !ruby/object:Gem::Requirement
51
50
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
56
55
  type: :runtime
57
56
  prerelease: false
58
- version_requirements: *id004
57
+ version_requirements: *16116540
59
58
  description: Transparently cache the results of methods using redis.
60
- email:
59
+ email:
61
60
  - jordanmprince@gmail.com
62
61
  executables: []
63
-
64
62
  extensions: []
65
-
66
63
  extra_rdoc_files: []
67
-
68
- files:
64
+ files:
69
65
  - .gitignore
70
66
  - Gemfile
71
67
  - LICENSE
@@ -84,40 +80,31 @@ files:
84
80
  - test/unit/cachely_test.rb
85
81
  - test/unit/conversions_test.rb
86
82
  - test/unit/mechanics_test.rb
87
- homepage: ""
83
+ homepage: ''
88
84
  licenses: []
89
-
90
85
  post_install_message:
91
86
  rdoc_options: []
92
-
93
- require_paths:
87
+ require_paths:
94
88
  - lib
95
- required_ruby_version: !ruby/object:Gem::Requirement
89
+ required_ruby_version: !ruby/object:Gem::Requirement
96
90
  none: false
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- hash: -39538212363365102
101
- segments:
102
- - 0
103
- version: "0"
104
- required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
96
  none: false
106
- requirements:
107
- - - ">="
108
- - !ruby/object:Gem::Version
109
- hash: -39538212363365102
110
- segments:
111
- - 0
112
- version: "0"
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
113
101
  requirements: []
114
-
115
102
  rubyforge_project:
116
- rubygems_version: 1.8.24
103
+ rubygems_version: 1.8.11
117
104
  signing_key:
118
105
  specification_version: 3
119
106
  summary: Transparently cache the results of methods using redis.
120
- test_files:
107
+ test_files:
121
108
  - test/models/dummy_class.rb
122
109
  - test/models/dummy_model.rb
123
110
  - test/unit/base_test.rb