-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94e987e
commit 537dfe9
Showing
7 changed files
with
229 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module Cached | ||
module Clear | ||
sig { void } | ||
def clear_cache | ||
super if defined?(super) | ||
|
||
return unless defined?(@cached_method_calls) | ||
|
||
remove_instance_variable(:@cached_method_calls) | ||
end | ||
end | ||
|
||
sig { params(method: Symbol).returns(Symbol) } | ||
def cached(method) | ||
uncached_instance_method = instance_method(method) | ||
|
||
define_method(method) do |*args, **options, &block| | ||
@cached_method_calls ||= T.let({}, T.nilable(T::Hash[Symbol, T::Hash[T.untyped, T.untyped]])) | ||
cache = @cached_method_calls[method] ||= {} | ||
|
||
key = [args, options, block] | ||
if cache.key?(key) | ||
cache.fetch(key) | ||
else | ||
cache[key] = uncached_instance_method.bind(self).call(*args, **options, &block) | ||
end | ||
end | ||
end | ||
|
||
sig { params(method: Symbol).returns(Symbol) } | ||
def cached_class_method(method) | ||
uncached_singleton_method = singleton_method(method) | ||
|
||
define_singleton_method(method) do |*args, **options, &block| | ||
@cached_method_calls ||= T.let({}, T.nilable(T::Hash[Symbol, T::Hash[T.untyped, T.untyped]])) | ||
cache = @cached_method_calls[method] ||= {} | ||
|
||
key = [args, options, block] | ||
if cache.key?(key) | ||
cache[key] | ||
else | ||
cache[key] = uncached_singleton_method.call(*args, **options, &block) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# typed: true | ||
|
||
module Cached | ||
requires_ancestor { Module } | ||
|
||
module Clear | ||
include Kernel | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.