temp fix, while cacheable item is not integrated

This commit is contained in:
Samantaz Fox 2023-06-18 13:09:12 +02:00
parent 3a4ca20309
commit c847e357f9
No known key found for this signature in database
GPG Key ID: F42821059186176E
4 changed files with 14 additions and 12 deletions

View File

@ -7,10 +7,10 @@ module Invidious::Cache
abstract class ItemStore abstract class ItemStore
# Retrieves an item from the store # Retrieves an item from the store
# Returns nil if item wasn't found or is expired # Returns nil if item wasn't found or is expired
abstract def fetch(key : String, *, as : T.class) abstract def fetch(key : String)
# Stores a given item into cache # Stores a given item into cache
abstract def store(key : String, value : CacheableItem, expires : Time::Span) abstract def store(key : String, value : CacheableItem | 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)

View File

@ -5,11 +5,11 @@ module Invidious::Cache
def initialize def initialize
end end
def fetch(key : String, *, as : T.class) : T? forall T def fetch(key : String) : String?
return nil return nil
end end
def store(key : String, value : CacheableItem, expires : Time::Span) def store(key : String, value : CacheableItem | String, expires : Time::Span)
end end
def delete(key : String) def delete(key : String)

View File

@ -5,19 +5,17 @@ require "redis"
module Invidious::Cache module Invidious::Cache
class RedisItemStore < ItemStore class RedisItemStore < ItemStore
@redis : Redis::PooledClient @redis : Redis::PooledClient
@node_name : String
def initialize(url : URI, @node_name = "") def initialize(url : URI)
@redis = Redis::PooledClient.new url @redis = Redis::PooledClient.new(url: url.to_s)
end end
def fetch(key : String, *, as : T.class) : (T | Nil) forall T def fetch(key : String) : String?
value = @redis.get(key) return @redis.get(key)
return nil if value.nil?
return T.from_json(JSON::PullParser.new(value))
end end
def store(key : String, value : CacheableItem, expires : Time::Span) def store(key : String, value : CacheableItem | 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

View File

@ -38,3 +38,7 @@ end
# some important informations, and that the query should be sent again. # some important informations, and that the query should be sent again.
class RetryOnceException < Exception class RetryOnceException < Exception
end end
# Exception used to indicate that the config file contains some errors
class InvalidConfigException < Exception
end