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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1385c5e7ddc821d41bbf56e62bd47280c920fce6ff108feadbd2d34aeba38194
4
- data.tar.gz: 7ead9b7f7673973a62f092d427dc0635b8c685dfbcc032a615ce45a6a551a72f
3
+ metadata.gz: 91f66190445f2955c5905be5a7e0f5fce6a9842dbb5cc6b92755927cfe29f0b9
4
+ data.tar.gz: a1806275ccb0004f7260b5cb38d54f2651299216932eb4db1c52558baa7d6ede
5
5
  SHA512:
6
- metadata.gz: '09e6ee6a1cdb35f3238514672b60a3a8c734fc7bbcac020e8d32f9c35a080fb5262e2df5112231b7e7d8daf8bab01e9aba3db27c90fc0bcd74d91bc83216ab1e'
7
- data.tar.gz: 6e392c12c109d10a7819ae8a87acc18b2093a82c30c14d2202da29a055fa5ef8123499c6fdb0f82b8604093f175cb98f4d6bcb6812a32b12a8297f580af440aa
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
- * [Usage](#usage)
30
+ * [Klass](#klass)
31
+ * [Instance](#instance)
32
+
33
+ ## Klass
28
34
 
29
- ## Usage
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::Cache.new
66
+ @cache ||= ActiveMemoize::Instance.new
36
67
  end
37
68
 
38
69
  def all
39
- cache.memoize('custom_cache_key') do
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, force = false)
49
- cache.clear if force
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- %w[version cache].each do |file_name|
3
+ %w[version shared klass instance].each do |file_name|
4
4
  require "active_memoize/#{file_name}"
5
5
  end
@@ -1,67 +1,60 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveMemoize
4
- class Cache
4
+ class Instance
5
+ include ActiveMemoize::Shared
5
6
 
6
7
  CALLER_METHOD_REGEX ||= /`([^']*)'/.freeze
7
8
 
8
- attr_reader :cache
9
-
10
- def initialize
11
- @cache = {}
12
- end
9
+ def initialize; end
13
10
 
14
11
  def [](key)
15
- @cache[key]
12
+ cache[key]
16
13
  end
17
14
 
18
15
  def []=(key, val)
19
- @cache[key] = val
16
+ cache[key] = val
20
17
  end
21
18
 
22
19
  def clear
23
- @cache.clear
20
+ cache.clear
24
21
  end
25
22
 
26
23
  def delete(key)
27
- @cache.delete(key)
24
+ cache.delete(key)
28
25
  end
29
26
 
30
27
  # :nocov:
31
28
  def each
32
- @cache.each { |key, val| yield(key, val) }
29
+ cache.each { |key, val| yield(key, val) }
33
30
  end
34
31
  # :nocov:
35
32
 
36
33
  def empty?
37
- @cache.empty?
34
+ cache.empty?
38
35
  end
39
36
 
40
37
  alias_method :blank?, :empty?
41
38
 
42
39
  def key?(key)
43
- @cache.key?(key)
40
+ cache.key?(key)
44
41
  end
45
42
 
46
43
  alias_method :hit?, :key?
47
44
 
48
45
  def keys
49
- @cache.keys
46
+ cache.keys
50
47
  end
51
48
 
52
49
  def merge!(hash)
53
- @cache.merge!(hash)
50
+ cache.merge!(hash)
54
51
  end
55
52
 
56
- def memoize(method_name = nil, &block)
57
- method_locals = caller_locals(block)
58
- method_name ||= begin
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
- return @cache[method_name] if @cache.key?(method_name)
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
- @cache.size
67
+ cache.size
75
68
  end
76
69
 
77
70
  def to_hash
78
- @cache
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
- @cache.values
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 = local_vars.flat_map { |name| [name, block.binding.local_variable_get(name)].join('=') }
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveMemoize
4
- VERSION ||= '1.1.1'
4
+ VERSION ||= '2.0.0'
5
5
  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: 1.1.1
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-18 00:00:00.000000000 Z
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/cache.rb
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: