Class: Xunch::ObjectCache

Inherits:
Cache
  • Object
show all
Defined in:
lib/xunch/cache/object_cache.rb

Instance Method Summary collapse

Methods inherited from Cache

#batch_evict, #destroy, #evict, #ttl

Constructor Details

#initialize(options, shard_infos) ⇒ ObjectCache

Returns a new instance of ObjectCache.



4
5
6
7
# File 'lib/xunch/cache/object_cache.rb', line 4

def initialize(options, shard_infos)
  super
  @codec = JsonCodec.new(@options[:cache_class])
end

Instance Method Details

#get(key) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/xunch/cache/object_cache.rb', line 9

def get(key)
  redis_key = assembleKey(key)
  data = @shard_redis.get(redis_key)
  if data != nil
    @codec.decode(data)
  end
end

#multi_get(keys) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xunch/cache/object_cache.rb', line 31

def multi_get(keys)
  redis_keys = Array.new(keys.length)
  for i in 0 .. keys.length - 1 do
    redis_keys[i] = assembleKey(keys[i])
  end
  datas = @shard_redis.mget(redis_keys)
  for i in 0 .. datas.length - 1 do
    if datas[i] != nil
      datas[i] = @codec.decode(datas[i])
    end
  end
  datas
end

#multi_put(values) ⇒ Object



45
46
47
# File 'lib/xunch/cache/object_cache.rb', line 45

def multi_put(values)
  multi_putex(values,@options[:expire_time])
end

#multi_putex(values, ttl) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xunch/cache/object_cache.rb', line 49

def multi_putex(values,ttl)
  kvs = Hash.new
  values.each { | value |
    if value == nil
      next
    end
    key = getCacheObjectKey(value)
    key = assembleKey(key);
    kvs[key] = @codec.encode(value);
  }
  @shard_redis.mset(kvs, ttl);
end

#put(value) ⇒ Object



17
18
19
# File 'lib/xunch/cache/object_cache.rb', line 17

def put(value)
  putex(value,@options[:expire_time])
end

#putex(value, ttl) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/xunch/cache/object_cache.rb', line 21

def putex(value, ttl)
  if(value == nil)
    return
  end
  key = getCacheObjectKey(value)
  key = assembleKey(key)
  data = @codec.encode(value)
  @shard_redis.set(key,data,ttl)
end