Skip to content

Instantly share code, notes, and snippets.

@mstruve
Created November 16, 2018 19:48
Show Gist options
  • Save mstruve/fe7ec87e7b76d228146b9a5a33c79bf3 to your computer and use it in GitHub Desktop.
Save mstruve/fe7ec87e7b76d228146b9a5a33c79bf3 to your computer and use it in GitHub Desktop.

Revisions

  1. mstruve created this gist Nov 16, 2018.
    28 changes: 28 additions & 0 deletions redis.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    if ENV['REDIS_ACCESS_MODE'] == 'readonly'
    class Redis
    class Client
    WRITE_COMMANDS = ::Rails.cache.data.command.map { |a| a[0] if a[2].include?('write') }.compact.to_set.freeze

    def process(commands)
    if commands.flatten.any? { |c| WRITE_COMMANDS.include?(c.to_s) }
    raise NotImplementedError, "REDIS_ACCESS_MODE is set to 'readonly', disallowing writes"
    end

    logging(commands) do
    ensure_connected do
    commands.each do |command|
    if command_map[command.first]
    command = command.dup
    command[0] = command_map[command.first]
    end

    write(command)
    end

    yield if block_given?
    end
    end
    end
    end
    end
    end