lite-memoize 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a2e51485362684ce1520b7eca9484106b5b83c9bc6336b2215dcffa6d56cfed
4
- data.tar.gz: 43eb2cd5cb4330f12a503f3b66594b6a0c076bb1a0a991a0735bef1c11fd3cb6
3
+ metadata.gz: 764d8311718bd18bada8e3f63104f1e81f9e300a995dbac0f61a41ded9706cd0
4
+ data.tar.gz: 9d6abb3e67c30129a3e9401fbdb1f213238991ccf9a636a0f889260081a29dd2
5
5
  SHA512:
6
- metadata.gz: b082b4804dbcedb965b006b0077375195316720d7397b5ed0140b5ae0de8847706d3503e8ca9f33f7c6ce064a0d79c2ff6f25a57a06e3a458ee2506e0f623436
7
- data.tar.gz: 0dc03f302a930483a8ca51f114e80b6ed36506153bf1840f0ddcfb221404acd1f307dc77ae1dcf8f4a0a60970513d382954180cb70e49928d7727a41dcdcf48c
6
+ metadata.gz: c34cde89e7350fb9d95d111924b90045ec0927d4f2f997623dba9ac28d794a06be10136cdc71e1f621f19f763af73a3688a7c983c7f651bfa10ee3c610266589
7
+ data.tar.gz: 4491d5d262bb90fcbc15a951f99bfeefc7ffa7631a4326f5037de0c5ca2016fa8f9b671f8575f16d5aea23c0ecda3d0a60d56e82d035ca0624c37cc13bc938ed
data/CHANGELOG.md CHANGED
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.0.2] - 2019-08-07
10
+ ### Added
11
+ - Added variable base memoization
12
+ ### Changed
13
+ - Renamed mixin to table
14
+ - Changed caller_key method to private
15
+
9
16
  ## [1.0.1] - 2019-07-18
10
17
  ### Added
11
18
  - Added alias memoization
@@ -13,7 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
13
20
  ### Changed
14
21
  - Improved speed of key generation and lookup for instance
15
22
  - Changed refresh arg to reload
16
- - Renamed shared to mixin
23
+ - Renamed shared to table
17
24
 
18
25
  ## [1.0.0] - 2019-06-24
19
26
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-memoize (1.0.1)
4
+ lite-memoize (1.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/lite-memoize.svg)](http://badge.fury.io/rb/lite-memoize)
4
4
  [![Build Status](https://travis-ci.org/drexed/lite-memoize.svg?branch=master)](https://travis-ci.org/drexed/lite-memoize)
5
5
 
6
- Lite::Memoize provides an API for caching and memoizing locally expensive calculations including those with parameters. The flexible API allows you to memoize results using alias, class, instance, or mixin level cache.
6
+ Lite::Memoize provides an API for caching and memoizing locally expensive calculations including those with parameters. The flexible API allows you to memoize results using alias, class, instance, table, or variable based cache.
7
7
 
8
8
  **NOTE:** If you are coming from `ActiveMemoize`, please read the [port](#port) section.
9
9
 
@@ -28,13 +28,14 @@ Or install it yourself as:
28
28
  * [Alias](#alias)
29
29
  * [Klass](#klass)
30
30
  * [Instance](#instance)
31
- * [Mixin](#mixin)
31
+ * [Table](#table)
32
+ * [Variable](#variable)
32
33
  * [Benchmarks](#benchmarks)
33
34
  * [Port](#port)
34
35
 
35
36
  ## Alias
36
37
 
37
- Alias level memoization is the fastest of the available methods, and provides a decent level
38
+ Alias based memoization is the fastest of the available methods, and provides a decent level
38
39
  of control. It's the only one that can also be used to memoize class level methods. Method
39
40
  arguments are automatically watched to cache dynamic values.
40
41
 
@@ -58,7 +59,9 @@ class Movies
58
59
  HTTP.get('http://movies.com/any')
59
60
  end
60
61
 
62
+ # NOTE: memoize must be before alias
61
63
  memoize :random
64
+ alias rando random
62
65
 
63
66
  def search(title)
64
67
  HTTP.get("http://movies.com?title=#{title}")
@@ -78,7 +81,7 @@ Movies.clear_cache #=> New value
78
81
 
79
82
  ## Klass
80
83
 
81
- Class level memoization is the quickest way to get up without polluting your class with new methods.
84
+ Class based memoization is the quickest way to get up without polluting your class with new methods.
82
85
  It's perfect for short lived or non-altering items like `activerecord` objects.
83
86
 
84
87
  You can only cache results without access to any information about the `store`.
@@ -91,7 +94,9 @@ class Movies
91
94
  HTTP.get('http://movies.com/any')
92
95
  end
93
96
 
97
+ # NOTE: memoize must be before alias
94
98
  memoize :random
99
+ alias rando random
95
100
 
96
101
  def search(title)
97
102
  HTTP.get("http://movies.com?title=#{title}")
@@ -104,11 +109,11 @@ end
104
109
 
105
110
  ## Instance
106
111
 
107
- Instance level memoization is the slowest of the available methods, but it provides
112
+ Instance based memoization is the slowest of the available methods, but it provides
108
113
  the most amount of flexibility and control. It's very useful for creating services or things
109
- where control is paramount like clearing the cache or dumping it to JSON. Please read the spec
110
- suite to see all available actions. Method arguments are automatically watched to cache dynamic
111
- values.
114
+ where control is paramount like clearing the cache or dumping it to JSON. Method arguments
115
+ are automatically watched to cache dynamic values. Please read the spec suite to see all
116
+ available actions.
112
117
 
113
118
  You can access almost all methods in the `instance.rb` file.
114
119
 
@@ -128,6 +133,8 @@ class Movies
128
133
  cache['random'] ||= HTTP.get("http://movies.com/any?type=#{type}")
129
134
  end
130
135
 
136
+ alias rando random
137
+
131
138
  # NOTE: Arguments in the memoize method are optional
132
139
  def search(title)
133
140
  cache.memoize(as: :find, args: [title], reload: !cache.empty?) do
@@ -138,21 +145,47 @@ class Movies
138
145
  end
139
146
  ```
140
147
 
141
- ## Mixin
148
+ ## Variable
149
+
150
+ Variable based memoization is lean but pollute the class with variables.
151
+
152
+ ```ruby
153
+ class Movies
154
+ include Lite::Memoize::Variable
155
+
156
+ def all
157
+ memoize(:all) { HTTP.get("http://movies.com/all") }
158
+ end
159
+
160
+ alias full all
161
+
162
+ # NOTE: Arguments in the memoize method are optional with the exception of method name
163
+ def search(title)
164
+ memoize(:find, args: [title], reload: false) do
165
+ HTTP.get("http://movies.com?title=#{title}")
166
+ end
167
+ end
168
+
169
+ end
170
+ ```
171
+
172
+ ## Table
142
173
 
143
- Mixin level memoization is the leanest of the available methods, and provides a decent level
174
+ Table based memoization is the leanest of the available methods, and provides a decent level
144
175
  of control. Useful when you want to keep your class light weight.
145
176
 
146
177
  You can access all methods to the `Hash` class.
147
178
 
148
179
  ```ruby
149
180
  class Movies
150
- include Lite::Memoize::Mixin
181
+ include Lite::Memoize::Table
151
182
 
152
183
  def all
153
184
  memoize(:all) { HTTP.get("http://movies.com/all") }
154
185
  end
155
186
 
187
+ alias full all
188
+
156
189
  # NOTE: Arguments in the memoize method are optional with the exception of method name
157
190
  def search(title)
158
191
  memoize(:find, args: [title], reload: false) do
@@ -165,9 +198,9 @@ end
165
198
 
166
199
  ## Benchmarks
167
200
 
168
- The classes ranked from fastest to slowest are `Alias`, `Mixin`, `Klass`, and `Instance`.
201
+ The classes ranked from fastest to slowest are `Alias`, `Table`, `Klass`, `Variable`, and `Instance`.
169
202
 
170
- View all how it compares to other libs by running the [benchmarks](https://github.com/drexed/lite-statistics/tree/master/benchmarks).
203
+ View how each compares to other libs by running the [benchmarks](https://github.com/drexed/lite-statistics/tree/master/benchmarks).
171
204
 
172
205
  ## Port
173
206
 
data/benchmarks/base.rb CHANGED
@@ -41,9 +41,19 @@ class LiteMemoizeKlassCache
41
41
 
42
42
  end
43
43
 
44
- class LiteMemoizeMixinCache
44
+ class LiteMemoizeTableCache
45
45
 
46
- include Lite::Memoize::Mixin
46
+ include Lite::Memoize::Table
47
+
48
+ def randomize
49
+ memoize(:randomize) { rand(1..99) }
50
+ end
51
+
52
+ end
53
+
54
+ class LiteMemoizeVariableCache
55
+
56
+ include Lite::Memoize::Variable
47
57
 
48
58
  def randomize
49
59
  memoize(:randomize) { rand(1..99) }
data/benchmarks/memery.rb CHANGED
@@ -15,8 +15,9 @@ end
15
15
 
16
16
  klass_a = LiteMemoizeInstanceCache.new
17
17
  klass_b = LiteMemoizeKlassCache.new
18
- klass_c = LiteMemoizeMixinCache.new
18
+ klass_c = LiteMemoizeTableCache.new
19
19
  klass_d = LiteMemoizeAliasCache.new
20
+ klass_e = LiteMemoizeVariableCache.new
20
21
  klass_z = MemeryCache.new
21
22
 
22
23
  Benchmark.ips do |x|
@@ -28,7 +29,7 @@ Benchmark.ips do |x|
28
29
  klass_b.randomize
29
30
  end
30
31
 
31
- x.report('LM.mixin') do
32
+ x.report('LM.table') do
32
33
  klass_c.randomize
33
34
  end
34
35
 
@@ -36,6 +37,10 @@ Benchmark.ips do |x|
36
37
  klass_d.randomize
37
38
  end
38
39
 
40
+ x.report('LM.variable') do
41
+ klass_e.randomize
42
+ end
43
+
39
44
  x.report('Memery') do
40
45
  klass_z.randomize
41
46
  end
data/benchmarks/memist.rb CHANGED
@@ -15,8 +15,9 @@ end
15
15
 
16
16
  klass_a = LiteMemoizeInstanceCache.new
17
17
  klass_b = LiteMemoizeKlassCache.new
18
- klass_c = LiteMemoizeMixinCache.new
18
+ klass_c = LiteMemoizeTableCache.new
19
19
  klass_d = LiteMemoizeAliasCache.new
20
+ klass_e = LiteMemoizeVariableCache.new
20
21
  klass_z = MemistCache.new
21
22
 
22
23
  Benchmark.ips do |x|
@@ -28,7 +29,7 @@ Benchmark.ips do |x|
28
29
  klass_b.randomize
29
30
  end
30
31
 
31
- x.report('LM.mixin') do
32
+ x.report('LM.table') do
32
33
  klass_c.randomize
33
34
  end
34
35
 
@@ -36,6 +37,10 @@ Benchmark.ips do |x|
36
37
  klass_d.randomize
37
38
  end
38
39
 
40
+ x.report('LM.variable') do
41
+ klass_e.randomize
42
+ end
43
+
39
44
  x.report('Memist') do
40
45
  klass_z.randomize
41
46
  end
@@ -15,8 +15,9 @@ end
15
15
 
16
16
  klass_a = LiteMemoizeInstanceCache.new
17
17
  klass_b = LiteMemoizeKlassCache.new
18
- klass_c = LiteMemoizeMixinCache.new
18
+ klass_c = LiteMemoizeTableCache.new
19
19
  klass_d = LiteMemoizeAliasCache.new
20
+ klass_e = LiteMemoizeVariableCache.new
20
21
  klass_z = MemoitCache.new
21
22
 
22
23
  Benchmark.ips do |x|
@@ -28,7 +29,7 @@ Benchmark.ips do |x|
28
29
  klass_b.randomize
29
30
  end
30
31
 
31
- x.report('LM.mixin') do
32
+ x.report('LM.table') do
32
33
  klass_c.randomize
33
34
  end
34
35
 
@@ -40,5 +41,9 @@ Benchmark.ips do |x|
40
41
  klass_z.randomize
41
42
  end
42
43
 
44
+ x.report('LM.variable') do
45
+ klass_e.randomize
46
+ end
47
+
43
48
  x.compare!
44
49
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = MemoistCache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoist') do
42
47
  klass_z.randomize
43
48
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = Memoist2Cache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoist2') do
42
47
  klass_z.randomize
43
48
  end
data/benchmarks/memoit.rb CHANGED
@@ -13,8 +13,9 @@ end
13
13
 
14
14
  klass_a = LiteMemoizeInstanceCache.new
15
15
  klass_b = LiteMemoizeKlassCache.new
16
- klass_c = LiteMemoizeMixinCache.new
16
+ klass_c = LiteMemoizeTableCache.new
17
17
  klass_d = LiteMemoizeAliasCache.new
18
+ klass_e = LiteMemoizeVariableCache.new
18
19
  klass_z = MemoitCache.new
19
20
 
20
21
  Benchmark.ips do |x|
@@ -26,7 +27,7 @@ Benchmark.ips do |x|
26
27
  klass_b.randomize
27
28
  end
28
29
 
29
- x.report('LM.mixin') do
30
+ x.report('LM.table') do
30
31
  klass_c.randomize
31
32
  end
32
33
 
@@ -34,6 +35,10 @@ Benchmark.ips do |x|
34
35
  klass_d.randomize
35
36
  end
36
37
 
38
+ x.report('LM.variable') do
39
+ klass_e.randomize
40
+ end
41
+
37
42
  x.report('Memoit') do
38
43
  klass_z.randomize
39
44
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = MemoizableCache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoizable') do
42
47
  klass_z.randomize
43
48
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = MemoizerCache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoizer') do
42
47
  klass_z.randomize
43
48
  end
data/lib/lite/memoize.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- %w[version alias mixin instance klass].each do |name|
3
+ %w[version alias table instance klass variable].each do |name|
4
4
  require "lite/memoize/#{name}"
5
5
  end
@@ -4,7 +4,7 @@ module Lite
4
4
  module Memoize
5
5
  class Instance
6
6
 
7
- include Lite::Memoize::Mixin
7
+ include Lite::Memoize::Table
8
8
 
9
9
  def initialize; end
10
10
 
@@ -4,7 +4,7 @@ module Lite
4
4
  module Memoize
5
5
  module Klass
6
6
 
7
- include Lite::Memoize::Mixin
7
+ include Lite::Memoize::Table
8
8
 
9
9
  def memoize(method_name, as: nil)
10
10
  inner_method = instance_method(method_name)
@@ -16,6 +16,10 @@ module Lite
16
16
  end
17
17
  end
18
18
 
19
+ # rubocop:disable Style/AccessModifierDeclarations
20
+ public :caller_key
21
+ # rubocop:enable Style/AccessModifierDeclarations
22
+
19
23
  end
20
24
  end
21
25
  end
@@ -2,22 +2,12 @@
2
2
 
3
3
  module Lite
4
4
  module Memoize
5
- module Mixin
5
+ module Table
6
6
 
7
7
  def store
8
8
  @_memoized_methods ||= {}
9
9
  end
10
10
 
11
- def caller_key(block, as: nil)
12
- name = as ? [as] : block.source_location
13
- return name.concat(block) if block.is_a?(Array)
14
-
15
- block.binding.local_variables.each_with_object(name) do |local_name, array|
16
- array << local_name
17
- array << block.binding.local_variable_get(local_name)
18
- end
19
- end
20
-
21
11
  def memoize(method_name, args: nil, reload: false)
22
12
  key = "#{method_name}:#{args}"
23
13
 
@@ -28,6 +18,18 @@ module Lite
28
18
  end
29
19
  end
30
20
 
21
+ private
22
+
23
+ def caller_key(block, as: nil)
24
+ name = as ? [as] : block.source_location
25
+ return name.concat(block) if block.is_a?(Array)
26
+
27
+ block.binding.local_variables.each_with_object(name) do |local_name, array|
28
+ array << local_name
29
+ array << block.binding.local_variable_get(local_name)
30
+ end
31
+ end
32
+
31
33
  end
32
34
  end
33
35
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Memoize
5
+ module Variable
6
+
7
+ def memoize(method_name, args: nil, reload: false)
8
+ key = "#{method_name}#{args}"
9
+ var = "@#{key.gsub(/\W/, '') || key}"
10
+ return instance_variable_get(var) if !reload && instance_variable_defined?(var)
11
+
12
+ instance_variable_set(var, yield)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Memoize
5
5
 
6
- VERSION ||= '1.0.1'
6
+ VERSION ||= '1.0.2'
7
7
 
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-memoize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-18 00:00:00.000000000 Z
11
+ date: 2019-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,7 +143,8 @@ files:
143
143
  - lib/lite/memoize/alias.rb
144
144
  - lib/lite/memoize/instance.rb
145
145
  - lib/lite/memoize/klass.rb
146
- - lib/lite/memoize/mixin.rb
146
+ - lib/lite/memoize/table.rb
147
+ - lib/lite/memoize/variable.rb
147
148
  - lib/lite/memoize/version.rb
148
149
  - lite-memoize.gemspec
149
150
  homepage: http://drexed.github.io/lite-memoize