Class: Xunch::ListFieldObjectCache

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

Overview

列表缓存目前不支持并发写入,未来也不打算支持并发 主要的使用场景是发现页热门的单线程写入和并发的读取 并且提供remove接口,帮助从列表中移除已经不存在的声音,用户,专辑

Instance Method Summary collapse

Methods inherited from Cache

#batch_evict, #destroy, #evict, #ttl

Constructor Details

#initialize(options, shard_infos, object_cache) ⇒ ListFieldObjectCache

Returns a new instance of ListFieldObjectCache.



7
8
9
10
# File 'lib/xunch/cache/list_field_object_cache.rb', line 7

def initialize(options, shard_infos, object_cache)
  super(options,shard_infos)
  @delegate = object_cache
end

Instance Method Details

#delegateObject



62
63
64
# File 'lib/xunch/cache/list_field_object_cache.rb', line 62

def delegate
  @delegate
end

#get(key, page, size) ⇒ Object

查询接口



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xunch/cache/list_field_object_cache.rb', line 17

def get(key, page, size)
  raise "key can not be nil." unless key != nil
  raise "page must be a positive number." unless page != nil and page > 0
  raise "size must be a positive number and less than 100." unless size != nil and size > 0 and size <= 100
  start = (page - 1) * size;
  stop = page * size - 1;
  new_key = assembleKey(key)
  object_keys = @shard_redis.lrange(new_key,start,stop)
  hash = {}
  hash["keys"] = object_keys
  hash["values"] = @delegate.multi_get(object_keys)
  hash
end

#put(key, values) ⇒ Object



31
32
33
# File 'lib/xunch/cache/list_field_object_cache.rb', line 31

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

#putex(key, values, ttl) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/xunch/cache/list_field_object_cache.rb', line 35

def putex(key, values, ttl)
  raise "key can not be nil." unless key != nil
  raise "values can not be nil." unless values != nil
  sub_keys = []
  values.each { | value |
    raise "value in values can not be nil." unless value != nil
    sub_keys.push(@delegate.getCacheObjectKey(value))
  }
  temp_key = assembleTempKey(key)
  new_key = assembleKey(key)
  @delegate.multi_putex(values,ttl)
  @shard_redis.lset(temp_key,new_key,sub_keys,ttl)
end

#remove(key, *sub_keys) ⇒ Object



49
50
51
52
53
54
# File 'lib/xunch/cache/list_field_object_cache.rb', line 49

def remove(key, *sub_keys)
  raise "key can not be nil." unless key != nil
  raise "sub_key can not be nil." unless sub_keys != nil
  new_key = assembleKey(key)
  @shard_redis.lremove(new_key,*sub_keys)
end

#size(key) ⇒ Object



56
57
58
59
60
# File 'lib/xunch/cache/list_field_object_cache.rb', line 56

def size(key)
  raise "key can not be nil." unless key != nil
  new_key = assembleKey(key)
  @shard_redis.llen(new_key)
end