Module: DistributedCache

Defined in:
lib/distributed_cache.rb,
lib/distributed_cache/utils.rb,
lib/distributed_cache/bundle.rb,
lib/distributed_cache/config.rb,
lib/distributed_cache/manifest.rb

Defined Under Namespace

Modules: Utils Classes: Bundle, Config, Manifest

Constant Summary collapse

@@config =
nil

Class Method Summary collapse

Class Method Details

.configObject



9
10
11
# File 'lib/distributed_cache.rb', line 9

def self.config
  @@config ||= DistributedCache::Config.new
end

.configure {|self.config| ... } ⇒ Object

Yields:



13
14
15
# File 'lib/distributed_cache.rb', line 13

def self.configure
  yield self.config
end

.syncObject



47
48
49
50
51
# File 'lib/distributed_cache.rb', line 47

def self.sync
  self.config.file_servers.each do |server|
    DistributedCache::Utils.rsync server
  end
end

.update_local_cache(cache_name) ⇒ Object



43
44
45
# File 'lib/distributed_cache.rb', line 43

def self.update_local_cache(cache_name)
  DistributedCache::Bundle.new(self.config, cache_name).install
end

.update_remote_cache(klass) ⇒ Object

in order to cache a klass it must respond to cache_name - this is the directory in the base cache directory where the new cache files are created, ex: applications cache_version - this is the version of the klass being saved, guards against changes to model where new fields are added or removed ex: 20130731 update_cache - this method caches data to disk, it is passed the manifest object. This can be used to store things like the last_id. So on later runs you can just pull the new records out of the database.

ex models = self.where("id > #{manifest['last_id'].to_i").all.each { |m| m.cache_model }; manifest['last_id'] = models.map(&:id).max


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/distributed_cache.rb', line 22

def self.update_remote_cache(klass)
  raise "cache_dir not configured" if self.config.cache_dir.nil?
  raise "bundle_dir not configured" if self.config.bundle_dir.nil?

  manifest = DistributedCache::Manifest.new self.config, klass.cache_name, klass.cache_version
  DistributedCache::Utils.rm_rf manifest.cache_dir
  FileUtils.mkdir_p manifest.cache_dir

  return unless klass.update_cache(manifest)

  tar_name = "#{manifest.cache_path}/#{klass.cache_name}-#{manifest.files.size + 1}.#{manifest.full? ? 'tgz' : 'tar'}"
  tar_file = "#{config.bundle_dir}/#{tar_name}"
  Dir.chdir(config.cache_dir) do
    DistributedCache::Utils.tar tar_file, klass.cache_name
  end
  manifest.add_file tar_name, tar_file

  manifest.save
  manifest.update_latest_bundle_dir
end