fmcache 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -0
- data/lib/fmcache.rb +1 -0
- data/lib/fmcache/engine.rb +4 -3
- data/lib/fmcache/helper.rb +0 -36
- data/lib/fmcache/jsonizer.rb +46 -0
- data/lib/fmcache/jsonizer/default_json_serializer.rb +19 -0
- data/lib/fmcache/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec627f276181ebe2b3545e82864946010a0f056221075829eb53a2fc3884575f
|
4
|
+
data.tar.gz: bcc99177ea4e1b935eff30925e2ccb6a3fde41b19034c1741f7c4d6109c506de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c6393c6abd0f841036bf1f4db7c33bf7328231c86373401de104bd388079df9b0907019cd7d46d53e027535d5e339362c06431c629c82813eeaac0aa2d4f464
|
7
|
+
data.tar.gz: ddda0f0ed14eddf7a528e1282cd54e126fc72dd641037fe0b4646813c911450ada586300d8b9e3188b3cf5045944b350023c009532484865e7a28f0fe5966c88
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# FMCache
|
2
2
|
|
3
|
+
[](https://travis-ci.org/south37/fmcache)
|
4
|
+
[](https://badge.fury.io/rb/fmcache)
|
5
|
+
|
3
6
|
Library for caching json masked by FieldMask
|
4
7
|
|
5
8
|
## Installation
|
data/lib/fmcache.rb
CHANGED
data/lib/fmcache/engine.rb
CHANGED
@@ -3,12 +3,13 @@ module FMCache
|
|
3
3
|
DEFAULT_TTL = 7 * 24 * 3600 # 7 days
|
4
4
|
|
5
5
|
# @param [Redis | MockRRedis] client
|
6
|
-
def initialize(client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil)
|
6
|
+
def initialize(client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil, json_serializer: nil)
|
7
7
|
@client = Client.new(client, notifier)
|
8
8
|
@fm_parser = wrap(fm_parser)
|
9
9
|
@ttl = ttl
|
10
10
|
@encoder = Encoder.new
|
11
11
|
@decoder = Decoder.new(@fm_parser)
|
12
|
+
@jsonizer = Jsonizer.new(json_serializer)
|
12
13
|
end
|
13
14
|
|
14
15
|
attr_reader :client, :fm_parser, :encoder, :decoder
|
@@ -19,7 +20,7 @@ module FMCache
|
|
19
20
|
def write(values:, field_mask:)
|
20
21
|
normalize!(field_mask)
|
21
22
|
h = encode(values, field_mask)
|
22
|
-
client.set(values:
|
23
|
+
client.set(values: @jsonizer.jsonize(h), ttl: @ttl)
|
23
24
|
end
|
24
25
|
|
25
26
|
# @param [<Integer | String>] ids
|
@@ -32,7 +33,7 @@ module FMCache
|
|
32
33
|
keys = Helper.to_keys(ids)
|
33
34
|
fields = Helper.to_fields(field_mask).map(&:to_s)
|
34
35
|
h = client.get(keys: keys, fields: fields)
|
35
|
-
decode(merge(
|
36
|
+
decode(merge(@jsonizer.dejsonize(h), ids), field_mask)
|
36
37
|
end
|
37
38
|
|
38
39
|
# @param [<Integer | String>] ids
|
data/lib/fmcache/helper.rb
CHANGED
@@ -26,42 +26,6 @@ module FMCache
|
|
26
26
|
"fmcache:#{id}"
|
27
27
|
end
|
28
28
|
|
29
|
-
# @param [{ String => { String => <Hash> } }] hash
|
30
|
-
# @return [{ String => { String => String } }]
|
31
|
-
def jsonize(hash)
|
32
|
-
r = {}
|
33
|
-
hash.each do |k, v|
|
34
|
-
h = {}
|
35
|
-
v.each do |kk, vv|
|
36
|
-
h[kk] = vv.to_json
|
37
|
-
end
|
38
|
-
r[k] = h
|
39
|
-
end
|
40
|
-
r
|
41
|
-
end
|
42
|
-
|
43
|
-
# @param [{ String => { String => String } }] hash
|
44
|
-
# @return [{ String => { String => <Hash> } }]
|
45
|
-
def dejsonize(hash)
|
46
|
-
r = {}
|
47
|
-
hash.each do |k, v|
|
48
|
-
h = {}
|
49
|
-
v.each do |kk, vv|
|
50
|
-
if vv.nil?
|
51
|
-
h[kk] = nil
|
52
|
-
else
|
53
|
-
begin
|
54
|
-
h[kk] = JSON.parse(vv, symbolize_names: true)
|
55
|
-
rescue
|
56
|
-
h[kk] = nil
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
r[k] = h
|
61
|
-
end
|
62
|
-
r
|
63
|
-
end
|
64
|
-
|
65
29
|
# @param [<Hash>] values
|
66
30
|
# @param [<Integer>] ids
|
67
31
|
# @return [<Hash>]
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "fmcache/jsonizer/default_json_serializer"
|
2
|
+
|
3
|
+
module FMCache
|
4
|
+
class Jsonizer
|
5
|
+
# @param [#dump#load | nil] json_serializer
|
6
|
+
def initialize(json_serializer)
|
7
|
+
@json_serializer = json_serializer || DefaultJsonSerializer
|
8
|
+
end
|
9
|
+
|
10
|
+
# @param [{ String => { String => <Hash> } }] hash
|
11
|
+
# @return [{ String => { String => String } }]
|
12
|
+
def jsonize(hash)
|
13
|
+
r = {}
|
14
|
+
hash.each do |k, v|
|
15
|
+
h = {}
|
16
|
+
v.each do |kk, vv|
|
17
|
+
h[kk] = @json_serializer.dump(vv)
|
18
|
+
end
|
19
|
+
r[k] = h
|
20
|
+
end
|
21
|
+
r
|
22
|
+
end
|
23
|
+
|
24
|
+
# @param [{ String => { String => String } }] hash
|
25
|
+
# @return [{ String => { String => <Hash> } }]
|
26
|
+
def dejsonize(hash)
|
27
|
+
r = {}
|
28
|
+
hash.each do |k, v|
|
29
|
+
h = {}
|
30
|
+
v.each do |kk, vv|
|
31
|
+
if vv.nil?
|
32
|
+
h[kk] = nil
|
33
|
+
else
|
34
|
+
begin
|
35
|
+
h[kk] = @json_serializer.load(vv)
|
36
|
+
rescue
|
37
|
+
h[kk] = nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
r[k] = h
|
42
|
+
end
|
43
|
+
r
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module FMCache
|
2
|
+
class Jsonizer
|
3
|
+
class DefaultJsonSerializer
|
4
|
+
class << self
|
5
|
+
# @param [Hash | Array] obj
|
6
|
+
# @return [String]
|
7
|
+
def dump(obj)
|
8
|
+
JSON.dump(obj)
|
9
|
+
end
|
10
|
+
|
11
|
+
# @param [String] json
|
12
|
+
# @return [Hash | Array]
|
13
|
+
def load(json)
|
14
|
+
JSON.parse(json, symbolize_names: true)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
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.1.
|
4
|
+
version: 0.1.1
|
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-
|
11
|
+
date: 2018-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -154,6 +154,8 @@ files:
|
|
154
154
|
- lib/fmcache/field.rb
|
155
155
|
- lib/fmcache/helper.rb
|
156
156
|
- lib/fmcache/incomplete_info.rb
|
157
|
+
- lib/fmcache/jsonizer.rb
|
158
|
+
- lib/fmcache/jsonizer/default_json_serializer.rb
|
157
159
|
- lib/fmcache/version.rb
|
158
160
|
homepage: https://github.com/south37/fmcache
|
159
161
|
licenses:
|