cache_keeper 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/cache_keeper/caching.rb +14 -12
- data/lib/cache_keeper/configuration.rb +15 -13
- data/lib/cache_keeper/engine.rb +23 -14
- data/lib/cache_keeper/manager.rb +29 -27
- data/lib/cache_keeper/replace_method.rb +36 -34
- data/lib/cache_keeper/version.rb +1 -1
- data/lib/cache_keeper.rb +7 -5
- data/test/manager_test.rb +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c67412b5f220a3adf468e0dbb26e7ebe170e2585806e5b1573f8ebba0c9e464f
|
4
|
+
data.tar.gz: 86ae3889c37ab555a6c7fd517cc6782de5cc3d3b11c793491f81a97f23a2b3ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d500ec9f67aaa5d61d3ffed9246ca9ac30fe08a76130b9687ae015859f22af5ac27881c95794310428f315e23d870f59e42d4fbd433f3c66ae2d469dd0480577
|
7
|
+
data.tar.gz: 1468c273f8f91f0761d9bc0b0ca31641732a3d424da4a85c247e776ff8eb68d5a9c162a02be3cff51f832cf55ac3d35b6ecf5193220c9f1174faff26e62affab
|
data/Gemfile.lock
CHANGED
data/lib/cache_keeper/caching.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
|
-
module CacheKeeper
|
2
|
-
|
1
|
+
module CacheKeeper
|
2
|
+
module Caching
|
3
|
+
extend ActiveSupport::Concern
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
included do
|
6
|
+
def self.caches(*method_names, **options)
|
7
|
+
method_names.each do |method_name|
|
8
|
+
CacheKeeper.manager.handle self, method_name, options
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
# If the method is already defined, we need to hook it
|
11
|
+
method_added method_name
|
12
|
+
end
|
11
13
|
end
|
12
|
-
end
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
def self.method_added(method_name)
|
16
|
+
super
|
16
17
|
|
17
|
-
|
18
|
+
CacheKeeper.manager.activate_if_handling self, method_name
|
19
|
+
end
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
@@ -1,20 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module CacheKeeper
|
2
|
+
class Configuration
|
3
|
+
DEFAULT_MUST_REVALIDATE = false
|
4
|
+
DEFAULT_QUEUES = {}
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
def must_revalidate
|
7
|
+
return rails_config[:must_revalidate] unless rails_config[:must_revalidate].nil?
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
DEFAULT_MUST_REVALIDATE
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def queues
|
13
|
+
rails_config[:queues] || DEFAULT_QUEUES
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
+
private
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
def rails_config
|
19
|
+
Rails.application.config.cache_keeper
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
data/lib/cache_keeper/engine.rb
CHANGED
@@ -1,22 +1,31 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module CacheKeeper
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace CacheKeeper
|
3
4
|
|
4
|
-
|
5
|
-
|
5
|
+
config.cache_keeper = ActiveSupport::OrderedOptions.new
|
6
|
+
config.cache_keeper.queues = ActiveSupport::InheritableOptions.new
|
6
7
|
|
7
|
-
|
8
|
-
config.
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
config.eager_load_namespaces << CacheKeeper
|
9
|
+
config.autoload_once_paths = %W(
|
10
|
+
#{root}/app/jobs
|
11
|
+
#{root}/app/models
|
12
|
+
#{root}/app/serializers
|
13
|
+
)
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
15
|
+
initializer "cache_keeper.active_job_serializer" do |app|
|
16
|
+
config.to_prepare do
|
17
|
+
Rails.application.config.active_job.custom_serializers << CacheKeeper::CachedMethodSerializer
|
18
|
+
end
|
16
19
|
end
|
17
20
|
|
18
|
-
|
19
|
-
|
21
|
+
initializer "cache_keeper.caching_methods" do |app|
|
22
|
+
ActiveSupport.on_load :action_controller do
|
23
|
+
ActionController::Base.send :include, CacheKeeper::Caching
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveSupport.on_load :active_record do
|
27
|
+
include CacheKeeper::Caching
|
28
|
+
end
|
20
29
|
end
|
21
30
|
end
|
22
31
|
end
|
data/lib/cache_keeper/manager.rb
CHANGED
@@ -1,42 +1,44 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module CacheKeeper
|
2
|
+
class Manager
|
3
|
+
attr_accessor :cached_methods
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize
|
6
|
+
self.cached_methods = []
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def find(klass, method_name)
|
10
|
+
cached_methods.find do |cached_method|
|
11
|
+
cached_method.klass == klass && cached_method.method_name == method_name
|
12
|
+
end
|
11
13
|
end
|
12
|
-
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
def handled?(klass, method_name)
|
16
|
+
find(klass, method_name).present?
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
def handle(klass, method_name, options)
|
20
|
+
CacheKeeper::CachedMethod.new(klass, method_name, options).tap do |cached_method|
|
21
|
+
cached_methods << cached_method
|
22
|
+
end
|
21
23
|
end
|
22
|
-
end
|
23
24
|
|
24
|
-
|
25
|
-
|
25
|
+
def activate_if_handling(klass, method_name)
|
26
|
+
cached_method = find(klass, method_name) or return
|
26
27
|
|
27
|
-
|
28
|
+
return unless requires_activation?(cached_method)
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
CacheKeeper::ReplaceMethod.replace(cached_method) do
|
31
|
+
cached_method.call(self)
|
32
|
+
end
|
31
33
|
end
|
32
|
-
end
|
33
34
|
|
34
|
-
|
35
|
+
private
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
def requires_activation?(cached_method)
|
38
|
+
return false if cached_method.klass.instance_methods.exclude?(cached_method.method_name) && cached_method.klass.private_instance_methods.exclude?(cached_method.method_name)
|
39
|
+
return false if cached_method.klass.private_instance_methods.include?(cached_method.alias_for_original_method)
|
39
40
|
|
40
|
-
|
41
|
+
true
|
42
|
+
end
|
41
43
|
end
|
42
44
|
end
|
@@ -1,46 +1,48 @@
|
|
1
1
|
# Based on https://github.com/Shopify/sorbet/blob/master/gems/sorbet-runtime/lib/types/private/class_utils.rb
|
2
|
-
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
module CacheKeeper
|
3
|
+
class ReplaceMethod
|
4
|
+
class << self
|
5
|
+
def replace(cached_method, &block)
|
6
|
+
klass = cached_method.klass
|
7
|
+
method_name = cached_method.method_name
|
8
|
+
alias_for_original_method = cached_method.alias_for_original_method
|
9
|
+
original_visibility = visibility_method_name(klass, method_name)
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
define_method_with_visibility klass, method_name, alias_for_original_method, original_visibility, &block
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
+
private
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
16
|
+
# `name` must be an instance method (for class methods, pass in mod.singleton_class)
|
17
|
+
def visibility_method_name(klass, method_name)
|
18
|
+
if klass.public_method_defined? method_name
|
19
|
+
:public
|
20
|
+
elsif klass.protected_method_defined? method_name
|
21
|
+
:protected
|
22
|
+
elsif klass.private_method_defined? method_name
|
23
|
+
:private
|
24
|
+
else
|
25
|
+
raise NameError.new("undefined method `#{method_name}` for `#{klass}`")
|
26
|
+
end
|
25
27
|
end
|
26
|
-
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
def define_method_with_visibility(klass, method_name, alias_for_original_method, visibility, &block)
|
30
|
+
klass.module_exec do
|
31
|
+
alias_method alias_for_original_method, method_name
|
32
|
+
private alias_for_original_method
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
# Start a visibility (public/protected/private) region, so that
|
35
|
+
# all of the method redefinitions happen with the right visibility
|
36
|
+
# from the beginning. This ensures that any other code that is
|
37
|
+
# triggered by `method_added`, sees the redefined method with the
|
38
|
+
# right visibility.
|
39
|
+
send visibility
|
39
40
|
|
40
|
-
|
41
|
+
define_method method_name, &block
|
41
42
|
|
42
|
-
|
43
|
-
|
43
|
+
if block && block.arity < 0 && respond_to?(:ruby2_keywords, true)
|
44
|
+
ruby2_keywords method_name
|
45
|
+
end
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
data/lib/cache_keeper/version.rb
CHANGED
data/lib/cache_keeper.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require "cache_keeper/engine"
|
2
2
|
|
3
|
-
require "cache_keeper/caching"
|
4
|
-
require "cache_keeper/configuration"
|
5
|
-
require "cache_keeper/manager"
|
6
|
-
require "cache_keeper/replace_method"
|
7
|
-
|
8
3
|
module CacheKeeper
|
4
|
+
extend ActiveSupport::Autoload
|
5
|
+
|
6
|
+
autoload :Caching
|
7
|
+
autoload :Configuration
|
8
|
+
autoload :Manager
|
9
|
+
autoload :ReplaceMethod
|
10
|
+
|
9
11
|
mattr_accessor :logger, default: ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout))
|
10
12
|
|
11
13
|
mattr_accessor :configuration, default: CacheKeeper::Configuration.new
|
data/test/manager_test.rb
CHANGED
@@ -2,6 +2,9 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class CacheKeeper::ManagerTest < ActiveSupport::TestCase
|
4
4
|
test "keeps record of the cached methods" do
|
5
|
+
# Load model
|
6
|
+
Recording
|
7
|
+
|
5
8
|
assert_equal 1, CacheKeeper.manager.cached_methods.count
|
6
9
|
assert_equal :slow_method, CacheKeeper.manager.cached_methods.first.method_name
|
7
10
|
end
|