lib/relaton/db_cache.rb in relaton-1.7.pre7 vs lib/relaton/db_cache.rb in relaton-1.7.0

- old
+ new

@@ -1,7 +1,6 @@ require "fileutils" -require "timeout" module Relaton class DbCache # @return [String] attr_reader :dir @@ -13,30 +12,10 @@ FileUtils::mkdir_p @dir # file_version = "#{@dir}/version" # set_version # unless File.exist? file_version end - # Move caches to anothe dir - # @param new_dir [String, nil] - # @return [String, nil] - def mv(new_dir) - return unless new_dir && @ext == "xml" - - if File.exist? new_dir - warn "[relaton] WARNING: target directory exists \"#{new_dir}\"" - return - end - - FileUtils.mv dir, new_dir - @dir = new_dir - end - - # Clear database - def clear - FileUtils.rm_rf Dir.glob "#{dir}/*" if @ext == "xml" # unless it's static DB - end - # Save item # @param key [String] # @param value [String] Bibitem xml serialization def []=(key, value) if value.nil? @@ -84,24 +63,23 @@ # @return [String] def fetched(key) value = self[key] return unless value - if value.match? /^not_found/ + if value =~ /^not_found/ value.match(/\d{4}-\d{2}-\d{2}/).to_s else doc = Nokogiri::XML value doc.at("/bibitem/fetched|bibdata/fetched")&.text end end # Returns all items # @return [Array<String>] def all - Dir.glob("#{@dir}/**/*.{xml,yml,yaml}").sort.map do |f| - content = File.read(f, encoding: "utf-8") - block_given? ? yield(f, content) : content + Dir.glob("#{@dir}/**/*.xml").sort.map do |f| + File.read(f, encoding: "utf-8") end end # Delete item # @param key [String] @@ -179,11 +157,11 @@ # @param key [String] # @return [String] def filename(key) prefcode = key.downcase.match /^(?<prefix>[^\(]+)\((?<code>[^\)]+)/ fn = if prefcode - "#{prefcode[:prefix]}/#{prefcode[:code].gsub(/[-:\s\/\()]/, '_').squeeze('_')}" + "#{prefcode[:prefix]}/#{prefcode[:code].gsub(/[-:\s\/\()]/, '_').squeeze("_")}" else key.gsub(/[-:\s]/, "_") end "#{@dir}/#{fn.sub(/(,|_$)/, '')}" end @@ -213,11 +191,11 @@ # @param file [String] # @content [String] def file_safe_write(file, content) File.open file, File::RDWR | File::CREAT, encoding: "UTF-8" do |f| - Timeout.timeout(10) { f.flock File::LOCK_EX } + Timeout.timeout(1) { f.flock File::LOCK_EX } f.write content end end class << self @@ -238,10 +216,10 @@ # Initialse and return relaton instance, with local and global cache names # local_cache: local cache name; none created if nil; "relaton" created # if empty global_cache: boolean to create global_cache # flush_caches: flush caches - def init_bib_caches(opts) # rubocop:disable Metrics/CyclomaticComplexity + def init_bib_caches(opts) globalname = global_bibliocache_name if opts[:global_cache] localname = local_bibliocache_name(opts[:local_cache]) localname = "relaton" if localname&.empty? if opts[:flush_caches] FileUtils.rm_rf globalname unless globalname.nil?