Sha256: fc8b6a029ed73a4436c9bbe190d95793be26b298c7cc2e4b22f8ea9138428626

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

class Cache < ActiveRecord::Base

  attr_protected :site_id
  cattr_accessor :perform_caching
  before_save    :set_site_id

  class << self

    def with(visitor_id, visitor_groups, kpath, *context)
      return yield unless perform_caching
      if cached = self.find(:first, :conditions => ["visitor_id = ? AND site_id = ? AND context = ?", visitor_id, visitor.site.id, context.join('.').hash.abs])
        cached[:content]
      else
        content = yield
        self.create(:visitor_id=>visitor_id, :visitor_groups=>".#{visitor_groups.join('.')}.", :kpath=>kpath,
                    :context=>context.join('.').hash.abs, :content=>content )
        content
      end
    end

    # We can provide a kpath selector for sweeping. If the kpath is in the cached scope, the cache is removed.
    def sweep(hash)
      if kpath  = hash[:kpath]
        klasses = []
        kpath.split(//).each_index { |i| klasses << kpath[0..i].inspect }
        kpath_selector = " AND kpath IN (#{klasses.join(',')})"
      else
        kpath_selector = ""
      end
      if hash[:visitor_id]
        self.connection.execute "DELETE FROM #{self.table_name} WHERE visitor_id = '#{hash[:visitor_id]}'" + kpath_selector
      end
      if hash[:visitor_groups]
        hash[:visitor_groups].each do |g|
          self.connection.execute "DELETE FROM #{self.table_name} WHERE visitor_groups LIKE '%.#{g}.%'" + kpath_selector
        end
      end
      if hash[:context]
        context = [hash[:context]].flatten.join('.').hash.abs
        self.connection.execute "DELETE FROM #{self.table_name} WHERE context = '#{context}'" + kpath_selector
      end
      if hash[:older_than]
        self.connection.execute "DELETE FROM #{self.table_name} WHERE updated_at < '#{hash[:older_than]}'" + kpath_selector
      end
    end
  end

  private
    def set_site_id
      self[:site_id] = visitor.site[:id]
    end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
zena-0.16.9 app/models/cache.rb
zena-0.16.8 app/models/cache.rb
zena-0.16.7 app/models/cache.rb
zena-0.16.6 app/models/cache.rb
zena-0.16.5 app/models/cache.rb
zena-0.16.4 app/models/cache.rb
zena-0.16.3 app/models/cache.rb
zena-0.16.2 app/models/cache.rb
zena-0.16.1 app/models/cache.rb
zena-0.16.0 app/models/cache.rb
zena-0.15.2 app/models/cache.rb
zena-0.15.1 app/models/cache.rb
zena-0.15.0 app/models/cache.rb