Improve db cache and support conditional render
This commit is contained in:
parent
af78545d43
commit
61587d6e0c
5 changed files with 24 additions and 11 deletions
|
|
@ -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}"
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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']
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
4
lib/fic_tracker/util/cache/database.rb
vendored
4
lib/fic_tracker/util/cache/database.rb
vendored
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue