Sha256: 7a829bd4055338aef0036247435d046b7e70c221d1727f966b7ae7f1385bdba5

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

module Picky

  module Backends

    class Redis

      class List < Basic

        # Clear the index for this list.
        #
        # Note: Perhaps we can use a server only command.
        #       This is not the optimal way to do it.
        #
        def clear
          redis_key = "#{namespace}:*"
          client.keys(redis_key).each do |key|
            client.del key
          end
        end

        # Deletes the list for the key.
        #
        def delete key
          client.del "#{namespace}:#{key}"
        end

        # Writes the hash into Redis.
        #
        def dump hash
          unless @immediate
            clear
            # client.pipelined do
              hash.each_pair do |key, values|
                redis_key = "#{namespace}:#{key}"
                i = 0
                values.each do |value|
                  i += 1
                  client.zadd redis_key, i, value
                end
              end
            # end
          end
        end

        # Get a collection.
        #
        # Internal API method for the index.
        #
        def [] key
          list = client.zrange "#{namespace}:#{key}", :'0', :'-1'
          DirectlyManipulable.make self, list, key
          list
        end

        # Set a single list.
        #
        # TODO Clear? Maybe only add the difference?
        #
        def []= key, values
          redis_key = "#{namespace}:#{key}"
          i = 0
          values.each do |value|
            i += 1
            client.zadd redis_key, i, value
          end

          # We need to return the whole list.
          #
          self[key]
        end

      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
picky-4.0.0pre2 lib/picky/backends/redis/list.rb