use redis for video cache

Signed-off-by: zzls Selfhost <root@selfhost.zzls.xyz>
This commit is contained in:
Emilien Devos
2023-06-10 22:21:38 +02:00
committed by Fijxu
parent b60e056f96
commit 389a2a4a4d
6 changed files with 33 additions and 5 deletions

View File

@@ -74,6 +74,8 @@ class Config
# Database configuration using 12-Factor "Database URL" syntax
@[YAML::Field(converter: Preferences::URIConverter)]
property database_url : URI = URI.parse("")
property redis_url : String?
property redis_socket : String?
# Use polling to keep decryption function up to date
property decrypt_polling : Bool = false
# Used for crawling channels: threads should check all videos uploaded by a channel

View File

@@ -10,7 +10,8 @@ module Invidious::Database::Videos
ON CONFLICT (id) DO NOTHING
SQL
PG_DB.exec(request, video.id, video.info.to_json, video.updated)
REDIS_DB.set(video.id, video.info.to_json, ex: 3600)
REDIS_DB.set(video.id + ":time", video.updated, ex: 3600)
end
def delete(id)
@@ -19,7 +20,8 @@ module Invidious::Database::Videos
WHERE id = $1
SQL
PG_DB.exec(request, id)
REDIS_DB.del(id)
REDIS_DB.del(id + ":time")
end
def delete_expired
@@ -47,6 +49,14 @@ module Invidious::Database::Videos
WHERE id = $1
SQL
return PG_DB.query_one?(request, id, as: Video)
if ((info = REDIS_DB.get(id)) && (time = REDIS_DB.get(id + ":time")))
return Video.new({
id: id,
info: JSON.parse(info).as_h,
updated: Time.parse(time, "%Y-%m-%d %H:%M:%S %z", Time::Location::UTC),
})
else
return nil
end
end
end

View File

@@ -373,7 +373,7 @@ def get_video(id, refresh = true, region = nil, force_refresh = false)
video.schema_version != Video::SCHEMA_VERSION # cache control
begin
video = fetch_video(id, region)
Invidious::Database::Videos.update(video)
Invidious::Database::Videos.insert(video)
rescue ex
Invidious::Database::Videos.delete(id)
raise ex