Class: Xunch::CacheBuilder
- Inherits:
-
Object
- Object
- Xunch::CacheBuilder
- Defined in:
- lib/xunch/cache/cache_builder.rb
Class Method Summary collapse
-
.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.
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# 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| = {} shard_info_config.each { |k,v| [k.to_sym] = v } shards[[:name]] = } caches = {} lazy_caches = {} cache_configs.each { |cache_key, cache_config| puts "loading cache #{cache_key} #{cache_config}" if cache_config["type"] == nil raise ArgumentError.new("#{cache_key} cache type can't be nil.") end if cache_config["shards"] == nil raise ArgumentError.new("#{cache_key} cache shards can't be nil.") end shard_names = cache_config["shards"].split(",") if cache_config["driver"] driver = cache_config["driver"].downcase.to_sym puts "#{cache_key} use redis driver #{driver}" end shard_infos = [] shard_size = shard_names.size shard_names.each { |shard| split = shard.split(":") shard_name = split[0] weight = split[1] = {} if driver [:driver] = driver if [:driver] == :synchrony and cache_config["type"].start_with?("list") raise ArgumentError.new("list cache can't use synchrony driver. cache key is #{cache_key}") end end if cache_config["timeout"] [:timeout] = cache_config["timeout"] end if cache_config["size"] [:size] = cache_config["size"] / shard_size if [:size] == 0 [:size] = 1 end end if weight [:weight] = weight.to_i end if shards[shard_name] .merge!(shards[shard_name]) else raise ArgumentError.new("Unknown shard #{shard_name}, initialize cache #{cache_key} failed.") end shard_infos.push(ShardInfo.new()) } 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::LISTID cache = Xunch::ListIdCache.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 |