Sha256: 5b051e855965c95d250e826652bac2d973c1c22e8030ccbca855b31e97508c26

Contents?: true

Size: 647 Bytes

Versions: 2

Compression:

Stored size: 647 Bytes

Contents

require 'set'
require 'mongo'
require 'flipper/adapters/mongo'

module Flipper
  module Adapters
    class MongoWithTTL < Mongo
      private

      # Override Mongo adapter's load
      def load
        if expired?
          @document = fresh_load
        end
      end

      def fresh_load
        @last_load_at = Time.now.to_i
        @collection.find_one(@mongo_criteria) || {}
      end

      def ttl
        @options.fetch(:ttl) { 0 }
      end

      def expired?
        return true if never_loaded?
        Time.now.to_i >= (@last_load_at + ttl)
      end

      def never_loaded?
        @last_load_at.nil?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flipper-mongo-0.1.1 lib/flipper/adapters/mongo_with_ttl.rb
flipper-mongo-0.1.0 lib/flipper/adapters/mongo_with_ttl.rb