cachely 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -79,11 +79,33 @@ the method name, and it's arguments. To expire a cached result by this signature
79
79
 
80
80
  Cachely::Mechanics.expire(object, method, *args)
81
81
 
82
- CAVEAT: Do NOT use Cachely for functions that depend on time of day or random numbers, as these are inherently uncachable.
82
+ Want to wipe all keys?
83
+
84
+ Cachely::Mechanics.flush_all_keys
85
+
86
+ And done.
87
+
88
+ ## Caveats
89
+
90
+ CAVEAT 1: Do NOT use Cachely for functions that depend on time of day or random numbers, as these are inherently uncachable.
83
91
  If you check the tests out, you'll see random number functions are used exhaustively to test the caching ability of cachely,
84
92
  because we know the function wasn't called if the second call yields the same number.
85
93
 
86
94
  If you do not understand the implications of the caveat, do not use cachely. You're not ready yet.
95
+
96
+ CAVEAT 2: Do NOT use Cachely on a method that you pass arguments with circular references to themselves, ie
97
+
98
+ hash = {}
99
+ hash[:hash] = hash
100
+ hash.to_json #will throw an error, to_json is used exhaustively by cachely to handle caching properly.
101
+
102
+ This also includes method results - if you ever return a result that has circular references, don't use cachely. If the object or class you're calling the method on
103
+ has circular references to itself, don't use cachely then, either.
104
+
105
+ One exception is ActiveRecord objects, which have already been fixed in this regard. There are three tests in conversion_tests.rb that fail still that deal with this
106
+ caveat. I'll be fixing them in the future and we'll be one caveat shorter.
107
+
108
+
87
109
  ## Installation
88
110
 
89
111
  Add this line to your application's Gemfile:
@@ -3,6 +3,7 @@ class CreateDummyModel < ActiveRecord::Migration
3
3
  create_table :dummy_models do |t|
4
4
  t.string :attr_1
5
5
  t.string :attr_2
6
+ t.integer :dummy_model_id
6
7
  t.timestamps
7
8
  end
8
9
  end
@@ -189,6 +189,7 @@ module Cachely
189
189
  # @p The object to convert
190
190
  # @return [String] The redis coded string.
191
191
  def self.map_param_to_s(p)
192
+
192
193
  if(p.is_a?(Hash))
193
194
  return map_hash_to_s(p)
194
195
  elsif(p.is_a?(Array))
@@ -214,10 +215,17 @@ module Cachely
214
215
  #don't want { "dummy_model" = > {:attributes => 1}}
215
216
  #want {:attributes => 1}
216
217
  translated = "instance|#{p.class.to_s}|#{JSON.parse(p.to_json)[p.class.to_s.underscore].to_json}"
217
- else
218
- translated = (p.to_s.match(/^#</) ? "instance|#{p.class}" : "class|#{p.to_s}") + "|"+ p.to_json
218
+ else
219
+ my_json = nil
220
+ begin
221
+ my_json = p.to_json #active record classes have circ references, we catch them below.
222
+ rescue ActiveSupport::JSON::Encoding::CircularReferenceError => e
223
+ my_json = "{}"
224
+ end
225
+
226
+ translated = (p.to_s.match(/^#</) ? "instance|#{p.class}" : "class|#{p.to_s}") + "|"+ my_json
219
227
  end
220
-
228
+
221
229
  return translated
222
230
  end
223
231
  end
@@ -1,3 +1,3 @@
1
1
  module Cachely
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,5 +1,7 @@
1
1
  class DummyModel < ActiveRecord::Base
2
- attr_accessible :attr_1, :attr_2
2
+ attr_accessible :attr_1, :attr_2, :dummy_model_id
3
+
4
+ belongs_to :dummy_model, :foreign_key => :dummy_model_id
3
5
 
4
6
  def instance_fixnum
5
7
  rand(500)
@@ -5,7 +5,9 @@ require 'yaml'
5
5
  base_dir = File.expand_path(File.join(File.dirname(__FILE__), "../.."))
6
6
  require base_dir + "/lib/cachely.rb"
7
7
  require_relative '../models/dummy_model.rb'
8
+ require_relative '../models/dummy_model_two.rb'
8
9
  require_relative '../models/dummy_class.rb'
10
+ require_relative '../models/dummy_class_2.rb'
9
11
 
10
12
  class BaseTest < ActiveSupport::TestCase
11
13
  def setup
@@ -37,6 +37,56 @@ class ConversionsTest < BaseTest
37
37
  assert_equal(d.random_no, reformed.random_no)
38
38
  end
39
39
 
40
+ test "orm class" do
41
+ assert_nothing_raised(Exception) do
42
+ Cachely::Mechanics.map_param_to_s(DummyModel)
43
+ end
44
+ end
45
+
46
+ test "orm obj referencing itself" do
47
+ d = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
48
+
49
+ d.dummy_model = d
50
+ d.save!
51
+ assert_nothing_raised(Exception) do
52
+ Cachely::Mechanics.map_param_to_s(d)
53
+ end
54
+ end
55
+
56
+ test "orm obj referencing another orm" do
57
+ d = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
58
+ d2 = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
59
+
60
+ d.dummy_model = d2
61
+ d.save!
62
+ assert_nothing_raised(Exception) do
63
+ Cachely::Mechanics.map_param_to_s(d)
64
+ end
65
+ end
66
+
67
+ test "to_json obj referencing itself" do
68
+ d = DummyClass2.new
69
+ assert_nothing_raised(Exception) do
70
+ Cachely::Mechanics.map_param_to_s(d)
71
+ end
72
+ end
73
+
74
+ test "array obj referencing itself" do
75
+ arr=[]
76
+ arr<<arr
77
+ assert_nothing_raised(Exception) do
78
+ Cachely::Mechanics.map_param_to_s(arr)
79
+ end
80
+ end
81
+
82
+ test "hash obj referencing itself" do
83
+ hash = {}
84
+ hash[:hash] = hash
85
+ assert_nothing_raised(Exception) do
86
+ Cachely::Mechanics.map_param_to_s(hash)
87
+ end
88
+ end
89
+
40
90
  test "class conversion" do
41
91
  str = Cachely::Mechanics.map_param_to_s(DummyClass)
42
92
  reformed = Cachely::Mechanics.map_s_to_param(str)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cachely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &9945220 !ruby/object:Gem::Requirement
16
+ requirement: &21983920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *9945220
24
+ version_requirements: *21983920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hiredis
27
- requirement: &9958720 !ruby/object:Gem::Requirement
27
+ requirement: &21982820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.4.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *9958720
35
+ version_requirements: *21982820
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: em-synchrony
38
- requirement: &9954440 !ruby/object:Gem::Requirement
38
+ requirement: &21997700 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *9954440
46
+ version_requirements: *21997700
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: json
49
- requirement: &9960660 !ruby/object:Gem::Requirement
49
+ requirement: &21996800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *9960660
57
+ version_requirements: *21996800
58
58
  description: Transparently cache the results of methods using redis.
59
59
  email:
60
60
  - jordanmprince@gmail.com