temp fix, while cacheable item is not integrated

This commit is contained in:
Samantaz Fox 2023-06-18 13:09:12 +02:00
parent eeda9b5dba
commit 2a3dca6b1f
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
# Retrieves an item from the store
# 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
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
abstract def delete(key : String)

View File

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

View File

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

View File

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