class EBuilder

Inter-Process Cache Manager

Public Instance Methods

assets() click to toggle source
# File lib/el/assets.rb, line 122
def assets
  @sprockets_env ||= Sprockets::Environment.new(root)
end
assets_map(url = nil, server = true)
Alias for: assets_url
assets_url(url = nil, server = true) click to toggle source

set the baseurl for assets. by default, assets URL is empty.

@example assets_url not set

script_tag 'master.js'
=> <script src="master.js"
style_tag 'theme.css'
=> <link href="theme.css"

@example assets_url set to /assets

script_tag 'master.js'
=> <script src="/assets/master.js"
style_tag 'theme.css'
=> <link href="/assets/theme.css"

@note

by default, Sprockets will be used to serve static files.
to disable this, set second argument to false.
# File lib/el/assets.rb, line 112
def assets_url url = nil, server = true
  return @assets_url if @assets_url
  url = url.to_s.dup.strip
  url = url =~ /\A[\w|\d]+\:\/\//i ? url : EUtils.rootify_url(url)
  @assets_url = (url =~ /\/\Z/ ? url : String.new(url) << '/').freeze
  return unless server
  mount_application(assets, @assets_url, on: :GET)
end
Also aliased as: assets_map
cache(key = nil, &proc) click to toggle source

simply running a block and store returned value. on next request the stored value will be returned.

@note

value is not stored if block returns false or nil
# File lib/el/cache.rb, line 28
def cache key = nil, &proc
  key ||= proc.source_location
  cache_pool[key] || ( (val = proc.call) && (cache_pool[key] = val) )
end
cache_pool(pool = nil) click to toggle source

very basic cache implementation. by default the cache will be kept in memory. if you want to use a different pool, set it by using ‘cache_pool` at app level. make sure your pool behaves just like a Hash, meant it responds to `[]=`, `[]`, `keys`, `delete` and `clear`

# File lib/el/cache.rb, line 16
def cache_pool pool = nil
  return @cache_pool if @cache_pool
  @cache_pool = pool if pool
  @cache_pool ||= Hash.new
end
clear_cache(*matchers) click to toggle source

same as ‘clear_cache!` except it is working only on current process

@param [Array] keys

# File lib/el/cache.rb, line 85
def clear_cache *matchers
  return cache_pool.clear if matchers.empty?
  keys = Array.new(cache_pool.keys)
  matchers.each do |matcher|
    mkeys = matcher.is_a?(Regexp) ?
      keys.select {|k| k =~ matcher} :
      keys.select {|k| k == matcher}
    mkeys.each {|k| cache_pool.delete k}
  end
end
clear_cache!(*matchers) click to toggle source

a simple way to manage stored cache. any number of arguments(actually matchers) accepted. matchers can be of String, Symbol or Regexp type. any other arguments ignored

@example

  class App < E

    before do
      if 'some condition occurred'
        # clearing cache only for @red_banners and @db_items
        clear_cache! :red_banners, :db_items
      end
      if 'some another condition occurred'
        # clearing all cache
        clear_cache!
      end
      if 'Yet another condition occurred'
        # clearing cache by regexp
        clear_cache! /banners/, /db/
      end
    end
  end

  def index
    @db_items = cache :db_items do
      # ...
    end
    @red_banners = cache :red_banners do
      # ...
    end
    @blue_banners = cache :blue_banners do
      # ...
    end
    # ...
  end

  def products
    cache do
      # fetch and render products
    end
  end
end

@param [Array] keys

# File lib/el/cache.rb, line 77
def clear_cache! *matchers
  clear_cache *matchers
  ipcm_trigger :clear_cache, *matchers
end
ipcm_signal(signal = nil) click to toggle source
# File lib/el/ipcm.rb, line 35
def ipcm_signal signal = nil
  return @ipcm_signal if @ipcm_signal
  @ipcm_signal = signal.to_s if signal
  @ipcm_signal ||= 'ALRM'
end
ipcm_tmpdir(path = nil) click to toggle source
# File lib/el/ipcm.rb, line 24
def ipcm_tmpdir path = nil
  return @ipcm_tmpdir if @ipcm_tmpdir
  if path
    @ipcm_tmpdir = ((path =~ /\A\// ? path : root + path) + '/').freeze
  else
    @ipcm_tmpdir = (root + 'tmp/ipcm/').freeze
  end
  FileUtils.mkdir_p @ipcm_tmpdir
  @ipcm_tmpdir
end
ipcm_trigger(*args) click to toggle source
# File lib/el/ipcm.rb, line 4
def ipcm_trigger *args
  if pids_reader
    pids = pids_reader.call rescue nil
    if pids.is_a?(Array)
      pids.map(&:to_i).reject {|p| p < 2 || p == Process.pid }.each do |pid|
        file = '%s/%s.%s-%s' % [ipcm_tmpdir, pid, args.hash, Time.now.to_f]
        begin
          File.open(file, 'w') {|f| f << Marshal.dump(args)}
          Process.kill ipcm_signal, pid
        rescue => e
          warn "was unable to perform IPCM operation because of error: %s" % ::CGI.escapeHTML(e.message)
          File.unlink(file) if File.file?(file)
        end
      end
    else
      warn "pids_reader should return an array of pids. Exiting IPCM..."
    end
  end
end
pids(&proc)
Alias for: pids_reader
pids_reader(&proc) click to toggle source
# File lib/el/ipcm.rb, line 58
def pids_reader &proc
  return @pids_reader if @pids_reader
  if proc.is_a?(Proc)
    @pids_reader = proc
    register_ipcm_signal
  end
end
Also aliased as: pids
register_ipcm_signal() click to toggle source
# File lib/el/ipcm.rb, line 41
def register_ipcm_signal
  Signal.trap ipcm_signal do
    Dir[ipcm_tmpdir + '%s.*' % Process.pid].each do |file|
      unless (setup = Marshal.restore(File.read(file)) rescue nil).is_a?(Array)
        warn "Was unable to process \"%s\" cache file, skipping cache cleaning" % file
      end
      File.unlink(file) if File.file?(file)
      meth = setup.shift
      [ :clear_cache,
        :clear_cache_like,
        :clear_compiler,
        :clear_compiler_like,
      ].include?(meth) && self.send(meth, *setup)
    end
  end
end