mirror of
https://gitea.invidious.io/iv-org/invidious.git
synced 2024-08-15 00:53:41 +00:00
WIP
This commit is contained in:
parent
d8dee8e767
commit
81fdf7b089
6 changed files with 16 additions and 22 deletions
|
@ -3,7 +3,7 @@ require "./cache/*"
|
||||||
module Invidious::Cache
|
module Invidious::Cache
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
INSTANCE = self.init(CONFIG.cache)
|
private INSTANCE = self.init(CONFIG.cache)
|
||||||
|
|
||||||
def init(cfg : Config::CacheConfig) : ItemStore
|
def init(cfg : Config::CacheConfig) : ItemStore
|
||||||
# Environment variable takes precedence over local config
|
# 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."
|
raise InvalidConfigException.new "Invalid cache url. Only redis:// URL are currently supported."
|
||||||
end
|
end
|
||||||
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
|
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)
|
abstract def fetch(key : String)
|
||||||
|
|
||||||
# Stores a given item into cache
|
# 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
|
# Prematurely deletes item(s) from the cache
|
||||||
abstract def delete(key : String)
|
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
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def store(key : String, value : CacheableItem | String, expires : Time::Span)
|
def store(key : String, value : String, expires : Time::Span)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(key : String)
|
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)
|
return @redis.get(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def store(key : String, value : CacheableItem | String, expires : Time::Span)
|
def store(key : String, value : String, expires : Time::Span)
|
||||||
value = value.to_json if value.is_a?(CacheableItem)
|
|
||||||
@redis.set(key, value, ex: expires.to_i)
|
@redis.set(key, value, ex: expires.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ struct Video
|
||||||
# the `params` structure in videos/parser.cr!!!
|
# the `params` structure in videos/parser.cr!!!
|
||||||
#
|
#
|
||||||
SCHEMA_VERSION = 2
|
SCHEMA_VERSION = 2
|
||||||
|
CACHE_KEY = "video_v#{SCHEMA_VERSION}"
|
||||||
|
|
||||||
property id : String
|
property id : String
|
||||||
property info : Hash(String, JSON::Any)
|
property info : Hash(String, JSON::Any)
|
||||||
|
@ -36,7 +37,7 @@ struct Video
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get(id : String, *, force_refresh = false, region = nil)
|
def self.get(id : String, *, force_refresh = false, region = nil)
|
||||||
key = "video:#{id}"
|
key = "#{CACHE_KEY}:#{id}"
|
||||||
key += ":#{region}" if !region.nil?
|
key += ":#{region}" if !region.nil?
|
||||||
|
|
||||||
# Fetch video from cache, unles a force refresh is requested
|
# Fetch video from cache, unles a force refresh is requested
|
||||||
|
@ -50,12 +51,8 @@ struct Video
|
||||||
else
|
else
|
||||||
video = Video.new(id, JSON.parse(info).as_h)
|
video = Video.new(id, JSON.parse(info).as_h)
|
||||||
|
|
||||||
# If video has premiered, live has started or the format
|
# If the video has premiered or the live has started, refresh the data.
|
||||||
# of the video data has changed, refresh the data.
|
if (video.live_now && video.published < Time.utc)
|
||||||
outdated_data = (video.schema_version != Video::SCHEMA_VERSION)
|
|
||||||
live_started = (video.live_now && video.published < Time.utc)
|
|
||||||
|
|
||||||
if outdated_data || live_started
|
|
||||||
video = Video.new(id, fetch_video(id, region))
|
video = Video.new(id, fetch_video(id, region))
|
||||||
updated = true
|
updated = true
|
||||||
end
|
end
|
||||||
|
@ -71,7 +68,7 @@ struct Video
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return video
|
return Video.new(id, info)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Methods for API v1 JSON
|
# Methods for API v1 JSON
|
||||||
|
|
Loading…
Reference in a new issue