From 47d9f402c4e2b3f4ecf82003e433109878a0d4b0 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 12 Feb 2024 21:51:25 +0100 Subject: [PATCH] WIP --- src/invidious/cache.cr | 9 ++++++++- src/invidious/cache/cacheable_item.cr | 9 --------- src/invidious/cache/item_store.cr | 2 +- src/invidious/cache/null_item_store.cr | 2 +- src/invidious/cache/redis_item_store.cr | 3 +-- src/invidious/videos.cr | 13 +++++-------- 6 files changed, 16 insertions(+), 22 deletions(-) delete mode 100644 src/invidious/cache/cacheable_item.cr diff --git a/src/invidious/cache.cr b/src/invidious/cache.cr index e7f2b966..1e64d959 100644 --- a/src/invidious/cache.cr +++ b/src/invidious/cache.cr @@ -3,7 +3,7 @@ require "./cache/*" module Invidious::Cache extend self - INSTANCE = self.init(CONFIG.cache) + private INSTANCE = self.init(CONFIG.cache) def init(cfg : Config::CacheConfig) : ItemStore # Environment variable takes precedence over local config @@ -26,4 +26,11 @@ module Invidious::Cache raise InvalidConfigException.new "Invalid cache url. Only redis:// URL are currently supported." end end + + # Shortcut methods to not have to specify INSTANCE everywhere in the code + {% for method in ["fetch", "store", "delete", "clear"] %} + def {{method.id}}(*args, **kwargs) + INSTANCE.{{method.id}}(*args, **kwargs) + end + {% end %} end diff --git a/src/invidious/cache/cacheable_item.cr b/src/invidious/cache/cacheable_item.cr deleted file mode 100644 index c1295a4a..00000000 --- a/src/invidious/cache/cacheable_item.cr +++ /dev/null @@ -1,9 +0,0 @@ -require "json" - -module Invidious::Cache - # Including this module allows the includer object to be cached. - # The object will automatically inherit from JSON::Serializable. - module CacheableItem - include JSON::Serializable - end -end diff --git a/src/invidious/cache/item_store.cr b/src/invidious/cache/item_store.cr index ebe5b1f9..4780841c 100644 --- a/src/invidious/cache/item_store.cr +++ b/src/invidious/cache/item_store.cr @@ -10,7 +10,7 @@ module Invidious::Cache abstract def fetch(key : String) # Stores a given item into cache - abstract def store(key : String, value : CacheableItem | String, expires : Time::Span) + abstract def store(key : String, value : String, expires : Time::Span) # Prematurely deletes item(s) from the cache abstract def delete(key : String) diff --git a/src/invidious/cache/null_item_store.cr b/src/invidious/cache/null_item_store.cr index 0f599564..786b4109 100644 --- a/src/invidious/cache/null_item_store.cr +++ b/src/invidious/cache/null_item_store.cr @@ -9,7 +9,7 @@ module Invidious::Cache return nil end - def store(key : String, value : CacheableItem | String, expires : Time::Span) + def store(key : String, value : String, expires : Time::Span) end def delete(key : String) diff --git a/src/invidious/cache/redis_item_store.cr b/src/invidious/cache/redis_item_store.cr index bd5550ef..3ae29980 100644 --- a/src/invidious/cache/redis_item_store.cr +++ b/src/invidious/cache/redis_item_store.cr @@ -14,8 +14,7 @@ module Invidious::Cache return @redis.get(key) end - def store(key : String, value : CacheableItem | String, expires : Time::Span) - value = value.to_json if value.is_a?(CacheableItem) + def store(key : String, value : String, expires : Time::Span) @redis.set(key, value, ex: expires.to_i) end diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 15a007c2..097182f8 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -14,6 +14,7 @@ struct Video # the `params` structure in videos/parser.cr!!! # SCHEMA_VERSION = 2 + CACHE_KEY = "video_v#{SCHEMA_VERSION}" property id : String property info : Hash(String, JSON::Any) @@ -36,7 +37,7 @@ struct Video end def self.get(id : String, *, force_refresh = false, region = nil) - key = "video:#{id}" + key = "#{CACHE_KEY}:#{id}" key += ":#{region}" if !region.nil? # Fetch video from cache, unles a force refresh is requested @@ -50,12 +51,8 @@ struct Video else video = Video.new(id, JSON.parse(info).as_h) - # If video has premiered, live has started or the format - # of the video data has changed, refresh the data. - outdated_data = (video.schema_version != Video::SCHEMA_VERSION) - live_started = (video.live_now && video.published < Time.utc) - - if outdated_data || live_started + # If the video has premiered or the live has started, refresh the data. + if (video.live_now && video.published < Time.utc) video = Video.new(id, fetch_video(id, region)) updated = true end @@ -71,7 +68,7 @@ struct Video end end - return video + return Video.new(id, info) end # Methods for API v1 JSON