f🤔
Size: a a a
f🤔
y
ES
y
y
y
y
Т
y
Т
y
y
ES
ES
ES
ES
ES
module CacheableMethod
extend ActiveSupport::Concern
DEFAULT_CACHE_EXPIRATION = 1.hour.to_i.freeze
module ClassMethods
unless const_defined?(:CACHE_EXPIRATION)
const_set :CACHE_EXPIRATION, CacheableMethod::DEFAULT_CACHE_EXPIRATION
end
def cache_methods(*methods)
methods.each do |method|
cache_method(method)
end
end
def cache_method(method, params: nil)
define_method("#{method}_with_cache") do |*args|
begin
unless args.last.is_a?(Hash) && args.last.delete(:skip_cache)
key = cache_key(method, params, args)
cached = REDIS.get(key).try(:force_encoding, Encoding::BINARY)
return Marshal.load(cached) if cached.present?
end
result = self.__send__("#{method}_without_cache", *args)
REDIS.setex(key, self.class.const_get(:CACHE_EXPIRATION), Marshal.dump(result))
result
rescue StandardError
self.__send__("#{method}_without_cache", *args)
end
end
alias_method_chain method, :cache
end
end
def cache_key(method, params, args)
[
self.class.name,
method.to_s,
Digest::MD5.hexdigest(get_required_args(method, params, args).to_s)
].join('_')
end
def get_required_args(method, params, args)
return args unless params.present?
arguments = self.method(method.to_s + "_without_cache").parameters.collect{ |a,b| b }
args.values_at(*params.collect{ |a| arguments.index(a) })
end
end
ES
АД
y
rescue StandardError
self.__send__("#{method}_without_cache", *args)