class Nexus::Cache

Attributes

analytics[RW]
analytics_enabled[R]
cache_base[R]
log[RW]

Public Class Methods

new(base_dir='/tmp/cache', enable_analytics=false, logger=nil) click to toggle source
# File lib/nexus_client/cache.rb, line 9
def initialize(base_dir='/tmp/cache', enable_analytics=false, logger=nil)
  @analytics_enabled = enable_analytics
  @cache_base = base_dir
  @log = logger
  create_base
end

Public Instance Methods

add_file(gav, dstfile) click to toggle source
# File lib/nexus_client/cache.rb, line 62
def add_file(gav, dstfile)
  if not File.exists?(location(gav))
    FileUtils.mkdir_p(location(gav))
  end
  if File.exists?(dstfile)
    FileUtils.copy(dstfile, file_path(gav))
    init_cache_info(gav)
  else
    log.warn "file #{dstfile } will not be cached as it doesn't exist"
  end

end
create_base() click to toggle source
# File lib/nexus_client/cache.rb, line 43
def create_base
  if not File.exists?(@cache_base)
    FileUtils.mkdir_p(@cache_base)
  end
end
exists?(gav) click to toggle source

is_cached? returns a bool true if the file is cached the sha1 checksum should be the file name and if it exists, it means its cached

# File lib/nexus_client/cache.rb, line 90
def exists?(gav)
  file = file_path(gav)
  File.exists?(file)
end
file_path(gav) click to toggle source

the file path of the gav, the name of the file is the <sha1>.cache

# File lib/nexus_client/cache.rb, line 81
def file_path(gav)
  if gav.sha1.nil?
    raise('Need sha1 for gav')
  end
  File.join(location(gav), "#{gav.sha1}.cache")
end
init_cache_info(gav) click to toggle source
# File lib/nexus_client/cache.rb, line 49
def init_cache_info(gav)
  if gav.attributes[:size].nil?
    gav.attributes[:size] =  File.size(file_path(gav))
  end
  if analytics_enabled
    analytics.add_item(gav, file_path(gav))
  end
end
location(gav) click to toggle source

location is the directory where the files should be cached

# File lib/nexus_client/cache.rb, line 76
def location(gav)
  File.join(cache_base, gav.dir_location)
end
prune_cache(mtime=15) click to toggle source

the fastest way to prune this is to use the local find command

# File lib/nexus_client/cache.rb, line 96
def prune_cache(mtime=15)
  # get old, unused entries and discard from DB and filesystem
  entries = remove_old_items(mtime)
  entries.each do |key, entry|
    FileUtils.rm_f(entry[:file])
  end
end
record_hit(gav) click to toggle source
# File lib/nexus_client/cache.rb, line 58
def record_hit(gav)
  analytics.update_item(gav) if analytics_enabled
end