From 61587d6e0ccfeed3011cdba5bccf76560fe248dd Mon Sep 17 00:00:00 2001 From: "Alexander \"Ace\" Olofsson" Date: Fri, 21 Jun 2024 14:29:07 +0200 Subject: [PATCH] Improve db cache and support conditional render --- bin/fic_tracker | 12 ++++++++++-- lib/fic_tracker/backends/ao3/backend.rb | 5 +++-- lib/fic_tracker/backends/ao3/client.rb | 4 +++- lib/fic_tracker/models/chapter.rb | 10 ++++++---- lib/fic_tracker/util/cache/database.rb | 4 ++-- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/bin/fic_tracker b/bin/fic_tracker index c739d4c..3ccf9ed 100755 --- a/bin/fic_tracker +++ b/bin/fic_tracker @@ -29,8 +29,13 @@ optparse = OptParse.new do |opts| options.output = output 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.on '-C', '--config=FILE', 'Specify a configuration file to read' do |config| options.config = config end @@ -74,11 +79,14 @@ slug = options.story 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:) +before = story.etag story.ensure_fully_loaded data = nil options.output ||= "#{story.safe_name}.#{options.format}" -FicTracker.logger.info "Saving to #{options.output}" -File.open(options.output, 'w') { |f| FicTracker::Renderers.render(options.format, story, io: f) } +if !options.only_changed || story.etag != before + FicTracker.logger.info "Saving to #{options.output}" + File.open(options.output, 'w') { |f| FicTracker::Renderers.render(options.format, story, io: f) } +end story.save_changes diff --git a/lib/fic_tracker/backends/ao3/backend.rb b/lib/fic_tracker/backends/ao3/backend.rb index f087d33..277bf06 100644 --- a/lib/fic_tracker/backends/ao3/backend.rb +++ b/lib/fic_tracker/backends/ao3/backend.rb @@ -154,14 +154,15 @@ module FicTracker::Backends::Ao3 slug = aut[:href].split('/')[2] pseud = aut[:href].split('/').last + aut_name = nil if slug != pseud - name = pseud + aut_name = pseud slug = "#{slug}/#{pseud}" end { slug: slug, - name: name, + name: aut_name, url: aut[:href], }.compact end diff --git a/lib/fic_tracker/backends/ao3/client.rb b/lib/fic_tracker/backends/ao3/client.rb index ab3928e..c6e0012 100644 --- a/lib/fic_tracker/backends/ao3/client.rb +++ b/lib/fic_tracker/backends/ao3/client.rb @@ -40,7 +40,9 @@ module FicTracker::Backends::Ao3 debug_http(resp) case resp 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 wait_time = 10 if resp['retry-after'] diff --git a/lib/fic_tracker/models/chapter.rb b/lib/fic_tracker/models/chapter.rb index 97ef6fb..f329bc8 100644 --- a/lib/fic_tracker/models/chapter.rb +++ b/lib/fic_tracker/models/chapter.rb @@ -46,9 +46,10 @@ module FicTracker::Models return unless cache_key key = cache_key + [:content] - refresh_content! unless FicTracker.cache.has?(key) - @content ||= FicTracker.cache.get(key) + refresh_content! unless @content + + @content end def content=(content) @@ -72,9 +73,10 @@ module FicTracker::Models return unless cache_key key = cache_key + [:content_type] - refresh_content! unless FicTracker.cache.has?(key) - @content_type ||= FicTracker.cache.get(key) + refresh_content! unless @content_type + + @content_type end def content_type=(type) diff --git a/lib/fic_tracker/util/cache/database.rb b/lib/fic_tracker/util/cache/database.rb index 1a2ca1e..37ad05b 100644 --- a/lib/fic_tracker/util/cache/database.rb +++ b/lib/fic_tracker/util/cache/database.rb @@ -7,8 +7,8 @@ module FicTracker::Util::CacheImpl def initialize(table: 'cache', **redis) @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_expired = @dataset_live.invert end def expire @@ -16,7 +16,7 @@ module FicTracker::Util::CacheImpl end def has?(key) - dataset_live.filter(key: key).any? + !dataset_live.filter(key: key).empty? end def get(key)