Sha256: 392aac8a02d07ddf89f71c18be1b5d2f144e04d7e313492436b534d7da7464af

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

class Redis
  class Store < self
    module Marshalling
      def set(key, value, options = nil)
        _marshal(value, options) { |value| super encode(key), encode(value), options }
      end

      def setnx(key, value, options = nil)
        _marshal(value, options) { |value| super encode(key), encode(value), options }
      end

      def get(key, options = nil)
        _unmarshal super(key), options
      end

      def mget(*keys)
        options = keys.flatten.pop if keys.flatten.last.is_a?(Hash)
        super(*keys).map do |result|
          _unmarshal result, options
        end
      end

      private
        def _marshal(val, options)
          yield marshal?(options) ? Marshal.dump(val) : val
        end

        def _unmarshal(val, options)
          unmarshal?(val, options) ? Marshal.load(val) : val
        end

        def marshal?(options)
          !(options && options[:raw])
        end

        def unmarshal?(result, options)
          result && result.size > 0 && marshal?(options)
        end

        if defined?(Encoding)
          def encode(string)
            string.to_s.force_encoding(Encoding::BINARY)
          end
        else
          def encode(string)
            string
          end
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
instructure-redis-store-1.0.0.1.instructure1 lib/redis/store/marshalling.rb
redis-store-1.0.0.1 lib/redis/store/marshalling.rb