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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b79c05d630df6693c5e414e4c715beed1ec241e3731d71fa9e4bf93a4406e6e6
4
- data.tar.gz: 074a40df96e911ac2a5150325075336daec5600e64ba53cd8956e0aa92a7d3c1
3
+ metadata.gz: e8f39c6defd1204754f92cd66b60f31bb5ee374bfd3818d8a7d86af6d06cc13a
4
+ data.tar.gz: c90c62883e589291e7d605e116881371a542a00d8f0cf59c45a05fc0c28353a2
5
5
  SHA512:
6
- metadata.gz: 2319028a0e9cb0813e2a3e4bda3c28afb06a8e5305938ff3abb74fdd6885a2080b6025b90452525cbd34c641c191520ccea4f5b7cea9419cc83c2c5954f5054c
7
- data.tar.gz: bac1356af719d330b90258d12429b4c32b3133d7e06176a5e7219532d4ff1cf8b5c463d9ccf318db5b24b301c29c80cf20a80be67af54ea9546964aa800650ff
6
+ metadata.gz: 2ca4ad6417c77e1dc8c4e7a0cd0c8c41f6e1fbefd0bec032b9b3442c24e28570cf01614f44d74d3e2ec8b7273bfc6eeacd0c2bba884cf8088bdd9a1a81c5efac
7
+ data.tar.gz: 8fd64169c4dccd1775dae0a4eacfdbf6a029240422c19c422b1babb256115ca0fa84fc33e439b1ee68b5fc920ebcafe8137f2c2a3e3052f0018ff710c8d839b3
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fmcache (0.4.0)
4
+ fmcache (0.5.0)
5
5
  field_mask_parser (~> 0.4.3)
6
6
 
7
7
  GEM
@@ -64,4 +64,4 @@ DEPENDENCIES
64
64
  rspec (~> 3.8)
65
65
 
66
66
  BUNDLED WITH
67
- 1.16.2
67
+ 1.17.2
@@ -4,6 +4,7 @@ require "fmcache/encoder"
4
4
  require "fmcache/engine"
5
5
  require "fmcache/field"
6
6
  require "fmcache/helper"
7
+ require "fmcache/id_key_gen"
7
8
  require "fmcache/incomplete_info"
8
9
  require "fmcache/jsonizer"
9
10
  require "fmcache/version"
@@ -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[Helper.to_key(id)] = h
24
+ r[@id_key_gen.to_key(id)] = h
20
25
  end
21
26
  r
22
27
  end
@@ -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
- def initialize(client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil, json_serializer: nil)
11
- @client = Client.new(client, notifier)
12
- @fm_parser = wrap(fm_parser)
13
- @ttl = ttl
14
- @encoder = Encoder.new
15
- @decoder = Decoder.new(@fm_parser)
16
- @jsonizer = Jsonizer.new(json_serializer)
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 = Helper.to_keys(ids)
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 = Helper.to_ids(with_no_id.keys)
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: Helper.to_keys(ids))
93
+ client.del(keys: @id_key_gen.to_keys(ids))
85
94
  end
86
95
 
87
96
  private
@@ -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
@@ -1,3 +1,3 @@
1
1
  module FMCache
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
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.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: 2018-12-25 00:00:00.000000000 Z
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
- rubyforge_project:
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