Class: Restforce::DB::TimestampCache
- Inherits:
-
Object
- Object
- Restforce::DB::TimestampCache
- Defined in:
- lib/restforce/db/timestamp_cache.rb
Overview
Restforce::DB::TimestampCache serves to cache the timestamps of the most recent known updates to records through the Restforce::DB system. It allows for more intelligent decision-making regarding what constitutes “stale” data during a synchronization.
While we can tell which user triggered the most recent changes to a record in Salesforce, we can’t tell if any modifications to that record were a result of a background Apex trigger or workflow (which apply any changes as if they were the user whose actions initiated the callback).
In order to distinguish between updates made by the worker and updates made _in response to_ changes by the worker, we have to check the record’s update timestamp against the timestamp of the last known update made by the system. This class serves as a mechanism to track the values for this comparison.
Instance Method Summary collapse
-
#cache_timestamp(instance) ⇒ Object
Public: Add a known update timestamp to the cache for the passed object.
-
#changed?(instance) ⇒ Boolean
Public: Has the passed instance been modified since the last known system-triggered update? This accounts for changes possibly introduced by callbacks and triggers.
-
#dump_timestamps(io) ⇒ Object
Public: Dump the currently cached timestamps into the specified writable object.
-
#initialize ⇒ TimestampCache
constructor
Public: Initialize a new Restforce::DB::TimestampCache.
-
#load_timestamps(io) ⇒ Object
Public: Load the previous collection of cached timestamps from the passed readable object.
-
#reset ⇒ Object
Public: Reset the cache.
-
#timestamp(instance) ⇒ Object
Public: Get the most recently-stored timestamp for the passed object.
Constructor Details
#initialize ⇒ TimestampCache
Public: Initialize a new Restforce::DB::TimestampCache.
23 24 25 |
# File 'lib/restforce/db/timestamp_cache.rb', line 23 def initialize reset end |
Instance Method Details
#cache_timestamp(instance) ⇒ Object
Public: Add a known update timestamp to the cache for the passed object.
instance - A Restforce::DB::Instances::Base.
Returns an Array of Restforce::DB::Instances::Base.
32 33 34 |
# File 'lib/restforce/db/timestamp_cache.rb', line 32 def (instance) @cache[key_for(instance)] = instance.last_update end |
#changed?(instance) ⇒ Boolean
Public: Has the passed instance been modified since the last known system-triggered update? This accounts for changes possibly introduced by callbacks and triggers.
instance - A Restforce::DB::Instances::Base.
Returns a Boolean.
55 56 57 58 59 60 61 62 |
# File 'lib/restforce/db/timestamp_cache.rb', line 55 def changed?(instance) return true unless instance.updated_internally? last_update = (instance) return true unless last_update instance.last_update > last_update end |
#dump_timestamps(io) ⇒ Object
Public: Dump the currently cached timestamps into the specified writable object.
io - An IO object opened for writing.
Returns nothing.
90 91 92 |
# File 'lib/restforce/db/timestamp_cache.rb', line 90 def (io) io.write(YAML.dump(@cache)) end |
#load_timestamps(io) ⇒ Object
Public: Load the previous collection of cached timestamps from the passed readable object.
io - An IO object opened for reading.
Returns nothing.
80 81 82 |
# File 'lib/restforce/db/timestamp_cache.rb', line 80 def (io) @cache = YAML.load(io.read) || {} end |
#reset ⇒ Object
Public: Reset the cache. Expires the previously-cached timestamps, and retires the currently-cached timestamps to ensure that they are only factored into the current synchronization run.
Returns nothing.
69 70 71 72 |
# File 'lib/restforce/db/timestamp_cache.rb', line 69 def reset @retired_cache = @cache || {} @cache = {} end |
#timestamp(instance) ⇒ Object
Public: Get the most recently-stored timestamp for the passed object. Falls back to the retired timestamps to ensure that this run is aware of the modifications made during the previous run.
instance - A Restforce::DB::Instances::Base.
Returns a Time or nil.
43 44 45 46 |
# File 'lib/restforce/db/timestamp_cache.rb', line 43 def (instance) key = key_for(instance) @cache.fetch(key) { @retired_cache[key] } end |