Improve db cache and support conditional render

This commit is contained in:
Alexander Olofsson 2024-06-21 14:29:07 +02:00
parent af78545d43
commit 61587d6e0c
Signed by: ace
GPG key ID: D439C9470CB04C73
5 changed files with 24 additions and 11 deletions

View file

@ -29,8 +29,13 @@ optparse = OptParse.new do |opts|
options.output = output options.output = output
end end
opts.on '--only-changed', 'Only store the story if it has been changed since the last update' do
options.only_changed = true
end
opts.separator '' opts.separator ''
opts.on '-C', '--config=FILE', 'Specify a configuration file to read' do |config| opts.on '-C', '--config=FILE', 'Specify a configuration file to read' do |config|
options.config = config options.config = config
end end
@ -74,11 +79,14 @@ slug = options.story
slug = backend.parse_slug(slug) if backend.respond_to? :parse_slug slug = backend.parse_slug(slug) if backend.respond_to? :parse_slug
story = FicTracker::Models::Story.find(backend_name: backend.name, slug:) || FicTracker::Models::Story.new(backend:, slug:) story = FicTracker::Models::Story.find(backend_name: backend.name, slug:) || FicTracker::Models::Story.new(backend:, slug:)
before = story.etag
story.ensure_fully_loaded story.ensure_fully_loaded
data = nil data = nil
options.output ||= "#{story.safe_name}.#{options.format}" options.output ||= "#{story.safe_name}.#{options.format}"
if !options.only_changed || story.etag != before
FicTracker.logger.info "Saving to #{options.output}" FicTracker.logger.info "Saving to #{options.output}"
File.open(options.output, 'w') { |f| FicTracker::Renderers.render(options.format, story, io: f) } File.open(options.output, 'w') { |f| FicTracker::Renderers.render(options.format, story, io: f) }
end
story.save_changes story.save_changes

View file

@ -154,14 +154,15 @@ module FicTracker::Backends::Ao3
slug = aut[:href].split('/')[2] slug = aut[:href].split('/')[2]
pseud = aut[:href].split('/').last pseud = aut[:href].split('/').last
aut_name = nil
if slug != pseud if slug != pseud
name = pseud aut_name = pseud
slug = "#{slug}/#{pseud}" slug = "#{slug}/#{pseud}"
end end
{ {
slug: slug, slug: slug,
name: name, name: aut_name,
url: aut[:href], url: aut[:href],
}.compact }.compact
end end

View file

@ -40,7 +40,9 @@ module FicTracker::Backends::Ao3
debug_http(resp) debug_http(resp)
case resp case resp
when Net::HTTPRedirection when Net::HTTPRedirection
req.path.replace resp['location'] uri = URI.join(url, resp['location'])
uri.query = URI.encode_www_form(query) if query
req.path.replace uri.request_uri
when Net::HTTPTooManyRequests when Net::HTTPTooManyRequests
wait_time = 10 wait_time = 10
if resp['retry-after'] if resp['retry-after']

View file

@ -46,9 +46,10 @@ module FicTracker::Models
return unless cache_key return unless cache_key
key = cache_key + [:content] key = cache_key + [:content]
refresh_content! unless FicTracker.cache.has?(key)
@content ||= FicTracker.cache.get(key) @content ||= FicTracker.cache.get(key)
refresh_content! unless @content
@content
end end
def content=(content) def content=(content)
@ -72,9 +73,10 @@ module FicTracker::Models
return unless cache_key return unless cache_key
key = cache_key + [:content_type] key = cache_key + [:content_type]
refresh_content! unless FicTracker.cache.has?(key)
@content_type ||= FicTracker.cache.get(key) @content_type ||= FicTracker.cache.get(key)
refresh_content! unless @content_type
@content_type
end end
def content_type=(type) def content_type=(type)

View file

@ -7,8 +7,8 @@ module FicTracker::Util::CacheImpl
def initialize(table: 'cache', **redis) def initialize(table: 'cache', **redis)
@dataset = FicTracker.database[table.to_s.to_sym] @dataset = FicTracker.database[table.to_s.to_sym]
@dataset_expired = @dataset.exclude{ Sequel.|({ expire_at: nil }, Sequel::CURRENT_DATE < expire_at) }
@dataset_live = @dataset.where{ Sequel.|({ expire_at: nil }, Sequel::CURRENT_DATE < expire_at) } @dataset_live = @dataset.where{ Sequel.|({ expire_at: nil }, Sequel::CURRENT_DATE < expire_at) }
@dataset_expired = @dataset_live.invert
end end
def expire def expire
@ -16,7 +16,7 @@ module FicTracker::Util::CacheImpl
end end
def has?(key) def has?(key)
dataset_live.filter(key: key).any? !dataset_live.filter(key: key).empty?
end end
def get(key) def get(key)