diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-05-15 22:55:16 +0300 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-05-15 22:55:16 +0300 |
commit | 6cc3a07dca635aa61d275eb6803cd986f4c9f967 (patch) | |
tree | 398079c866fd6ef146273a15f67ffb7c7c0fc577 /app/models/concerns/redis_cacheable.rb | |
parent | 2c29e80a93dffb1a854f4c63171b1a91eddea8d9 (diff) |
Dynamically cast value from cache
Diffstat (limited to 'app/models/concerns/redis_cacheable.rb')
-rw-r--r-- | app/models/concerns/redis_cacheable.rb | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/app/models/concerns/redis_cacheable.rb b/app/models/concerns/redis_cacheable.rb index 8bd0df8dfbe..4fdaaddeee7 100644 --- a/app/models/concerns/redis_cacheable.rb +++ b/app/models/concerns/redis_cacheable.rb @@ -8,16 +8,9 @@ module RedisCacheable def cached_attr_reader(*attributes) attributes.each do |attribute| define_method(attribute) do - cached_attribute(attribute) || read_attribute(attribute) - end - end - end - - def cached_attr_time_reader(*attributes) - attributes.each do |attribute| - define_method(attribute) do cached_value = cached_attribute(attribute) - cached_value ? Time.zone.parse(cached_value) : read_attribute(attribute) + cached_value = cast_value_from_cache(attribute, cached_value) if cached_value + cached_value || read_attribute(attribute) end end end @@ -49,4 +42,12 @@ module RedisCacheable end end end + + def cast_value_from_cache(attribute, value) + if self.class.column_for_attribute(attribute).respond_to?(:type_cast_from_database) + self.class.column_for_attribute(attribute).type_cast_from_database(value) + else + self.class.type_for_attribute(attribute).cast(value) + end + end end |