Class: Xunch::ListFieldObjectCache
- Defined in:
- lib/xunch/cache/list_field_object_cache.rb
Overview
列表缓存目前不支持并发写入,未来也不打算支持并发主要的使用场景是发现页热门的单线程写入和并发的读取并且提供remove接口,帮助从列表中移除已经不存在的声音,用户,专辑
Instance Method Summary collapse
- #delegate ⇒ Object
-
#get(key, page, size) ⇒ Object
查询接口.
-
#initialize(options, shard_infos, object_cache) ⇒ ListFieldObjectCache
constructor
A new instance of ListFieldObjectCache.
- #put(key, values) ⇒ Object
- #remove(key, sub_key) ⇒ Object
- #size(key) ⇒ Object
Methods inherited from Cache
#batch_evict, #destroy, #evict
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(, shard_infos, object_cache) super(,shard_infos) @delegate = object_cache end |
Instance Method Details
#delegate ⇒ Object
58 59 60 |
# File 'lib/xunch/cache/list_field_object_cache.rb', line 58 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 > 0 raise "size must be a positive number and less than 100." unless page != nil or 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 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/xunch/cache/list_field_object_cache.rb', line 31 def put(key, values) 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,[:expire_time]) @shard_redis.lset(temp_key,new_key,sub_keys,[:expire_time]) end |
#remove(key, sub_key) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/xunch/cache/list_field_object_cache.rb', line 45 def remove(key, sub_key) raise "key can not be nil." unless key != nil raise "sub_key can not be nil." unless sub_key != nil new_key = assembleKey(key) @shard_redis.lremove(new_key,sub_key) end |
#size(key) ⇒ Object
52 53 54 55 56 |
# File 'lib/xunch/cache/list_field_object_cache.rb', line 52 def size(key) raise "key can not be nil." unless key != nil new_key = assembleKey(key) @shard_redis.llen(new_key) end |