cachely 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/README.md +75 -5
- data/cachely.gemspec +6 -3
- data/config/database.yml +1 -0
- data/config/redis.yml +4 -0
- data/lib/cachely.rb +42 -12
- data/lib/cachely/mechanics.rb +218 -0
- data/lib/cachely/version.rb +1 -1
- data/test/models/dummy_class.rb +439 -76
- data/test/models/dummy_model.rb +5 -1
- data/test/unit/base_test.rb +3 -1
- data/test/unit/cachely_test.rb +142 -3
- data/test/unit/conversions_test.rb +68 -0
- data/test/unit/mechanics_test.rb +41 -0
- metadata +84 -33
data/test/models/dummy_model.rb
CHANGED
data/test/unit/base_test.rb
CHANGED
@@ -12,7 +12,9 @@ class BaseTest < ActiveSupport::TestCase
|
|
12
12
|
database_config ||= YAML.load(File.open(File.dirname(__FILE__)+'/../../config/database.yml', 'r'))
|
13
13
|
ActiveRecord::Base.configurations = database_config
|
14
14
|
ActiveRecord::Base.establish_connection("test")
|
15
|
-
|
15
|
+
conf = YAML.load(File.read('./config/redis.yml'))
|
16
|
+
Cachely::Mechanics.connect(conf["test"])
|
17
|
+
Cachely::Mechanics.flush_all_keys
|
16
18
|
DummyModel.destroy_all
|
17
19
|
end
|
18
20
|
end
|
data/test/unit/cachely_test.rb
CHANGED
@@ -1,7 +1,146 @@
|
|
1
1
|
require_relative 'base_test.rb'
|
2
|
-
|
2
|
+
#This test level is for functionality that occurs at the cachely.rb level. Things that interact
|
3
|
+
#With the model.
|
3
4
|
class CachelyTest < BaseTest
|
4
|
-
test
|
5
|
-
|
5
|
+
#later test: having defaults in parameters.
|
6
|
+
test "caches output of instance and class methods primitive" do
|
7
|
+
["instance","class"].each do |preset|
|
8
|
+
[
|
9
|
+
"fixnum",
|
10
|
+
"float",
|
11
|
+
"string",
|
12
|
+
"symbol",
|
13
|
+
"nilclass",
|
14
|
+
"trueclass",
|
15
|
+
"falseclass"
|
16
|
+
].each do |type|
|
17
|
+
obj = (preset == "class" ? DummyClass : DummyClass.new)
|
18
|
+
assert_equal(obj.send(preset+ "_" + type), obj.send(preset + "_" + type))
|
19
|
+
assert_equal(obj.send(preset+ "_" + type+"_one",3), obj.send(preset+ "_" + type + "_one",3))
|
20
|
+
assert_equal(obj.send(preset+ "_" + type+"_two",3,4), obj.send(preset+ "_" + type + "_two",3,4))
|
21
|
+
if(!["nilclass", "trueclass", "falseclass"].include?(type)) #always will be nil/false/true the way we set up tests.
|
22
|
+
assert_not_equal(obj.send(preset+ "_" + type+"_two",3,5), obj.send(preset+ "_" + type + "_two",3,4))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
6
26
|
end
|
27
|
+
|
28
|
+
test "caches output of instance and class methods orm" do
|
29
|
+
["instance","class"].each do |preset|
|
30
|
+
obj = preset == "class" ? DummyClass : DummyClass.new
|
31
|
+
assert_equal(obj.send(preset+ "_orm").id, obj.send(preset + "_orm").id)
|
32
|
+
assert_equal(obj.send(preset+ "_orm_one",3).id, obj.send(preset+ "_orm_one" ,3).id)
|
33
|
+
assert_equal(obj.send(preset+ "_orm_two",3,4).id, obj.send(preset+ "_orm_two",3,4).id)
|
34
|
+
assert_not_equal(obj.send(preset+ "_orm_two",3,5).id, obj.send(preset+ "_orm_two",3,4).id)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test "caches output of instance and class methods hash" do
|
39
|
+
["instance","class"].each do |preset|
|
40
|
+
obj = preset == "class" ? DummyClass : DummyClass.new
|
41
|
+
get_back = obj.send(preset+ "_hash")
|
42
|
+
get_back_2 = obj.send(preset+ "_hash")
|
43
|
+
assert_equal(get_back.keys.size, get_back_2.keys.size)
|
44
|
+
assert_equal(get_back[:foo], get_back_2[:foo])
|
45
|
+
|
46
|
+
get_back = obj.send(preset+ "_hash_one",3)
|
47
|
+
get_back_2 = obj.send(preset+ "_hash_one",3)
|
48
|
+
assert_equal(get_back.keys.size, get_back_2.keys.size)
|
49
|
+
assert_equal(get_back[:foo], get_back_2[:foo])
|
50
|
+
|
51
|
+
get_back = obj.send(preset+ "_hash_two",3,4)
|
52
|
+
get_back_2 = obj.send(preset+ "_hash_two",3,4)
|
53
|
+
assert_equal(get_back.keys.size, get_back_2.keys.size)
|
54
|
+
assert_equal(get_back[:foo], get_back_2[:foo])
|
55
|
+
|
56
|
+
get_back = obj.send(preset+ "_hash_two",3,5)
|
57
|
+
get_back_2 = obj.send(preset+ "_hash_two",3,4)
|
58
|
+
assert_equal(get_back.keys.size, get_back_2.keys.size)
|
59
|
+
assert_not_equal(get_back[:foo], get_back_2[:foo])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
test "caches output of instance and class methods array" do
|
64
|
+
["instance","class"].each do |preset|
|
65
|
+
obj = preset == "class" ? DummyClass : DummyClass.new
|
66
|
+
get_back = obj.send(preset+ "_array")
|
67
|
+
get_back_2 = obj.send(preset+ "_array")
|
68
|
+
assert_equal(0, get_back <=> get_back_2)
|
69
|
+
|
70
|
+
get_back = obj.send(preset+ "_array_one",3)
|
71
|
+
get_back_2 = obj.send(preset+ "_array_one",3)
|
72
|
+
assert_equal(0, get_back <=> get_back_2)
|
73
|
+
|
74
|
+
get_back = obj.send(preset+ "_array_two",3,4)
|
75
|
+
get_back_2 = obj.send(preset+ "_array_two",3,4)
|
76
|
+
assert_equal(0, get_back <=> get_back_2)
|
77
|
+
|
78
|
+
get_back = obj.send(preset+ "_array_two",3,5)
|
79
|
+
get_back_2 = obj.send(preset+ "_array_two",3,4)
|
80
|
+
assert_not_equal(0, get_back <=> get_back_2)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
test "caches output of instance and class methods to_json" do
|
85
|
+
["instance"].each do |preset|
|
86
|
+
obj = preset == "class" ? DummyClass : DummyClass.new
|
87
|
+
get_back = obj.send(preset+ "_to_json")
|
88
|
+
get_back_2 = obj.send(preset+ "_to_json")
|
89
|
+
assert_equal(get_back.random_no, get_back_2.random_no)
|
90
|
+
|
91
|
+
get_back = obj.send(preset+ "_to_json_one",3)
|
92
|
+
get_back_2 = obj.send(preset+ "_to_json_one",3)
|
93
|
+
assert_equal(get_back.random_no, get_back_2.random_no)
|
94
|
+
|
95
|
+
get_back = obj.send(preset+ "_to_json_two",3,4)
|
96
|
+
get_back_2 = obj.send(preset+ "_to_json_two",3,4)
|
97
|
+
assert_equal(get_back.random_no, get_back_2.random_no)
|
98
|
+
|
99
|
+
get_back = obj.send(preset+ "_to_json_two",3,5)
|
100
|
+
get_back_2 = obj.send(preset+ "_to_json_two",3,4)
|
101
|
+
assert_equal(get_back.random_no, get_back_2.random_no)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
test "updated object in argument triggers cache refill" do
|
106
|
+
#an updated object means logic inside might react differently. SO we need to not get it.
|
107
|
+
["instance","class"].each do |preset|
|
108
|
+
obj = preset == "class" ? DummyClass : DummyClass.new
|
109
|
+
d = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
|
110
|
+
get_back = obj.send(preset+"_fixnum_one", d)
|
111
|
+
d.attr_1 +=1
|
112
|
+
old_time = d.updated_at
|
113
|
+
d.save!
|
114
|
+
assert_not_equal(old_time, d.updated_at) #just making sure it overwrote it correctly.
|
115
|
+
|
116
|
+
get_back_2 = obj.send(preset+"_fixnum_one", d)
|
117
|
+
assert_not_equal(get_back, get_back_2) #because orm changed.
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
test "instance method called on two diff objects knows its different" do
|
122
|
+
obj_1 = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
|
123
|
+
obj_2 = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
|
124
|
+
assert_not_equal(obj_1.instance_fixnum, obj_2.instance_fixnum)
|
125
|
+
end
|
126
|
+
|
127
|
+
test "class method gets cached while normal one does not" do
|
128
|
+
obj = DummyClass.new
|
129
|
+
klazz = DummyClass
|
130
|
+
assert_not_equal(obj.instance_only_cache, klazz.instance_only_cache)
|
131
|
+
end
|
132
|
+
|
133
|
+
test "diff between class and obj method" do
|
134
|
+
obj = DummyClass.new
|
135
|
+
klazz = DummyClass
|
136
|
+
assert_not_equal(obj.class_diff, klazz.class_diff)
|
137
|
+
end
|
138
|
+
|
139
|
+
test "cache expires" do
|
140
|
+
obj = DummyClass.new
|
141
|
+
old = obj.cache_expiry
|
142
|
+
sleep(3)
|
143
|
+
assert_not_equal(old, obj.cache_expiry)
|
144
|
+
end
|
145
|
+
|
7
146
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative 'base_test.rb'
|
2
|
+
#Only do conversions testing here. Test recursivity.
|
3
|
+
class ConversionsTest < BaseTest
|
4
|
+
test "hash conversion" do
|
5
|
+
str = Cachely::Mechanics.map_param_to_s({:foo => "bar", :baz => { "nada" => :pink }})
|
6
|
+
reformed = Cachely::Mechanics.map_s_to_param(str)
|
7
|
+
assert_equal("bar", reformed[:foo])
|
8
|
+
assert(reformed[:baz].is_a?(Hash))
|
9
|
+
internal = reformed[:baz]
|
10
|
+
assert_equal(:pink, internal["nada"])
|
11
|
+
assert_equal(2, reformed.keys.size)
|
12
|
+
assert_equal(1, internal.keys.size)
|
13
|
+
end
|
14
|
+
|
15
|
+
test "hash conversion with an array subkey" do
|
16
|
+
str = Cachely::Mechanics.map_param_to_s({
|
17
|
+
:method => :foo,
|
18
|
+
:args => [3,4]
|
19
|
+
})
|
20
|
+
assert_equal("{\"Symbol:method\":\"Symbol:foo\",\"Symbol:args\":\"[\\\"Fixnum:3\\\",\\\"Fixnum:4\\\"]\"}",str)
|
21
|
+
reformed = Cachely::Mechanics.map_s_to_param(str)
|
22
|
+
assert_equal(2, reformed.keys.size)
|
23
|
+
assert_equal(:foo, reformed[:method])
|
24
|
+
assert_equal(0, [3,4] <=> reformed[:args])
|
25
|
+
end
|
26
|
+
|
27
|
+
test "array conversion" do
|
28
|
+
str = Cachely::Mechanics.map_param_to_s([1,2,["3","4"]])
|
29
|
+
reformed = Cachely::Mechanics.map_s_to_param(str)
|
30
|
+
assert_equal(0,[1,2,["3", "4"]] <=> reformed)
|
31
|
+
end
|
32
|
+
|
33
|
+
test "to_json obj conversion" do
|
34
|
+
d = DummyClass.new
|
35
|
+
d.random_no = rand(500)
|
36
|
+
str = Cachely::Mechanics.map_param_to_s(d)
|
37
|
+
reformed = Cachely::Mechanics.map_s_to_param(str)
|
38
|
+
assert_equal(d.random_no, reformed.random_no)
|
39
|
+
end
|
40
|
+
|
41
|
+
test "orm conversion" do
|
42
|
+
d = DummyModel.create!(:attr_1 => rand(500), :attr_2 => rand(500))
|
43
|
+
str = Cachely::Mechanics.map_param_to_s(d)
|
44
|
+
reformed = Cachely::Mechanics.map_s_to_param(str)
|
45
|
+
assert_equal(d.attr_1, reformed.attr_1)
|
46
|
+
assert_equal(d.attr_2, reformed.attr_2)
|
47
|
+
assert_equal(d.id, reformed.id)
|
48
|
+
end
|
49
|
+
|
50
|
+
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"))
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'base_test.rb'
|
2
|
+
|
3
|
+
#This is for mechanics.rb test that don't have to do with conversions, which are complex enough to
|
4
|
+
#have their own file. Test getting/storing, stuff like that.
|
5
|
+
class MechanicsTest < BaseTest
|
6
|
+
test "get/store array" do
|
7
|
+
Cachely::Mechanics.store(DummyClass,:foo, [1,2,3], nil, [3,4])
|
8
|
+
assert_equal(0, [1,2,3] <=> Cachely::Mechanics.get(DummyClass,:foo, [3,4]).first)
|
9
|
+
end
|
10
|
+
|
11
|
+
test "get/store hash" do
|
12
|
+
Cachely::Mechanics.store(DummyClass,:foo, {:bar => "baz"}, nil, [3,4])
|
13
|
+
respawned = Cachely::Mechanics.get(DummyClass,:foo, [3,4]).first
|
14
|
+
assert_equal(1, respawned.keys.size)
|
15
|
+
assert_equal("baz", respawned[:bar])
|
16
|
+
end
|
17
|
+
|
18
|
+
test "get/store orm" do
|
19
|
+
d = DummyModel.create!(:attr_1 => 1, :attr_2 => 3)
|
20
|
+
Cachely::Mechanics.store(DummyClass,:foo, d,nil,[3,4])
|
21
|
+
respawned = Cachely::Mechanics.get(DummyClass,:foo, [3,4]).first
|
22
|
+
assert_equal(d.id, respawned.id)
|
23
|
+
assert_equal(d.attr_1, respawned.attr_1)
|
24
|
+
assert_equal(d.attr_2, respawned.attr_2)
|
25
|
+
end
|
26
|
+
|
27
|
+
test "get/store to_json" do
|
28
|
+
d = DummyClass.new
|
29
|
+
Cachely::Mechanics.store(DummyClass,:foo, d,nil, [3,4])
|
30
|
+
respawned = Cachely::Mechanics.get(DummyClass,:foo, [3,4]).first
|
31
|
+
assert_equal(d.random_no, respawned.random_no)
|
32
|
+
end
|
33
|
+
|
34
|
+
test "get/store primitive" do
|
35
|
+
[3, "3", true, false, nil, 3.5, :stuff].each do |result|
|
36
|
+
Cachely::Mechanics.store(DummyClass,:foo, result, nil, [3,4])
|
37
|
+
respawned = Cachely::Mechanics.get(DummyClass,:foo, [3,4]).first
|
38
|
+
assert_equal(result, respawned)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,34 +1,71 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: cachely
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
5
4
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
|
13
|
+
date: 2013-03-04 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: redis
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
18
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.0.1
|
22
23
|
type: :runtime
|
23
24
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
|
26
|
-
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: hiredis
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.4.5
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: em-synchrony
|
39
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: json
|
50
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
type: :runtime
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *id004
|
59
|
+
description: Transparently cache the results of methods using redis.
|
60
|
+
email:
|
27
61
|
- jordanmprince@gmail.com
|
28
62
|
executables: []
|
63
|
+
|
29
64
|
extensions: []
|
65
|
+
|
30
66
|
extra_rdoc_files: []
|
31
|
-
|
67
|
+
|
68
|
+
files:
|
32
69
|
- .gitignore
|
33
70
|
- Gemfile
|
34
71
|
- LICENSE
|
@@ -36,40 +73,54 @@ files:
|
|
36
73
|
- Rakefile
|
37
74
|
- cachely.gemspec
|
38
75
|
- config/database.yml
|
76
|
+
- config/redis.yml
|
39
77
|
- db/migrate/001_create_dummy_model.rb
|
40
78
|
- lib/cachely.rb
|
79
|
+
- lib/cachely/mechanics.rb
|
41
80
|
- lib/cachely/version.rb
|
42
81
|
- test/models/dummy_class.rb
|
43
82
|
- test/models/dummy_model.rb
|
44
83
|
- test/unit/base_test.rb
|
45
84
|
- test/unit/cachely_test.rb
|
46
|
-
|
85
|
+
- test/unit/conversions_test.rb
|
86
|
+
- test/unit/mechanics_test.rb
|
87
|
+
homepage: ""
|
47
88
|
licenses: []
|
89
|
+
|
48
90
|
post_install_message:
|
49
91
|
rdoc_options: []
|
50
|
-
|
92
|
+
|
93
|
+
require_paths:
|
51
94
|
- lib
|
52
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
96
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
|
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
|
59
105
|
none: false
|
60
|
-
requirements:
|
61
|
-
- -
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: -39538212363365102
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
64
113
|
requirements: []
|
114
|
+
|
65
115
|
rubyforge_project:
|
66
|
-
rubygems_version: 1.8.
|
116
|
+
rubygems_version: 1.8.24
|
67
117
|
signing_key:
|
68
118
|
specification_version: 3
|
69
|
-
summary:
|
70
|
-
|
71
|
-
test_files:
|
119
|
+
summary: Transparently cache the results of methods using redis.
|
120
|
+
test_files:
|
72
121
|
- test/models/dummy_class.rb
|
73
122
|
- test/models/dummy_model.rb
|
74
123
|
- test/unit/base_test.rb
|
75
124
|
- test/unit/cachely_test.rb
|
125
|
+
- test/unit/conversions_test.rb
|
126
|
+
- test/unit/mechanics_test.rb
|