Class: JOffice::Redis::HashTable

Inherits:
Object
  • Object
show all
Defined in:
lib/joffice_redis/hash_table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, redis) ⇒ HashTable

Returns a new instance of HashTable.



23
24
25
# File 'lib/joffice_redis/hash_table.rb', line 23

def initialize(id, redis)
  @id, @redis, @_attributes=id, redis,{}
end

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



21
22
23
# File 'lib/joffice_redis/hash_table.rb', line 21

def redis
  @redis
end

Class Method Details

.keys(id, redis) ⇒ Object



67
68
69
# File 'lib/joffice_redis/hash_table.rb', line 67

def keys(id, redis)
 (r=redis.keys("#{id}:*")) ? r : []          
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/joffice_redis/hash_table.rb', line 27

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



35
36
37
# File 'lib/joffice_redis/hash_table.rb', line 35

def []=(key, value)
  set(key, value)
end

#delete(key) ⇒ Object



61
62
63
64
# File 'lib/joffice_redis/hash_table.rb', line 61

def delete(key)
  redis.del(hash_key(key))
  @_attributes.delete(key)
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/joffice_redis/hash_table.rb', line 56

def exists?(key)
  k=hash_key(key)
  (@_attributes.has_key?(k) || redis.exists?(k))
end

#get(key) ⇒ Object



31
32
33
# File 'lib/joffice_redis/hash_table.rb', line 31

def get(key)
  @_attributes[key]||=((v=redis.get(hash_key(key))) ? Marshal.load(v) : v)
end

#hash_key(key) ⇒ Object



52
53
54
# File 'lib/joffice_redis/hash_table.rb', line 52

def hash_key(key)
  "#{@id}:#{key}".force_encoding('ASCII-8BIT')
end

#set(key, value, expire = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/joffice_redis/hash_table.rb', line 39

def set(key, value, expire=nil)
  if value
    @_attributes[key]=value unless expire
    k=hash_key(key)      
    redis.set(k, Marshal.dump(value))
    redis.expire(k, expire) if expire
  else
    redis.del(hash_key(key))
    @_attributes.delete(key)
  end
  value
end