fmcache 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/lib/fmcache.rb +1 -0
- data/lib/fmcache/encoder.rb +6 -1
- data/lib/fmcache/engine.rb +21 -12
- data/lib/fmcache/helper.rb +0 -30
- data/lib/fmcache/id_key_gen.rb +40 -0
- data/lib/fmcache/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8f39c6defd1204754f92cd66b60f31bb5ee374bfd3818d8a7d86af6d06cc13a
|
4
|
+
data.tar.gz: c90c62883e589291e7d605e116881371a542a00d8f0cf59c45a05fc0c28353a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ca4ad6417c77e1dc8c4e7a0cd0c8c41f6e1fbefd0bec032b9b3442c24e28570cf01614f44d74d3e2ec8b7273bfc6eeacd0c2bba884cf8088bdd9a1a81c5efac
|
7
|
+
data.tar.gz: 8fd64169c4dccd1775dae0a4eacfdbf6a029240422c19c422b1babb256115ca0fa84fc33e439b1ee68b5fc920ebcafe8137f2c2a3e3052f0018ff710c8d839b3
|
data/Gemfile.lock
CHANGED
data/lib/fmcache.rb
CHANGED
data/lib/fmcache/encoder.rb
CHANGED
@@ -2,6 +2,11 @@ require "fmcache/encoder/itemizer"
|
|
2
2
|
|
3
3
|
module FMCache
|
4
4
|
class Encoder
|
5
|
+
# @param [FMCache::IdKeyGen]
|
6
|
+
def initialize(id_key_gen)
|
7
|
+
@id_key_gen = id_key_gen
|
8
|
+
end
|
9
|
+
|
5
10
|
# @param [<Hash>] values
|
6
11
|
# @param [FieldMaskParser::Node] field_mask
|
7
12
|
# @return [{ String => { String => <Hash> } }]
|
@@ -16,7 +21,7 @@ module FMCache
|
|
16
21
|
h.merge! encode_one(value, field_mask)
|
17
22
|
|
18
23
|
id = value.fetch(:id)
|
19
|
-
r[
|
24
|
+
r[@id_key_gen.to_key(id)] = h
|
20
25
|
end
|
21
26
|
r
|
22
27
|
end
|
data/lib/fmcache/engine.rb
CHANGED
@@ -5,15 +5,24 @@ module FMCache
|
|
5
5
|
# @param [Redis | MockRRedis] client
|
6
6
|
# @param [Proc] fm_parser
|
7
7
|
# @param [Integer] ttl
|
8
|
-
# @param [Proc] notifier
|
9
|
-
# @param [#dump#load] json_serializer
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
8
|
+
# @param [Proc, nil] notifier
|
9
|
+
# @param [#dump#load, nil] json_serializer
|
10
|
+
# @param [String, nil] id_key_prefix
|
11
|
+
def initialize(
|
12
|
+
client:,
|
13
|
+
fm_parser:,
|
14
|
+
ttl: DEFAULT_TTL,
|
15
|
+
notifier: nil,
|
16
|
+
json_serializer: nil,
|
17
|
+
id_key_prefix: nil
|
18
|
+
)
|
19
|
+
@client = Client.new(client, notifier)
|
20
|
+
@fm_parser = wrap(fm_parser)
|
21
|
+
@ttl = ttl
|
22
|
+
@id_key_gen = IdKeyGen.new(id_key_prefix)
|
23
|
+
@encoder = Encoder.new(@id_key_gen)
|
24
|
+
@decoder = Decoder.new(@fm_parser)
|
25
|
+
@jsonizer = Jsonizer.new(json_serializer)
|
17
26
|
end
|
18
27
|
|
19
28
|
attr_reader :client, :fm_parser, :encoder, :decoder
|
@@ -34,13 +43,13 @@ module FMCache
|
|
34
43
|
ids = ids.map(&:to_i)
|
35
44
|
normalize!(field_mask)
|
36
45
|
|
37
|
-
keys =
|
46
|
+
keys = @id_key_gen.to_keys(ids)
|
38
47
|
fields = Helper.to_fields(field_mask)
|
39
48
|
h = client.get(keys: keys, fields: fields)
|
40
49
|
|
41
50
|
with_id, with_no_id = split(h)
|
42
51
|
v, i_v, i_i = decode(@jsonizer.dejsonize(with_id), field_mask)
|
43
|
-
with_no_id_list =
|
52
|
+
with_no_id_list = @id_key_gen.to_ids(with_no_id.keys)
|
44
53
|
|
45
54
|
return v, i_v, merge(i_i, with_no_id_list, field_mask)
|
46
55
|
end
|
@@ -81,7 +90,7 @@ module FMCache
|
|
81
90
|
# @param [<Integer | String>] ids
|
82
91
|
def delete(ids:)
|
83
92
|
ids = ids.map(&:to_i)
|
84
|
-
client.del(keys:
|
93
|
+
client.del(keys: @id_key_gen.to_keys(ids))
|
85
94
|
end
|
86
95
|
|
87
96
|
private
|
data/lib/fmcache/helper.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module FMCache
|
2
2
|
class Helper
|
3
|
-
KEY_PREFIX = "fmcache:"
|
4
|
-
|
5
3
|
class << self
|
6
4
|
# @param [FieldMaskParser::Node] field_mask
|
7
5
|
# @param [<Symbol>] prefix
|
@@ -10,34 +8,6 @@ module FMCache
|
|
10
8
|
field_mask.to_paths(prefix: prefix, sort: false)
|
11
9
|
end
|
12
10
|
|
13
|
-
# @param [<Integer>] ids
|
14
|
-
# @return [<String>]
|
15
|
-
def to_keys(ids)
|
16
|
-
ids.map { |id| to_key(id) }
|
17
|
-
end
|
18
|
-
|
19
|
-
# @param [Integer] id
|
20
|
-
# @return [String]
|
21
|
-
def to_key(id)
|
22
|
-
"#{KEY_PREFIX}#{id}"
|
23
|
-
end
|
24
|
-
|
25
|
-
# @param [<String>] keys
|
26
|
-
# @return [<Integer>]
|
27
|
-
def to_ids(keys)
|
28
|
-
keys.map { |key| to_id(key) }
|
29
|
-
end
|
30
|
-
|
31
|
-
# @param [String] id
|
32
|
-
# @return [Integer]
|
33
|
-
def to_id(key)
|
34
|
-
if key[0..7] == KEY_PREFIX
|
35
|
-
key[8..-1].to_i
|
36
|
-
else
|
37
|
-
raise "invalid key: #{key}"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
11
|
# @param [<Hash>] values
|
42
12
|
# @param [<Integer>] ids
|
43
13
|
# @return [<Hash>]
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module FMCache
|
2
|
+
class IdKeyGen
|
3
|
+
DEFAULT_KEY_PREFIX = "fmcache"
|
4
|
+
|
5
|
+
# @param [String, nil] prefix
|
6
|
+
def initialize(prefix)
|
7
|
+
p = prefix || DEFAULT_KEY_PREFIX
|
8
|
+
@prefix = "#{p}:"
|
9
|
+
end
|
10
|
+
|
11
|
+
# @param [<Integer>] ids
|
12
|
+
# @return [<String>]
|
13
|
+
def to_keys(ids)
|
14
|
+
ids.map { |id| to_key(id) }
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [Integer] id
|
18
|
+
# @return [String]
|
19
|
+
def to_key(id)
|
20
|
+
"#{@prefix}#{id}"
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param [<String>] keys
|
24
|
+
# @return [<Integer>]
|
25
|
+
def to_ids(keys)
|
26
|
+
keys.map { |key| to_id(key) }
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param [String] id
|
30
|
+
# @return [Integer]
|
31
|
+
def to_id(key)
|
32
|
+
prefix_len = @prefix.size
|
33
|
+
if key[0..(prefix_len-1)] == @prefix
|
34
|
+
key[prefix_len..-1].to_i
|
35
|
+
else
|
36
|
+
raise "invalid key: #{key}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/fmcache/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fmcache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nao Minami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -153,6 +153,7 @@ files:
|
|
153
153
|
- lib/fmcache/engine.rb
|
154
154
|
- lib/fmcache/field.rb
|
155
155
|
- lib/fmcache/helper.rb
|
156
|
+
- lib/fmcache/id_key_gen.rb
|
156
157
|
- lib/fmcache/incomplete_info.rb
|
157
158
|
- lib/fmcache/jsonizer.rb
|
158
159
|
- lib/fmcache/jsonizer/default_json_serializer.rb
|
@@ -176,8 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
177
|
- !ruby/object:Gem::Version
|
177
178
|
version: '0'
|
178
179
|
requirements: []
|
179
|
-
|
180
|
-
rubygems_version: 2.7.6
|
180
|
+
rubygems_version: 3.0.3
|
181
181
|
signing_key:
|
182
182
|
specification_version: 4
|
183
183
|
summary: Library for caching json masked by FieldMask
|