Class: Nexus::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_client/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = '/tmp/cache', enable_analytics = false, logger = nil) ⇒ Cache

Returns a new instance of Cache.



9
10
11
12
13
14
# 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

Instance Attribute Details

#analyticsObject

Returns the value of attribute analytics.



7
8
9
# File 'lib/nexus_client/cache.rb', line 7

def analytics
  @analytics
end

#analytics_enabledObject (readonly)

Returns the value of attribute analytics_enabled.



6
7
8
# File 'lib/nexus_client/cache.rb', line 6

def analytics_enabled
  @analytics_enabled
end

#cache_baseObject (readonly)

Returns the value of attribute cache_base.



6
7
8
# File 'lib/nexus_client/cache.rb', line 6

def cache_base
  @cache_base
end

#logObject

Returns the value of attribute log.



7
8
9
# File 'lib/nexus_client/cache.rb', line 7

def log
  @log
end

Instance Method Details

#add_file(gav, dstfile) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# 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_baseObject



43
44
45
46
47
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/nexus_client/cache.rb', line 90

def exists?(gav)
  file = file_path(gav)
  File.exists?(file)
end

#file_path(gav) ⇒ Object

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



81
82
83
84
85
86
# 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) ⇒ Object



49
50
51
52
53
54
55
56
# 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) ⇒ Object

location is the directory where the files should be cached



76
77
78
# File 'lib/nexus_client/cache.rb', line 76

def location(gav)
  File.join(cache_base, gav.dir_location)
end

#prune_cache(mtime = 15) ⇒ Object

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



96
97
98
99
100
101
102
# 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) ⇒ Object



58
59
60
# File 'lib/nexus_client/cache.rb', line 58

def record_hit(gav)
  analytics.update_item(gav) if analytics_enabled
end