Module: DistributedCache

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

Defined Under Namespace

Modules: Utils Classes: Config, Manifest

Class Method Summary collapse

Class Method Details

.configObject



10
11
12
13
14
15
16
# File 'lib/distributed_cache.rb', line 10

def self.config
  @@config ||= DistributedCache::Config.new.tap do |c|
    if defined?(Rails)
      c.cache_dir = "#{Rails.root}/cache"
    end
  end
end

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

Yields:



18
19
20
# File 'lib/distributed_cache.rb', line 18

def self.configure
  yield self.config
end

.syncObject



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

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

.update_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


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/distributed_cache.rb', line 27

def self.update_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'}"
  manifest.files << tar_name
  tar_file = "#{config.bundle_dir}/#{tar_name}"
  Dir.chdir(config.cache_dir) do
    DistributedCache::Utils.tar tar_file, klass.cache_name
  end

  manifest.save
  manifest.update_latest_bundle_dir
end