active_memoize 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -10
- data/lib/active_memoize.rb +1 -1
- data/lib/active_memoize/{cache.rb → instance.rb} +20 -30
- data/lib/active_memoize/klass.rb +17 -0
- data/lib/active_memoize/shared.rb +17 -0
- data/lib/active_memoize/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91f66190445f2955c5905be5a7e0f5fce6a9842dbb5cc6b92755927cfe29f0b9
|
4
|
+
data.tar.gz: a1806275ccb0004f7260b5cb38d54f2651299216932eb4db1c52558baa7d6ede
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f1e19d175f9e2ba0ca739737c88d347aadb9c8a7f7b8e00569593452384d8ac9d9ffec1e73975f0065bed362dddfb549ecff411795f8f236785eb20b9d9e4f8
|
7
|
+
data.tar.gz: 06fa2777ea5b110690dd16e55c6f6773a8c3b6aec1fb695c96cfedc9f31809ad812207f0044535eb4514fe456adcf0627270d74915dc65d22c9efdbbb120cf72
|
data/README.md
CHANGED
@@ -6,6 +6,9 @@
|
|
6
6
|
ActiveMemoize provides an API caching and memoizing local
|
7
7
|
expensive calculations including those with parameters.
|
8
8
|
|
9
|
+
The flexible API allows you to memoize results using class
|
10
|
+
or instance level cache.
|
11
|
+
|
9
12
|
## Installation
|
10
13
|
|
11
14
|
Add this line to your application's Gemfile:
|
@@ -24,31 +27,55 @@ Or install it yourself as:
|
|
24
27
|
|
25
28
|
## Table of Contents
|
26
29
|
|
27
|
-
* [
|
30
|
+
* [Klass](#klass)
|
31
|
+
* [Instance](#instance)
|
32
|
+
|
33
|
+
## Klass
|
28
34
|
|
29
|
-
|
35
|
+
Class level memoization is the quickest way to get up and running using your cache, but provides the least amount of flexibility. You can only cache results
|
36
|
+
without access to any information about your cache.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class Movies
|
40
|
+
extend ActiveMemoize::Klass
|
41
|
+
|
42
|
+
def random
|
43
|
+
HTTP.get('http://movies.com/any')
|
44
|
+
end
|
45
|
+
|
46
|
+
memoize :random
|
47
|
+
|
48
|
+
def search(title)
|
49
|
+
HTTP.get("http://movies.com?title=#{title}")
|
50
|
+
end
|
51
|
+
|
52
|
+
memoize :search, as: :find
|
53
|
+
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Instance
|
58
|
+
|
59
|
+
Instance level memoization is a more involved way to setup your cache, but provides the most amount of flexibility. You can access almost all methods
|
60
|
+
in the `instance.rb` file.
|
30
61
|
|
31
62
|
```ruby
|
32
63
|
class Movies
|
33
64
|
|
34
65
|
def cache
|
35
|
-
@cache ||= ActiveMemoize::
|
66
|
+
@cache ||= ActiveMemoize::Instance.new
|
36
67
|
end
|
37
68
|
|
38
69
|
def all
|
39
|
-
cache.memoize(
|
40
|
-
HTTP.get("http://movies.com/all")
|
41
|
-
end
|
70
|
+
cache.memoize { HTTP.get("http://movies.com/all") }
|
42
71
|
end
|
43
72
|
|
44
73
|
def random
|
45
74
|
cache['random'] ||= HTTP.get('http://movies.com/any')
|
46
75
|
end
|
47
76
|
|
48
|
-
def search(title
|
49
|
-
cache.
|
50
|
-
|
51
|
-
cache.memoize do
|
77
|
+
def search(title)
|
78
|
+
cache.memoize(as: :find, refresh: !cache.empty?) do
|
52
79
|
HTTP.get("http://movies.com?title=#{title}")
|
53
80
|
end
|
54
81
|
end
|
data/lib/active_memoize.rb
CHANGED
@@ -1,67 +1,60 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveMemoize
|
4
|
-
class
|
4
|
+
class Instance
|
5
|
+
include ActiveMemoize::Shared
|
5
6
|
|
6
7
|
CALLER_METHOD_REGEX ||= /`([^']*)'/.freeze
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@cache = {}
|
12
|
-
end
|
9
|
+
def initialize; end
|
13
10
|
|
14
11
|
def [](key)
|
15
|
-
|
12
|
+
cache[key]
|
16
13
|
end
|
17
14
|
|
18
15
|
def []=(key, val)
|
19
|
-
|
16
|
+
cache[key] = val
|
20
17
|
end
|
21
18
|
|
22
19
|
def clear
|
23
|
-
|
20
|
+
cache.clear
|
24
21
|
end
|
25
22
|
|
26
23
|
def delete(key)
|
27
|
-
|
24
|
+
cache.delete(key)
|
28
25
|
end
|
29
26
|
|
30
27
|
# :nocov:
|
31
28
|
def each
|
32
|
-
|
29
|
+
cache.each { |key, val| yield(key, val) }
|
33
30
|
end
|
34
31
|
# :nocov:
|
35
32
|
|
36
33
|
def empty?
|
37
|
-
|
34
|
+
cache.empty?
|
38
35
|
end
|
39
36
|
|
40
37
|
alias_method :blank?, :empty?
|
41
38
|
|
42
39
|
def key?(key)
|
43
|
-
|
40
|
+
cache.key?(key)
|
44
41
|
end
|
45
42
|
|
46
43
|
alias_method :hit?, :key?
|
47
44
|
|
48
45
|
def keys
|
49
|
-
|
46
|
+
cache.keys
|
50
47
|
end
|
51
48
|
|
52
49
|
def merge!(hash)
|
53
|
-
|
50
|
+
cache.merge!(hash)
|
54
51
|
end
|
55
52
|
|
56
|
-
def memoize(
|
57
|
-
|
58
|
-
|
59
|
-
method_locals.nil? ? caller_method : "#{caller_method}:#{method_locals}"
|
60
|
-
end
|
53
|
+
def memoize(as: nil, refresh: false, &block)
|
54
|
+
key = key(as || caller_method, caller_locals(block))
|
55
|
+
return cache[key] if !refresh && cache.key?(key)
|
61
56
|
|
62
|
-
|
63
|
-
|
64
|
-
@cache[method_name] = yield(block)
|
57
|
+
cache[key] = yield(block)
|
65
58
|
end
|
66
59
|
|
67
60
|
def present?
|
@@ -71,18 +64,18 @@ module ActiveMemoize
|
|
71
64
|
alias_method :hits?, :present?
|
72
65
|
|
73
66
|
def size
|
74
|
-
|
67
|
+
cache.size
|
75
68
|
end
|
76
69
|
|
77
70
|
def to_hash
|
78
|
-
|
71
|
+
cache
|
79
72
|
end
|
80
73
|
|
81
74
|
alias_method :as_json, :to_hash
|
82
75
|
alias_method :hits, :to_hash
|
83
76
|
|
84
77
|
def values
|
85
|
-
|
78
|
+
cache.values
|
86
79
|
end
|
87
80
|
|
88
81
|
private
|
@@ -90,10 +83,7 @@ module ActiveMemoize
|
|
90
83
|
# rubocop:disable Metrics/LineLength
|
91
84
|
def caller_locals(block)
|
92
85
|
local_vars = block.binding.local_variables
|
93
|
-
local_vars
|
94
|
-
return if local_vars.empty?
|
95
|
-
|
96
|
-
local_vars.join(',')
|
86
|
+
local_vars.flat_map { |name| [name, block.binding.local_variable_get(name)] }
|
97
87
|
end
|
98
88
|
# rubocop:enable Metrics/LineLength
|
99
89
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveMemoize
|
4
|
+
module Klass
|
5
|
+
include ActiveMemoize::Shared
|
6
|
+
|
7
|
+
def memoize(method_name, as: nil)
|
8
|
+
inner_method = instance_method(method_name)
|
9
|
+
|
10
|
+
define_method(method_name) do |*args|
|
11
|
+
key = self.class.key(as || method_name, args)
|
12
|
+
self.class.cache[key] ||= inner_method.bind(self).call(*args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'digest'
|
4
|
+
|
5
|
+
module ActiveMemoize
|
6
|
+
module Shared
|
7
|
+
|
8
|
+
def cache
|
9
|
+
@cache ||= {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def key(method_name, method_args)
|
13
|
+
Digest::SHA1.hexdigest("#{method_name}:#{method_args}")
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_memoize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
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-03-
|
11
|
+
date: 2019-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -118,7 +118,9 @@ files:
|
|
118
118
|
- bin/rake
|
119
119
|
- bin/setup
|
120
120
|
- lib/active_memoize.rb
|
121
|
-
- lib/active_memoize/
|
121
|
+
- lib/active_memoize/instance.rb
|
122
|
+
- lib/active_memoize/klass.rb
|
123
|
+
- lib/active_memoize/shared.rb
|
122
124
|
- lib/active_memoize/version.rb
|
123
125
|
homepage: http://drexed.github.io/active_memoize
|
124
126
|
licenses:
|