evt-entity_cache 0.14.0.0 → 0.14.0.1

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
  SHA1:
3
- metadata.gz: 60ce778606243f14d742f85711593dbf6fe226ec
4
- data.tar.gz: eadfeebaaac7e8477278e938eaa90e8328d1d307
3
+ metadata.gz: 3014f3bc8543059ba7f26b57e2f8cd0963906a6a
4
+ data.tar.gz: 7acbff224fdc2ff7b6e75c6779a7cb4668e7c082
5
5
  SHA512:
6
- metadata.gz: 5d77d5302fac329c64b7642d2553a0dbdc0e470aa1744b1922fd6f06102dd33bd64a67c911a0ddcf49360e6eaa2072f02ccfd307353ed55d22751c56840bc124
7
- data.tar.gz: 0af5181fd7d7998c0d46972163ed1f3859b3c9f25ff5d64b35a72dbe1724374f4cd34584473521ef397ba4d9350a2a36aca03d73ae33b1dad0193fbc3a09af35
6
+ metadata.gz: 1e1f097e2c67571aab3ed85e145ac90bf840f59e7f7f0bf68a19e790f7081290ebfe0ef2bee47d1fb3be3d3229edaefeb8ea95d092d7a199b5929821cde55672
7
+ data.tar.gz: 78c4aef2dd3281b1e5e1220aae805477a5a899bbc33ef85539701c9675e7291a345415573837eab6c264bdd78700772a412da86ce0ddccc722916ec3016e6149
@@ -0,0 +1,122 @@
1
+ class EntityCache
2
+ include Log::Dependency
3
+
4
+ configure :entity_cache
5
+
6
+ attr_writer :persist_interval
7
+ def persist_interval
8
+ @persist_interval ||= Defaults.persist_interval
9
+ end
10
+
11
+ dependency :clock, Clock::UTC
12
+ dependency :temporary_store, Store::Temporary
13
+ dependency :persistent_store, Store::Persistent
14
+
15
+ def self.build(subject, scope: nil, persist_interval: nil, persistent_store: nil, persistent_store_session: nil)
16
+ instance = new
17
+
18
+ instance.configure(
19
+ subject: subject,
20
+ scope: scope,
21
+ persist_interval: persist_interval,
22
+ persistent_store: persistent_store,
23
+ persistent_store_session: persistent_store_session
24
+ )
25
+
26
+ instance
27
+ end
28
+
29
+ def configure(subject:, scope: nil, persist_interval: nil, persistent_store: nil, persistent_store_session: nil)
30
+ persistent_store ||= Store::Persistent::Null
31
+
32
+ unless persist_interval.nil?
33
+ self.persist_interval = persist_interval
34
+ end
35
+
36
+ Store::Temporary.configure(self, subject, scope: scope)
37
+
38
+ persistent_store.configure(self, subject, session: persistent_store_session)
39
+
40
+ Clock::UTC.configure(self)
41
+ end
42
+
43
+ def get(id)
44
+ logger.trace { "Get entity (ID: #{id.inspect})" }
45
+
46
+ record = temporary_store.get(id)
47
+
48
+ if record.nil?
49
+ record = restore(id)
50
+ end
51
+
52
+ if record.nil?
53
+ logger.info { "Get entity failed; cache miss (ID: #{id.inspect}, #{Record::LogText.get(record)})" }
54
+ else
55
+ logger.info { "Get entity done (ID: #{id.inspect}, #{Record::LogText.get(record)})" }
56
+ end
57
+
58
+ record
59
+ end
60
+
61
+ def put(id, entity, version, time: nil, persisted_version: nil, persisted_time: nil)
62
+ time ||= clock.now
63
+
64
+ updated_persistent_store = false
65
+
66
+ record = Record.build(id, entity, version, time)
67
+
68
+ logger.trace { "Put entity (ID: #{id.inspect}, #{Record::LogText.get(record)}, Persist Interval: #{persist_interval.inspect})" }
69
+
70
+ if persist?(version, persisted_version)
71
+ persistent_store.put(id, entity, version, time)
72
+
73
+ persisted_version = version
74
+ persisted_time = time
75
+ updated_persistent_store = true
76
+ end
77
+
78
+ record.persisted_version = persisted_version
79
+ record.persisted_time = persisted_time
80
+
81
+ temporary_store.put(record)
82
+
83
+ logger.info { "Put entity done (ID: #{id.inspect}, #{Record::LogText.get(record)}, Persist Interval: #{persist_interval.inspect}, Updated Persistent Store: #{updated_persistent_store})" }
84
+
85
+ record
86
+ end
87
+
88
+ def restore(id)
89
+ logger.trace { "Restoring entity (ID: #{id.inspect})" }
90
+
91
+ entity, version, time = persistent_store.get(id)
92
+
93
+ if entity.nil?
94
+ logger.debug { "Could not restore entity (ID: #{id.inspect})" }
95
+
96
+ return nil
97
+ end
98
+
99
+ record = Record.build(
100
+ id,
101
+ entity,
102
+ version,
103
+ time,
104
+ persisted_version: version,
105
+ persisted_time: time
106
+ )
107
+
108
+ temporary_store.put(record)
109
+
110
+ logger.debug { "Restored entity (ID: #{id.inspect}, #{Record::LogText.get(record)})" }
111
+
112
+ record
113
+ end
114
+
115
+ def persist?(version, persisted_version)
116
+ persisted_version ||= -1
117
+
118
+ age_versions = version - persisted_version
119
+
120
+ age_versions >= persist_interval
121
+ end
122
+ end
@@ -0,0 +1,80 @@
1
+ class EntityCache
2
+ module Store
3
+ module Persistent
4
+ def self.included(cls)
5
+ cls.class_exec do
6
+ include Log::Dependency
7
+
8
+ extend Build
9
+ extend RegisterTelemetrySink
10
+
11
+ configure :persistent_store
12
+
13
+ dependency :telemetry, ::Telemetry
14
+
15
+ initializer :subject
16
+
17
+ virtual :configure
18
+ abstract :get
19
+ abstract :put
20
+
21
+ prepend Configure
22
+ prepend Get
23
+ prepend Put
24
+ end
25
+ end
26
+
27
+ module Configure
28
+ def configure(session: nil)
29
+ ::Telemetry.configure(self)
30
+
31
+ super
32
+ end
33
+ end
34
+
35
+ module Get
36
+ def get(id)
37
+ logger.trace { "Getting entity (ID: #{id.inspect})" }
38
+
39
+ entity, version, time = super
40
+
41
+ telemetry.record(:get, Telemetry::Data.new(id, entity, version, time))
42
+
43
+ logger.debug { "Get entity done (ID: #{id.inspect}, Entity Class: #{entity.class}, Version: #{version.inspect}, Time: #{Clock.iso8601(time)})" }
44
+
45
+ return entity, version, time
46
+ end
47
+ end
48
+
49
+ module Put
50
+ def put(id, entity, version, time)
51
+ logger.trace { "Putting entity (ID: #{id.inspect}, Entity Class: #{entity.class}, Version: #{version.inspect}, Time: #{Clock.iso8601(time)})" }
52
+
53
+ return_value = super
54
+
55
+ telemetry.record(:put, Telemetry::Data.new(id, entity, version, time))
56
+
57
+ logger.debug { "Put entity done (ID: #{id.inspect}, Entity Class: #{entity.class}, Version: #{version.inspect}, Time: #{Clock.iso8601(time)})" }
58
+
59
+ return_value
60
+ end
61
+ end
62
+
63
+ module Build
64
+ def build(subject, session: nil)
65
+ instance = new(subject)
66
+ instance.configure(session: session)
67
+ instance
68
+ end
69
+ end
70
+
71
+ module RegisterTelemetrySink
72
+ def register_telemetry_sink(persistent_store)
73
+ sink = Telemetry::Sink.new
74
+ persistent_store.telemetry.register(sink)
75
+ sink
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt-entity_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0.0
4
+ version: 0.14.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Eventide Project
@@ -101,11 +101,13 @@ files:
101
101
  - lib/entity_cache/controls/time.rb
102
102
  - lib/entity_cache/controls/version.rb
103
103
  - lib/entity_cache/defaults.rb
104
+ - lib/entity_cache/entity_cache.rb
104
105
  - lib/entity_cache/log.rb
105
106
  - lib/entity_cache/record.rb
106
107
  - lib/entity_cache/record/destructure.rb
107
108
  - lib/entity_cache/record/log_text.rb
108
109
  - lib/entity_cache/record/transformer.rb
110
+ - lib/entity_cache/store/persistent.rb
109
111
  - lib/entity_cache/store/persistent/null.rb
110
112
  - lib/entity_cache/store/persistent/substitute.rb
111
113
  - lib/entity_cache/store/persistent/telemetry.rb