mirror of
https://github.com/iv-org/invidious.git
synced 2024-06-07 19:40:52 +00:00
WIP
This commit is contained in:
parent
82d92f4314
commit
47d9f402c4
@ -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
|
||||
|
9
src/invidious/cache/cacheable_item.cr
vendored
9
src/invidious/cache/cacheable_item.cr
vendored
@ -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
|
2
src/invidious/cache/item_store.cr
vendored
2
src/invidious/cache/item_store.cr
vendored
@ -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)
|
||||
|
2
src/invidious/cache/null_item_store.cr
vendored
2
src/invidious/cache/null_item_store.cr
vendored
@ -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)
|
||||
|
3
src/invidious/cache/redis_item_store.cr
vendored
3
src/invidious/cache/redis_item_store.cr
vendored
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user