Class: Xunch::CacheBuilder

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

Class Method Summary collapse

Class Method Details

.build(file) ⇒ Object

every cache config hava attributes below:

‘type’

> the cache type, have object|field_object|list_object

‘name’

> the cache name, used for redis key prefix

‘version’

> the cache version, maybe you change your cache object format and you will change a version of the cache

‘expire_time’

> cache expire time for every key in millisecond

‘cache_class’

> which type of object you cache, this will help us to encode/decode your object

‘key_field_name’

> define which field in the object we used for redis key

‘regex’

> a regex string which used for match consistency hash key

‘shard_infos’

> define which redis instance we used for caching



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xunch/cache/cache_builder.rb', line 14

def self.build(file)
  configs = YAML.load_file(file)
  shard_info_configs = configs["shard_infos"]
  cache_configs = configs["caches"]

  shards = {}
  shard_info_configs.each_value  { |shard_info_config|
    options = {}
    shard_info_config.each { |k,v|
        options[k.to_sym] = v
    }
    shards[options[:name]] = ShardInfo.new(options)
  }
  caches = {}
  lazy_caches = {}
  cache_configs.each_value  { |cache_config|
    shard_names = cache_config["shards"].split(",")
    shard_infos = []
    shard_names.each { |shard_name|
      shard_infos.push(shards[shard_name])
    }
    case cache_config["type"]
    when CacheType::OBJECT
      cache = Xunch::ObjectCache.new(cache_config,shard_infos)
      caches[cache_config["name"]] = cache
    when CacheType::FIELDOBJECT
      cache = Xunch::FieldObjectCache.new(cache_config,shard_infos)
      caches[cache_config["name"]] = cache
    when CacheType::LISTOBJECT
      lazy_caches[cache_config] = shard_infos
    when CacheType::LISTFIELDOBJECT
      lazy_caches[cache_config] = shard_infos
    else
      raise XunchConfigError.new("Unknown cache type #{cache_config["type"]}.")
    end
  }
  lazy_caches.each { |cache_config,shard_infos|
    delegate = caches[cache_config["delegate"]]
    raise XunchConfigError.new("list_cache init error, delegate does not exist.") unless delegate != nil
    cache = nil
    case cache_config["type"]
    when CacheType::LISTOBJECT
      cache = Xunch::ListObjectCache.new(cache_config,shard_infos,delegate)
    when CacheType::LISTFIELDOBJECT
      cache = Xunch::ListFieldObjectCache.new(cache_config,shard_infos,delegate)
    else
      raise XunchConfigError.new("Unknown cache type #{cache_config["type"]}.")
    end
    
    caches[cache_config["name"]] = cache
  }
  caches
end