Class: Familia::HashKey

Inherits:
RedisObject show all
Defined in:
lib/familia/redisobject.rb

Instance Attribute Summary

Attributes inherited from RedisObject

#cache, #name, #parent, #redis

Instance Method Summary collapse

Methods inherited from RedisObject

#class?, #clear_cache, #db, #dump_method, #echo, #exists?, #expire, #expireat, #from_redis, inherited, #initialize, #load_method, #move, #multi_from_redis, #parent?, #persist, #realttl, #rediskey, register, registration, #rename, #renamenx, #to_redis, #ttl, #type, #update_expiration

Constructor Details

This class inherits a constructor from Familia::RedisObject

Instance Method Details

#[](n) ⇒ Object Also known as: get



783
784
785
# File 'lib/familia/redisobject.rb', line 783

def [] n
  from_redis redis.hget(rediskey, n)
end

#[]=(n, v) ⇒ Object Also known as: put, store



775
776
777
778
779
# File 'lib/familia/redisobject.rb', line 775

def []= n, v
  ret = redis.hset rediskey, n, to_redis(v)
  update_expiration
  ret
end

#allObject Also known as: to_hash, clone



807
808
809
810
# File 'lib/familia/redisobject.rb', line 807

def all
  # TODO: from_redis
  redis.hgetall rediskey
end

#decrement(n, by = 1) ⇒ Object Also known as: decr, decrby



833
834
835
# File 'lib/familia/redisobject.rb', line 833

def decrement n, by=1
  increment n, -by
end

#delete(n) ⇒ Object Also known as: remove, rem, del



820
821
822
# File 'lib/familia/redisobject.rb', line 820

def delete n
  redis.hdel rediskey, n
end

#empty?Boolean

Returns:

  • (Boolean)


771
772
773
# File 'lib/familia/redisobject.rb', line 771

def empty?
  size == 0
end

#fetch(n, default = nil) ⇒ Object



788
789
790
791
792
793
794
795
796
# File 'lib/familia/redisobject.rb', line 788

def fetch n, default=nil
  ret = self[n]
  if ret.nil? 
    raise IndexError.new("No such index for: #{n}") if default.nil?
    default
  else
    ret
  end
end

#has_key?(n) ⇒ Boolean Also known as: include?, member?

Returns:

  • (Boolean)


814
815
816
# File 'lib/familia/redisobject.rb', line 814

def has_key? n
  redis.hexists rediskey, n
end

#increment(n, by = 1) ⇒ Object Also known as: incr, incrby



827
828
829
# File 'lib/familia/redisobject.rb', line 827

def increment n, by=1
  redis.hincrby(rediskey, n, by).to_i
end

#keysObject



798
799
800
# File 'lib/familia/redisobject.rb', line 798

def keys
  redis.hkeys rediskey
end

#sizeObject Also known as: length



766
767
768
# File 'lib/familia/redisobject.rb', line 766

def size
  redis.hlen rediskey
end

#update(h = {}) ⇒ Object Also known as: merge!

Raises:

  • (ArgumentError)


839
840
841
842
843
844
845
# File 'lib/familia/redisobject.rb', line 839

def update h={}
  raise ArgumentError, "Argument to bulk_set must be a hash" unless Hash === h
  data = h.inject([]){ |ret,pair| ret << [pair[0], to_redis(pair[1])] }.flatten
  ret = redis.hmset(rediskey, *data)
  update_expiration
  ret
end

#valuesObject



802
803
804
805
# File 'lib/familia/redisobject.rb', line 802

def values
  el = redis.hvals(rediskey)
  multi_from_redis *el
end

#values_at(*names) ⇒ Object



848
849
850
851
# File 'lib/familia/redisobject.rb', line 848

def values_at *names
  el = redis.hmget(rediskey, *names.flatten.compact)
  multi_from_redis *el
end