forked from Fijxu/invidious
use redis for video cache
Signed-off-by: zzls Selfhost <root@selfhost.zzls.xyz>
This commit is contained in:
parent
b60e056f96
commit
389a2a4a4d
6 changed files with 33 additions and 5 deletions
|
@ -32,6 +32,10 @@ shards:
|
||||||
git: https://github.com/will/crystal-pg.git
|
git: https://github.com/will/crystal-pg.git
|
||||||
version: 0.24.0
|
version: 0.24.0
|
||||||
|
|
||||||
|
pool:
|
||||||
|
git: https://github.com/ysbaddaden/pool.git
|
||||||
|
version: 0.2.4
|
||||||
|
|
||||||
protodec:
|
protodec:
|
||||||
git: https://github.com/iv-org/protodec.git
|
git: https://github.com/iv-org/protodec.git
|
||||||
version: 0.1.5
|
version: 0.1.5
|
||||||
|
@ -40,6 +44,10 @@ shards:
|
||||||
git: https://github.com/luislavena/radix.git
|
git: https://github.com/luislavena/radix.git
|
||||||
version: 0.4.1
|
version: 0.4.1
|
||||||
|
|
||||||
|
redis:
|
||||||
|
git: https://github.com/stefanwille/crystal-redis.git
|
||||||
|
version: 2.9.1
|
||||||
|
|
||||||
spectator:
|
spectator:
|
||||||
git: https://github.com/icy-arctic-fox/spectator.git
|
git: https://github.com/icy-arctic-fox/spectator.git
|
||||||
version: 0.10.4
|
version: 0.10.4
|
||||||
|
|
|
@ -28,6 +28,8 @@ dependencies:
|
||||||
athena-negotiation:
|
athena-negotiation:
|
||||||
github: athena-framework/negotiation
|
github: athena-framework/negotiation
|
||||||
version: ~> 0.1.1
|
version: ~> 0.1.1
|
||||||
|
redis:
|
||||||
|
github: stefanwille/crystal-redis
|
||||||
|
|
||||||
development_dependencies:
|
development_dependencies:
|
||||||
spectator:
|
spectator:
|
||||||
|
|
|
@ -31,6 +31,7 @@ require "xml"
|
||||||
require "yaml"
|
require "yaml"
|
||||||
require "compress/zip"
|
require "compress/zip"
|
||||||
require "protodec/utils"
|
require "protodec/utils"
|
||||||
|
require "redis"
|
||||||
|
|
||||||
require "./invidious/database/*"
|
require "./invidious/database/*"
|
||||||
require "./invidious/database/migrations/*"
|
require "./invidious/database/migrations/*"
|
||||||
|
@ -60,7 +61,12 @@ alias IV = Invidious
|
||||||
CONFIG = Config.load
|
CONFIG = Config.load
|
||||||
HMAC_KEY = CONFIG.hmac_key
|
HMAC_KEY = CONFIG.hmac_key
|
||||||
|
|
||||||
PG_DB = DB.open CONFIG.database_url
|
PG_DB = DB.open CONFIG.database_url
|
||||||
|
REDIS_DB = Redis::PooledClient.new(unixsocket: CONFIG.redis_socket || nil, url: CONFIG.redis_url || nil)
|
||||||
|
|
||||||
|
if REDIS_DB.ping
|
||||||
|
puts "Connected to redis"
|
||||||
|
end
|
||||||
ARCHIVE_URL = URI.parse("https://archive.org")
|
ARCHIVE_URL = URI.parse("https://archive.org")
|
||||||
PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")
|
PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")
|
||||||
REDDIT_URL = URI.parse("https://www.reddit.com")
|
REDDIT_URL = URI.parse("https://www.reddit.com")
|
||||||
|
|
|
@ -74,6 +74,8 @@ class Config
|
||||||
# Database configuration using 12-Factor "Database URL" syntax
|
# Database configuration using 12-Factor "Database URL" syntax
|
||||||
@[YAML::Field(converter: Preferences::URIConverter)]
|
@[YAML::Field(converter: Preferences::URIConverter)]
|
||||||
property database_url : URI = URI.parse("")
|
property database_url : URI = URI.parse("")
|
||||||
|
property redis_url : String?
|
||||||
|
property redis_socket : String?
|
||||||
# Use polling to keep decryption function up to date
|
# Use polling to keep decryption function up to date
|
||||||
property decrypt_polling : Bool = false
|
property decrypt_polling : Bool = false
|
||||||
# Used for crawling channels: threads should check all videos uploaded by a channel
|
# Used for crawling channels: threads should check all videos uploaded by a channel
|
||||||
|
|
|
@ -10,7 +10,8 @@ module Invidious::Database::Videos
|
||||||
ON CONFLICT (id) DO NOTHING
|
ON CONFLICT (id) DO NOTHING
|
||||||
SQL
|
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
|
end
|
||||||
|
|
||||||
def delete(id)
|
def delete(id)
|
||||||
|
@ -19,7 +20,8 @@ module Invidious::Database::Videos
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
PG_DB.exec(request, id)
|
REDIS_DB.del(id)
|
||||||
|
REDIS_DB.del(id + ":time")
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_expired
|
def delete_expired
|
||||||
|
@ -47,6 +49,14 @@ module Invidious::Database::Videos
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
SQL
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -373,7 +373,7 @@ def get_video(id, refresh = true, region = nil, force_refresh = false)
|
||||||
video.schema_version != Video::SCHEMA_VERSION # cache control
|
video.schema_version != Video::SCHEMA_VERSION # cache control
|
||||||
begin
|
begin
|
||||||
video = fetch_video(id, region)
|
video = fetch_video(id, region)
|
||||||
Invidious::Database::Videos.update(video)
|
Invidious::Database::Videos.insert(video)
|
||||||
rescue ex
|
rescue ex
|
||||||
Invidious::Database::Videos.delete(id)
|
Invidious::Database::Videos.delete(id)
|
||||||
raise ex
|
raise ex
|
||||||
|
|
Loading…
Reference in a new issue