Sha256: b8ad3f7e1c63e1809faa74a7230a83774729063420e2d880788cc16d5fef8f0f

Contents?: true

Size: 707 Bytes

Versions: 1

Compression:

Stored size: 707 Bytes

Contents

module Restruct
  class Batch
    class << self

      def execute(redis=nil)
        redis ||= Restruct.redis
        multi redis
        result = yield
        exec redis
        result
      rescue => ex
        discard redis
        raise ex
      end

      private

      def nesting
        @nesting ||= ::Hash.new { |h,k| h[k] = 0 }
      end

      def multi(redis)
        redis.call 'MULTI' if nesting[redis] == 0
        nesting[redis] += 1
      end

      def exec(redis)
        nesting[redis] -= 1
        redis.call 'EXEC' if nesting[redis] == 0
      end

      def discard(redis)
        nesting[redis] -= 1
        redis.call 'DISCARD' if nesting[redis] == 0
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restruct-0.0.3 lib/restruct/batch.rb