Sha256: 6166c7ff81a056f00ad6ad5042e13fd7352cd047d7feb39cc6091482c1fb287f

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

if Hancock.mongoid?

  module Hancock::Cacheable
    extend ActiveSupport::Concern

    included do
      field :cache_keys_str, type: String, default: -> { default_cache_keys.join("\n") }
      def self.default_cache_keys
        []
      end
      def default_cache_keys
        self.class.default_cache_keys
      end
      def set_default_cache_keys(strategy = :append)
        _old_keys = cache_keys

        _keys = case strategy.to_sym
        when :append
          (_old_keys + default_cache_keys).uniq
        when :overwrite, :replace
          default_cache_keys
        else
          _old_keys
        end
        self
      end
      def set_default_cache_keys!(strategy = :append)
        self.set_default_cache_keys(strategy) and self.save
      end


      def cache_keys
        cache_keys_str.split(/\s+/).map { |k| k.strip }.reject { |k| k.blank? }
      end
      field :perform_caching, type: Boolean, default: true

      after_touch :clear_cache
      after_save :clear_cache
      after_destroy :clear_cache
      def clear_cache
        if perform_caching
          cache_keys.each do |k|
            Rails.cache.delete(k)
          end
        end
      end

    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hancock_cms-2.0.0.2 app/models/concerns/hancock/cacheable.rb
hancock_cms-1.0.0.4 app/models/concerns/hancock/cacheable.rb