Class: Xunch::ListIdCache

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

Overview

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

Instance Method Summary collapse

Methods inherited from Cache

#batch_evict, #destroy, #evict, #ttl

Constructor Details

#initialize(options, shard_infos) ⇒ ListIdCache

Returns a new instance of ListIdCache.



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

def initialize(options, shard_infos)
  unless RUBY_PLATFORM =~ /mingw/
    options[:driver] = 'hiredis'
  else
    options.delete(:driver)
  end
  super(options,shard_infos)
  # p options
end

Instance Method Details

#get(key, page, size) ⇒ Object

查询接口



22
23
24
25
26
27
28
29
30
31
# File 'lib/xunch/cache/list_id_cache.rb', line 22

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)
  ids = @shard_redis.lrange(new_key,start,stop)
  ids
end

#put(key, ids) ⇒ Object



33
34
35
# File 'lib/xunch/cache/list_id_cache.rb', line 33

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

#putex(key, ids, ttl) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xunch/cache/list_id_cache.rb', line 37

def putex(key, ids, ttl)
  raise "key can not be nil." unless key != nil
  raise "ids can not be nil." unless ids != nil
  sub_keys = []
  ids.each { | id |
    raise "id in ids can not be nil." unless ids != nil
    sub_keys.push(id.to_s)
  }
  if sub_keys.length == 0
    return nil
  end
  temp_key = assembleTempKey(key)
  new_key = assembleKey(key)
  @shard_redis.lset(temp_key,new_key,sub_keys,ttl)
end

#remove(key, *sub_keys) ⇒ Object



53
54
55
56
57
58
# File 'lib/xunch/cache/list_id_cache.rb', line 53

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



61
62
63
64
65
# File 'lib/xunch/cache/list_id_cache.rb', line 61

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