Sha256: 5fbfc3a3261adce679c16dda8409c15f464451f3fb2dd61d53d0bc2ca9c3c989

Contents?: true

Size: 883 Bytes

Versions: 2

Compression:

Stored size: 883 Bytes

Contents

module Pubsubstub
  class Channel
    class << self
      def name_from_pubsub_key(key)
        key.sub(/\.pubsub$/, '')
      end
    end

    attr_reader :name

    def initialize(name)
      @name = name.to_s
    end

    def publish(event)
      redis.pipelined do |pipeline|
        pipeline.zadd(scrollback_key, event.id, event.to_json)
        pipeline.zremrangebyrank(scrollback_key, 0, -Pubsubstub.channels_scrollback_size)
        pipeline.expire(scrollback_key, Pubsubstub.channels_scrollback_ttl)
        pipeline.publish(pubsub_key, event.to_json)
      end
    end

    def scrollback(since: )
      redis.zrangebyscore(scrollback_key, Integer(since) + 1, '+inf').map(&Event.method(:from_json))
    end

    def scrollback_key
      "#{name}.scrollback"
    end

    def pubsub_key
      "#{name}.pubsub"
    end

    def redis
      Pubsubstub.redis
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pubsubstub-0.3.0 lib/pubsubstub/channel.rb
pubsubstub-0.2.2 lib/pubsubstub/channel.rb