Class: Xunch::FieldObjectCache
- Inherits:
-
Cache
- Object
- Cache
- Xunch::FieldObjectCache
show all
- Defined in:
- lib/xunch/cache/field_object_cache.rb
Instance Method Summary
collapse
-
#get(key) ⇒ Object
-
#get_with_field(key, fields) ⇒ Object
-
#initialize(options, shard_infos) ⇒ FieldObjectCache
constructor
A new instance of FieldObjectCache.
-
#multi_get(keys) ⇒ Object
-
#multi_get_with_field(keys, fields) ⇒ Object
-
#multi_put(values) ⇒ Object
-
#multi_put_with_field(values, fields) ⇒ Object
-
#multi_put_with_field_ex(values, ttl, fields) ⇒ Object
-
#multi_putex(values, ttl) ⇒ Object
-
#put(value) ⇒ Object
-
#put_with_field(value, fields) ⇒ Object
-
#put_with_field_ex(value, ttl, fields) ⇒ Object
-
#putex(value, ttl) ⇒ Object
Methods inherited from Cache
#batch_evict, #destroy, #evict, #ttl
Constructor Details
#initialize(options, shard_infos) ⇒ FieldObjectCache
Returns a new instance of FieldObjectCache.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/xunch/cache/field_object_cache.rb', line 8
def initialize(options, shard_infos)
super
fields = options['fields']
if fields == nil
raise XunchConfigError.new("fields can not be nil")
end
fields_array = fields.split(",")
if fields_array.length == 1 and fields_array[0].strip.empty?
raise XunchConfigError.new("fields config error")
end
formatted_fields_array = Utils.format_fields(fields_array)
@fields_array = formatted_fields_array
@codec = HashCodec.new(@options[:cache_class],formatted_fields_array)
end
|
Instance Method Details
#get(key) ⇒ Object
23
24
25
26
27
|
# File 'lib/xunch/cache/field_object_cache.rb', line 23
def get(key)
redis_key = assembleKey(key)
data = @shard_redis.mapped_hget(redis_key,@fields_array)
@codec.decode(data)
end
|
#get_with_field(key, fields) ⇒ Object
72
73
74
75
76
|
# File 'lib/xunch/cache/field_object_cache.rb', line 72
def get_with_field(key, fields)
redis_key = assembleKey(key)
data = @shard_redis.mapped_hget(redis_key,fields)
@codec.decode_fields(data,fields)
end
|
#multi_get(keys) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/xunch/cache/field_object_cache.rb', line 43
def multi_get(keys)
redis_keys = Array.new(keys.length)
for i in 0 .. keys.length - 1 do
redis_keys[i] = assembleKey(keys[i])
end
datas = @shard_redis.mapped_hmget(redis_keys,@fields_array)
for i in 0 .. datas.length - 1 do
datas[i] = @codec.decode(datas[i])
end
return datas
end
|
#multi_get_with_field(keys, fields) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/xunch/cache/field_object_cache.rb', line 109
def multi_get_with_field(keys, fields)
for i in 0 .. keys.length - 1 do
keys[i] = assembleKey(keys[i])
end
datas = @shard_redis.mapped_hmget(keys,fields)
for i in 0 .. datas.length - 1 do
datas[i] = @codec.decode_fields(datas[i],fields)
end
return datas
end
|
#multi_put(values) ⇒ Object
55
56
57
|
# File 'lib/xunch/cache/field_object_cache.rb', line 55
def multi_put(values)
multi_putex(values,@options[:expire_time])
end
|
#multi_put_with_field(values, fields) ⇒ Object
105
106
107
|
# File 'lib/xunch/cache/field_object_cache.rb', line 105
def multi_put_with_field(values, fields)
multi_put_with_field_ex(values,@options[:expire_time],fields)
end
|
#multi_put_with_field_ex(values, ttl, fields) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/xunch/cache/field_object_cache.rb', line 92
def multi_put_with_field_ex(values, ttl, fields)
kvs = Hash.new
values.each { | value |
if value == nil
next
end
key = getCacheObjectKey(value)
key = assembleKey(key);
kvs[key] = @codec.encode_fields(value,fields);
}
@shard_redis.hmsetall(kvs, ttl);
end
|
#multi_putex(values, ttl) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/xunch/cache/field_object_cache.rb', line 59
def multi_putex(values,ttl)
kvs = Hash.new
values.each { | value |
if value == nil
next
end
key = getCacheObjectKey(value)
redis_key = assembleKey(key);
kvs[redis_key] = @codec.encode(value);
}
@shard_redis.hmsetall(kvs, ttl);
end
|
#put(value) ⇒ Object
29
30
31
|
# File 'lib/xunch/cache/field_object_cache.rb', line 29
def put(value)
putex(value,@options[:expire_time])
end
|
#put_with_field(value, fields) ⇒ Object
78
79
80
|
# File 'lib/xunch/cache/field_object_cache.rb', line 78
def put_with_field(value, fields)
put_with_field_ex(value,@options[:expire_time],fields)
end
|
#put_with_field_ex(value, ttl, fields) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/xunch/cache/field_object_cache.rb', line 82
def put_with_field_ex(value, ttl, fields)
if(value == nil)
return
end
key = getCacheObjectKey(value)
key = assembleKey(key)
data = @codec.encode_fields(value,fields)
@shard_redis.hsetall(key,data,ttl)
end
|
#putex(value, ttl) ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/xunch/cache/field_object_cache.rb', line 33
def putex(value, ttl)
if(value == nil)
return
end
key = getCacheObjectKey(value)
key = assembleKey(key)
data = @codec.encode(value)
@shard_redis.hsetall(key,data,ttl)
end
|