Sha256: c90d7c401ebdf6e779745c8d93119480ccbb206743482d104b004b1ec8bc51f2

Contents?: true

Size: 1.81 KB

Versions: 60

Compression:

Stored size: 1.81 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

        # Size of the list(s).
        #
        def size
          redis_key = "#{namespace}:*"
          client.keys(redis_key).inject(0) do |total, key|
            total + client.zcard(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 @realtime
            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.
        #
        def []= key, values
          delete key

          redis_key = "#{namespace}:#{key}"
          i = 0
          values.each do |value|
            i += 1
            client.zadd redis_key, i, value
          end

          DirectlyManipulable.make self, values, key
          values
        end

      end

    end

  end

end

Version data entries

60 entries across 60 versions & 1 rubygems

Version Path
picky-4.4.0 lib/picky/backends/redis/list.rb
picky-4.3.2 lib/picky/backends/redis/list.rb
picky-4.3.1 lib/picky/backends/redis/list.rb
picky-4.3.0 lib/picky/backends/redis/list.rb
picky-4.2.4 lib/picky/backends/redis/list.rb
picky-4.2.3 lib/picky/backends/redis/list.rb
picky-4.2.2 lib/picky/backends/redis/list.rb
picky-4.2.1 lib/picky/backends/redis/list.rb
picky-4.2.0 lib/picky/backends/redis/list.rb
picky-4.1.0 lib/picky/backends/redis/list.rb
picky-4.0.9 lib/picky/backends/redis/list.rb
picky-4.0.8 lib/picky/backends/redis/list.rb
picky-4.0.7 lib/picky/backends/redis/list.rb
picky-4.0.6 lib/picky/backends/redis/list.rb
picky-4.0.5 lib/picky/backends/redis/list.rb
picky-4.0.4 lib/picky/backends/redis/list.rb
picky-4.0.3 lib/picky/backends/redis/list.rb
picky-4.0.1 lib/picky/backends/redis/list.rb
picky-4.0.0 lib/picky/backends/redis/list.rb
picky-4.0.0pre6 lib/picky/backends/redis/list.rb