fat_cache 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +11 -10
- data/VERSION +1 -1
- data/lib/fat_cache/shortcuts.rb +17 -0
- data/spec/fat_cache_shortcuts_spec.rb +32 -0
- data/spec/spec_helper.rb +1 -0
- metadata +4 -1
data/README.md
CHANGED
@@ -4,14 +4,13 @@ fat_cache
|
|
4
4
|
Data migration got you down? RAM to spare? Let `fat_cache` do the work for you.
|
5
5
|
`fat_cache` wastes resources for the sake of speed, intentionally!
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
Case Study / Motivation
|
8
|
+
-----------------------
|
9
9
|
|
10
10
|
Say you are importing bank accounts associated with your users from an old
|
11
11
|
system, maybe 10,000 of them.
|
12
12
|
|
13
|
-
Naive Implementation
|
14
|
-
--------------------
|
13
|
+
### Naive Implementation
|
15
14
|
|
16
15
|
You might write code that looks something like this:
|
17
16
|
|
@@ -27,8 +26,7 @@ You might write code that looks something like this:
|
|
27
26
|
|
28
27
|
But this is slow, two queries for each of your 10,000 accounts.
|
29
28
|
|
30
|
-
Refactor One: Fat Query
|
31
|
-
-----------------------
|
29
|
+
### Refactor One: Fat Query
|
32
30
|
|
33
31
|
You can attack the speed problem by loading all your users into memory first.
|
34
32
|
You pay for a fat query up front, but you get a speed boost afterwards.
|
@@ -45,8 +43,7 @@ You pay for a fat query up front, but you get a speed boost afterwards.
|
|
45
43
|
But now instead of spending all your time in the network stack doing queries,
|
46
44
|
you're spinning the CPU doing a linear search through the `all_users` array.
|
47
45
|
|
48
|
-
Refactor Two: Indexed Hash
|
49
|
-
--------------------------
|
46
|
+
### Refactor Two: Indexed Hash
|
50
47
|
|
51
48
|
A similar "pay up front, gain later" strategy can be used on the in-memory data
|
52
49
|
structure by indexing it on the key that we will be searching on.
|
@@ -67,8 +64,7 @@ structure by indexing it on the key that we will be searching on.
|
|
67
64
|
|
68
65
|
Now finding a user for an account is constant time lookup in the hash.
|
69
66
|
|
70
|
-
FatCache makes this strategy simpler
|
71
|
-
------------------------------------
|
67
|
+
### FatCache makes this strategy simpler
|
72
68
|
|
73
69
|
FatCache is a simple abstraction and encapsulation of the strategies used in
|
74
70
|
each refactor. Here is how the code looks:
|
@@ -84,3 +80,8 @@ each refactor. Here is how the code looks:
|
|
84
80
|
And in fact, the call to `index` is optional, since `lookup` will create the
|
85
81
|
index the first time you call it if one doesn't exist, and you're still only
|
86
82
|
paying O(N) once.
|
83
|
+
|
84
|
+
How to use
|
85
|
+
----------
|
86
|
+
|
87
|
+
TODO: basic howto, until then look at the specs!
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe FatCache::Shortcuts do
|
4
|
+
|
5
|
+
describe 'when included in a class' do
|
6
|
+
before do
|
7
|
+
@manatee_class = Class.new
|
8
|
+
@manatee_class.send(:include, FatCache::Shortcuts)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'provides a `cache` method at the class level' do
|
12
|
+
@manatee_class.should respond_to(:cache)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'provides a `cache` method at the instance level' do
|
16
|
+
@manatee_class.new.should respond_to(:cache)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'the class level `cache` method' do
|
20
|
+
it 'yields the FatCache class' do
|
21
|
+
@manatee_class.cache
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'the instance level `cache` method' do
|
26
|
+
it 'yields the FatCache class' do
|
27
|
+
@manatee_class.new.cache
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fat_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- phinze
|
@@ -49,6 +49,8 @@ files:
|
|
49
49
|
- Rakefile
|
50
50
|
- VERSION
|
51
51
|
- lib/fat_cache.rb
|
52
|
+
- lib/fat_cache/shortcuts.rb
|
53
|
+
- spec/fat_cache_shortcuts_spec.rb
|
52
54
|
- spec/fat_cache_spec.rb
|
53
55
|
- spec/spec.opts
|
54
56
|
- spec/spec_helper.rb
|
@@ -82,4 +84,5 @@ specification_version: 3
|
|
82
84
|
summary: A dead simple pure-ruby caching framework for large datasets.
|
83
85
|
test_files:
|
84
86
|
- spec/fat_cache_spec.rb
|
87
|
+
- spec/fat_cache_shortcuts_spec.rb
|
85
88
|
- spec/spec_helper.rb
|