Sha256: 73ef2a15c34169e313bef1bd0067cbcce964b00b641e833bab6967da4c7a7451

Contents?: true

Size: 588 Bytes

Versions: 3

Compression:

Stored size: 588 Bytes

Contents

# frozen_string_literal: true

class Kredis::Types::Counter < Kredis::Types::Proxying
  prepend Kredis::DefaultValues

  proxying :multi, :set, :incrby, :decrby, :get, :del, :exists?

  attr_accessor :expires_in

  def increment(by: 1)
    multi do
      set 0, ex: expires_in, nx: true if expires_in
      incrby by
    end[-1]
  end

  def decrement(by: 1)
    multi do
      set 0, ex: expires_in, nx: true if expires_in
      decrby by
    end[-1]
  end

  def value
    get.to_i
  end

  def reset
    del
  end

  private
    def set_default
      increment by: default
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kredis-1.7.0 lib/kredis/types/counter.rb
kredis-1.6.1 lib/kredis/types/counter.rb
kredis-1.6.0 lib/kredis/types/counter.rb